-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infinite recursion when parsing expression grammar #23
Comments
You're getting infinite recursion because you've said that (modulo There are ways to write code like this differently so that it's not recursive, check out We're also thinking (see #5) about enhancing parsy to better handle cases like this. In a future version, it should be a) very easy to make code like yours work (automagic recursion elimination) and b) even easier to achieve what you're trying to achieve in more comprehensive way and less lines of code (chaining combinators). |
If you are trying to google for other solutions, this is a well-known problem with this type of parsing approach, called left recursive grammar. |
I see. So, would I be better off waiting on the left-recursion elimination, trying to rewrite to eliminate it, or go with an LR solution? I'm having a bit of trouble following the parsing logic in simple_eval.py. I see that the And then both My apologies, this isn't a general help forum. But thank you for offering advice. |
Try to eliminate it, it's not at all that hard.
It's very important that
Yes, this is to handle operator precedence and associativity (
(This is not parsy code, but should hopefully make sense). Again, this demonstrates the same idea: no left recursion, delegate to
You see, there's a lot of theory behind this (called the formal language theory) — it describes regular expressions, context-free grammars, LL and LR parsers, and so on. That way of manual left recursion elimination and that way to parse expressions are very standard and familiar for people who know the theoretical side of things. If you're interested in writing complex parsers or even compilers, I recommend you to read up on these things, e.g. this book will teach you all of that and more. |
It's been ages since I read the Dragon Book. I might need a refresher. Thanks for your help. |
I am trying to write a parser program that will parse a very minimal expression language; for now, all I want are variable names, string or numeric values, basic comparators (=, !=, <, >, <=, >=), boolean operators (AND, OR, and NOT), symbols for true, false, and null, and parentheses. I want to break these expressions up into parts that I can then assign to a syntax tree.
I've been able to make a parser which can do the simple comparison case, of "variable comparator value" form: x = 1, y = 2, etc. I can make it strip off the parens, though I don't yet know how to implement precedence. And I do have basic handling of an isolated boolean operator: I can parse a single AND expression, or OR expression, of the form "foo = 1 AND bar != 2" or the like.
But when I try to use the generalized form, the system overflows the maximum recursion stack. I'm not sure what I'm doing wrong here?
I've attached my code file. I cribbed some of it from the JSON example, such as the lexeme trick for dealing with whitespace.
parsertest.py.txt
The text was updated successfully, but these errors were encountered: