yacc_error4.py revision 6498:e21e9ab5fad0
16657Snate@binkert.org# ----------------------------------------------------------------------------- 26657Snate@binkert.org# yacc_error4.py 36657Snate@binkert.org# 46657Snate@binkert.org# Attempt to define a rule named 'error' 56657Snate@binkert.org# ----------------------------------------------------------------------------- 66657Snate@binkert.orgimport sys 76657Snate@binkert.org 86657Snate@binkert.orgif ".." not in sys.path: sys.path.insert(0,"..") 96657Snate@binkert.orgimport ply.yacc as yacc 106657Snate@binkert.org 116657Snate@binkert.orgfrom calclex import tokens 126657Snate@binkert.org 136657Snate@binkert.org# Parsing rules 146657Snate@binkert.orgprecedence = ( 156657Snate@binkert.org ('left','PLUS','MINUS'), 166657Snate@binkert.org ('left','TIMES','DIVIDE'), 176657Snate@binkert.org ('right','UMINUS'), 186657Snate@binkert.org ) 196657Snate@binkert.org 206657Snate@binkert.org# dictionary of names 216657Snate@binkert.orgnames = { } 226657Snate@binkert.org 236657Snate@binkert.orgdef p_statement_assign(t): 246657Snate@binkert.org 'statement : NAME EQUALS expression' 256657Snate@binkert.org names[t[1]] = t[3] 266657Snate@binkert.org 276657Snate@binkert.orgdef p_statement_expr(t): 288453Snate@binkert.org 'statement : expression' 296657Snate@binkert.org print(t[1]) 306657Snate@binkert.org 316657Snate@binkert.orgdef p_expression_binop(t): 326657Snate@binkert.org '''expression : expression PLUS expression 336657Snate@binkert.org | expression MINUS expression 346657Snate@binkert.org | expression TIMES expression 356657Snate@binkert.org | expression DIVIDE expression''' 366999Snate@binkert.org if t[2] == '+' : t[0] = t[1] + t[3] 376999Snate@binkert.org elif t[2] == '-': t[0] = t[1] - t[3] 386999Snate@binkert.org elif t[2] == '*': t[0] = t[1] * t[3] 396657Snate@binkert.org elif t[2] == '/': t[0] = t[1] / t[3] 406657Snate@binkert.org 416657Snate@binkert.orgdef p_expression_uminus(t): 426657Snate@binkert.org 'expression : MINUS expression %prec UMINUS' 436657Snate@binkert.org t[0] = -t[2] 446657Snate@binkert.org 458453Snate@binkert.orgdef p_expression_group(t): 468453Snate@binkert.org 'expression : LPAREN expression RPAREN' 476657Snate@binkert.org t[0] = t[2] 486657Snate@binkert.org 496657Snate@binkert.orgdef p_expression_number(t): 506657Snate@binkert.org 'expression : NUMBER' 516657Snate@binkert.org t[0] = t[1] 528453Snate@binkert.org 538453Snate@binkert.orgdef p_expression_name(t): 546657Snate@binkert.org 'expression : NAME' 556657Snate@binkert.org try: 566657Snate@binkert.org t[0] = names[t[1]] 576657Snate@binkert.org except LookupError: 586657Snate@binkert.org print("Undefined name '%s'" % t[1]) 596999Snate@binkert.org t[0] = 0 606999Snate@binkert.org 616999Snate@binkert.orgdef p_error_handler(t): 626657Snate@binkert.org 'error : NAME' 636657Snate@binkert.org pass 646657Snate@binkert.org 656657Snate@binkert.orgdef p_error(t): 666657Snate@binkert.org pass 676657Snate@binkert.org 686657Snate@binkert.orgyacc.yacc() 696657Snate@binkert.org 706657Snate@binkert.org 719767Slena@cs.wisc.edu 729767Slena@cs.wisc.edu 7310985SBrad.Beckmann@amd.com