vlasovskikh / funcparserlib

Recursive descent parsing library for Python based on functional combinators
https://funcparserlib.pirx.ru
MIT License
338 stars 38 forks source link

Problem Combining Operator | and Operator + #48

Closed xie-dongping closed 7 years ago

xie-dongping commented 7 years ago

Consider the following simple and fictive example:

from funcparserlib.parser import many, some, finished

code = "aa"

p1 = some(lambda x: x == 'a')
p2 = many(p1)

p_or = p1 | p2

parsed_tokens = (p3+finished).parse("aa") 
# Error, since it would short-circuit to p1!

I encounter the case when writing a toy parser for a modeling language.

I'm not really familiar with the parsing theory, so I was wondering if this is the intended behavior for a parser combinator?

If it is not, how may I fix the __or__ method of the Parser class (maybe even more)? I thought about propagating the exceptions back to the __or__ method, or maybe you would have a better idea?

xie-dongping commented 7 years ago

I understand that the grammar defined above is ambiguous, and it is only used to illustrate the point, that I understand the or operator in a different way.

vlasovskikh commented 7 years ago

@xie-dongping Infinite left recursion is a known problem. There is an experimental branch declarative-api where I've added, among other things, automatic detection of left recursion that shows the problematic piece of your grammar. See also the description of the problem in the docs.

xie-dongping commented 7 years ago

Thank you for the reference! I've also solved my problem!