Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from rply import ParserGenerator
- from ast import Int, Sum, Sub, Say, String
- class Parser():
- def __init__(self):
- self.pg = ParserGenerator(
- # A list of all token names accepted by the parser.
- ['INTEGER', 'SAY', 'OPEN_PAREN', 'CLOSE_PAREN',
- 'SEMI_COLON', 'SUM', 'SUB', 'STRING']
- )
- def parse(self):
- @self.pg.production('say : SAY OPEN_PAREN expression CLOSE_PAREN
- SEMI_COLON')
- def say(p):
- return Say(p[2])
- @self.pg.production('expression : expression SUM expression')
- @self.pg.production('expression : expression SUB expression')
- def expression(p):
- left = p[0]
- right = p[2]
- operator = p[1]
- if operator.gettokentype() == 'SUM':
- return Sum(left, right)
- elif operator.gettokentype() == 'SUB':
- return Sub(left, right)
- @self.pg.production('expression : INTEGER')
- def int(p):
- return Int(p[0].value)
- @self.pg.production('expression : STRING')
- def string(p):
- return String(p[0].value)
- @self.pg.error
- def error_handler(token):
- raise ValueError("Ran into a %s where it wasn't expected" % token.gettokentype())
- def get_parser(self):
- return self.pg.build()
- say("yo");
- say("yo");
- say("yoyo");
- C:Usersgdog1Desktopprojintparser.py:42: ParserGeneratorWarning: 4
- shift/reduce conflicts
- return self.pg.build()
- Traceback (most recent call last):
- File "<input>", line 1, in <module>
- File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.3helperspydev_pydev_bundlepydev_umd.py", line 197, in runfile
- pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
- File "C:/Users/gdog1/Desktop/proj/main.py", line 20, in <module>
- parser.parse(tokens).eval()
- File "C:Python27libsite-packagesrplyparser.py", line 60, in parse
- self.error_handler(lookahead)
- File "C:Usersgdog1Desktopprojintparser.py", line 39, in error_handler
- raise ValueError("Ran into a %s where it wasn't expected" %
- token.gettokentype())
- ValueError: Ran into a SAY where it wasn't expected
Add Comment
Please, Sign In to add comment