Is pyparsing really a recursive descent parser?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Nov 2 11:47:13 CET 2007


On Fri, 02 Nov 2007 06:05:13 +0000, Just Another Victim of the Ambient
Morality wrote:

> Is pyparsing really a recursive descent parser?  I ask this because 
> there are grammars it can't parse that my recursive descent parser would 
> parse, should I have written one.  For instance:
> 
> 
> from pyparsing import *
> 
> grammar = OneOrMore(Word(alphas)) + Literal('end')
> grammar.parseString('First Second Third end')
> 
> 
>     Amazingly enough, this will fail to parse!
>     Now, maybe I have no idea what I'm talking about but shouldn't a 
> recursive descent parser recursively descend through all possible rule 
> combinations to discover one that works?  Why does pyparsing fail to parse 
> this?


Pyparsing is no recursive descent parser.  It doesn't go back in the input
stream.  The ``OneOrMore(Word(alphas))`` part "eats" the 'end' and when it
can't get more, the parser moves to the ``Literal('end')`` part which
fails because the 'end' is already gone.

>  Is there a way to get pyparsing to parse a grammar like this?

Negative lookahead maybe:

grammar = (OneOrMore(NotAny(Literal('end')) + Word(alphas))
           + Literal('end'))

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list