-
Notifications
You must be signed in to change notification settings - Fork 112
Open
Description
Hi!
I'm trying to parse 1 2 | 3 4
to
OR(
a =
AND(
a = 1
b = 2
)
b =
AND(
a = 3
b = 4
)
)
The 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.
from sly import Lexer, Parser
from collections import namedtuple
class TestLexer(Lexer):
tokens = { NUMBER, OR }
ignore = ' \t'
NUMBER = r'\d+'
OR = r'\|'
class TestParser(Parser):
debugfile = 'testparser.out'
tokens = TestLexer.tokens
@_('number_group number')
def number_group(self, p):
AND = namedtuple('AND', ('a', 'b'))
return AND(p[0], p[1])
@_('number_group OR number_group')
def number_group(self, p):
OR = namedtuple('OR', ('a', 'b'))
return OR(p[0], p[2])
@_('number')
def number_group(self, p):
return p[0]
@_('NUMBER')
def number(self, p):
return p.NUMBER
def debug_repr(node, level=0):
if hasattr(node, '_fields'):
yield ' ' * level + f'{node.__class__.__name__}('
for field in sorted(node._fields):
val = getattr(node, field)
if isinstance(val, (int, str)):
yield ' ' * (level + 1) + f'{field} = {val}'
else:
yield ' ' * (level + 1) + f'{field} ='
yield from debug_repr(val, level + 2)
yield ' ' * level + ')'
else:
yield ' ' * level + repr(node)
if __name__ == '__main__':
lexer = TestLexer()
parser = TestParser()
print("\n".join(debug_repr(parser.parse(lexer.tokenize('1 2 | 3 4')))))
Metadata
Metadata
Assignees
Labels
No labels