Is pyparsing really a recursive descent parser?

JimJJewett at JimJJewett at
Thu Nov 8 17:58:02 CET 2007

On Nov 3, 10:49 am, Paul McGuire <pt... at> wrote:

> grammar << ((word + grammar) | (word + Literal(end)))

> which works.  

[Clarifies that the common (and similar) solution doesn't work -- this
works only because the literal binds tightly to the word, so you can't
get a word that matches on its own.]

> Unfortunately, when the OneOrMore gets constructed, it does not have
> any visibility beyond knowing what is to be repeated.  Again, here is
> the data structure that is being built:
> - And
>   - OneOrMore
>     - Word(alphas)
>   - Literal('end')
> Only at the level of the And is there any awareness that the OneOrMore
> is followed by anything, let alone by something which could be
> misinterpreted as something matching the OneOrMore's repetition
> expression.

> Can you suggest a way I could generalize this, so that OneOrMore stops
> matching before it gets to 'end'?

Not efficiently.

I think JAVotAM's point was that you could make it at least an option
if OneOrMore (and ZeroOrMore ...) could return fallback results as

Then, when the And failed, it could retry with the next fallback
result from OneOrMore before it gives up completely.


More information about the Python-list mailing list