Closed NofelYaseen closed 5 years ago
I found a solution, but it seems a bit hacky. It creates a precedence of an imaginary token and works (at least for me).
precedence = (
('left', 'NAME'),
('right', 'IMAGINE')
)
def p_expressions(p):
'''
expressions : character
| expressions expressions %prec IMAGINE
'''
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + p[2]
print('expressions', p[0])
Close the issue, if there is no other way.
I've been a bit sidetracked (sorry for delayed response). Use of %prec is a known technique for resolving ambiguous grammar rules in LALR(1) parser generators like yacc/bison/ply, etc. The alternative would be to rewrite the grammar in some way to avoid ambiguity.
Below is a simplified version of what I am exactly trying to do. The problem is equivalent as the original is too large. Anyway, if you run this, we see the output is:
The problem I am having is that it is going in "bc" and then "abc". Is it possible to go in "ab" and then "abc".
I expected there would be a simple way to go using other way, but I couldn't find out. I am looking for parser precedence and associativity. The only thing I found here was token precedence and associativity.
I am aware it is possible to update it to
But I am hoping there is another way as things are not simple in the original problem. I need to combine two different expressions together.