Path: blob/main/third_party/ply/example/classcalc/calc.py
7087 views
#!/usr/bin/env python12# -----------------------------------------------------------------------------3# calc.py4#5# A simple calculator with variables. This is from O'Reilly's6# "Lex and Yacc", p. 63.7#8# Class-based example contributed to PLY by David McNab9# -----------------------------------------------------------------------------1011import sys12sys.path.insert(0,"../..")1314if sys.version_info[0] >= 3:15raw_input = input1617import ply.lex as lex18import ply.yacc as yacc19import os2021class Parser:22"""23Base class for a lexer/parser that has the rules defined as methods24"""25tokens = ()26precedence = ()2728def __init__(self, **kw):29self.debug = kw.get('debug', 0)30self.names = { }31try:32modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__33except:34modname = "parser"+"_"+self.__class__.__name__35self.debugfile = modname + ".dbg"36self.tabmodule = modname + "_" + "parsetab"37#print self.debugfile, self.tabmodule3839# Build the lexer and parser40lex.lex(module=self, debug=self.debug)41yacc.yacc(module=self,42debug=self.debug,43debugfile=self.debugfile,44tabmodule=self.tabmodule)4546def run(self):47while 1:48try:49s = raw_input('calc > ')50except EOFError:51break52if not s: continue53yacc.parse(s)545556class Calc(Parser):5758tokens = (59'NAME','NUMBER',60'PLUS','MINUS','EXP', 'TIMES','DIVIDE','EQUALS',61'LPAREN','RPAREN',62)6364# Tokens6566t_PLUS = r'\+'67t_MINUS = r'-'68t_EXP = r'\*\*'69t_TIMES = r'\*'70t_DIVIDE = r'/'71t_EQUALS = r'='72t_LPAREN = r'\('73t_RPAREN = r'\)'74t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'7576def t_NUMBER(self, t):77r'\d+'78try:79t.value = int(t.value)80except ValueError:81print("Integer value too large %s" % t.value)82t.value = 083#print "parsed number %s" % repr(t.value)84return t8586t_ignore = " \t"8788def t_newline(self, t):89r'\n+'90t.lexer.lineno += t.value.count("\n")9192def t_error(self, t):93print("Illegal character '%s'" % t.value[0])94t.lexer.skip(1)9596# Parsing rules9798precedence = (99('left','PLUS','MINUS'),100('left','TIMES','DIVIDE'),101('left', 'EXP'),102('right','UMINUS'),103)104105def p_statement_assign(self, p):106'statement : NAME EQUALS expression'107self.names[p[1]] = p[3]108109def p_statement_expr(self, p):110'statement : expression'111print(p[1])112113def p_expression_binop(self, p):114"""115expression : expression PLUS expression116| expression MINUS expression117| expression TIMES expression118| expression DIVIDE expression119| expression EXP expression120"""121#print [repr(p[i]) for i in range(0,4)]122if p[2] == '+' : p[0] = p[1] + p[3]123elif p[2] == '-': p[0] = p[1] - p[3]124elif p[2] == '*': p[0] = p[1] * p[3]125elif p[2] == '/': p[0] = p[1] / p[3]126elif p[2] == '**': p[0] = p[1] ** p[3]127128def p_expression_uminus(self, p):129'expression : MINUS expression %prec UMINUS'130p[0] = -p[2]131132def p_expression_group(self, p):133'expression : LPAREN expression RPAREN'134p[0] = p[2]135136def p_expression_number(self, p):137'expression : NUMBER'138p[0] = p[1]139140def p_expression_name(self, p):141'expression : NAME'142try:143p[0] = self.names[p[1]]144except LookupError:145print("Undefined name '%s'" % p[1])146p[0] = 0147148def p_error(self, p):149if p:150print("Syntax error at '%s'" % p.value)151else:152print("Syntax error at EOF")153154if __name__ == '__main__':155calc = Calc()156calc.run()157158159