-
Notifications
You must be signed in to change notification settings - Fork 109
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
Need help with shift/reduce conflicts #87
Comments
Not easy, but I made it work. class TestParser(Parser):
tokens = TestLexer.tokens
# precedence - lower = higher priority
precedence = (
('left', OR),
('left', AND),
)
@_('expr')
def number_group(self, p):
return p[0]
@_('NUMBER')
def expr(self, p):
return p[0]
@_('number_group OR number_group')
def number_group(self, p):
OR = namedtuple('OR', ('a', 'b'))
return OR(p[0], p[2])
@_('expr expr %prec AND')
def expr(self, p):
AND = namedtuple('AND', ('a', 'b'))
return AND(p[0], p[1]) I learn from examples, and an example like this would've helped me a lot with understanding |
Hello. I added an improvement to SLY in a separate branch that gives extra information about the conflicts. Here is the output for your original source file:
This first example shows the two possible interpretation of a sequence with two ORs: the two possible interpretations are to construct the second OR and use the result as the second operand in the first OR. The second option is to first construct the leftmost OR expression and use the result as the left operand in the second OR expression. I believe both results are correct mathematically, the default of the parser is the option labeled SHIFT (so, the first option).
In this second conflict, the debug output shows the two possible interpretation of a sequence containing an OR operation followed by an AND operation. The two options are to either execute the AND operation first, using the result as the right operand to the OR operation, or to first reduce the OR operation and use the result as the left operand of the AND operation. Your solution works as you have assigned a higher priority to the AND operator over the OR operator, and you have specified left associativity for each operator, thus making a priority call for all the conflicts. |
Hi!
I'm trying to parse
1 2 | 3 4
toThe code is working as expected, but I get this warning: 2 shift/reduce conflicts, which I interpret as that my rules aren't all right. The docs say that
precedence
is the way to solve similar issues, but I don't see how I could apply precedence here. Would appreciate any help.The text was updated successfully, but these errors were encountered: