Steve Reinhardt | ba2eae5 | 2006-05-22 14:29:33 -0400 | [diff] [blame] | 1 | # ----------------------------------------------------------------------------- |
| 2 | # yacc_rr.py |
| 3 | # |
| 4 | # A grammar with a reduce/reduce conflict |
| 5 | # ----------------------------------------------------------------------------- |
| 6 | import sys |
Steve Reinhardt | ba2eae5 | 2006-05-22 14:29:33 -0400 | [diff] [blame] | 7 | |
Nathan Binkert | e1270f8 | 2009-08-16 13:39:58 -0700 | [diff] [blame] | 8 | if ".." not in sys.path: sys.path.insert(0,"..") |
Nathan Binkert | 44ebb8d | 2007-05-24 21:54:51 -0700 | [diff] [blame] | 9 | import ply.yacc as yacc |
| 10 | |
Steve Reinhardt | ba2eae5 | 2006-05-22 14:29:33 -0400 | [diff] [blame] | 11 | from calclex import tokens |
| 12 | |
| 13 | # Parsing rules |
| 14 | precedence = ( |
| 15 | ('left','PLUS','MINUS'), |
| 16 | ('left','TIMES','DIVIDE'), |
| 17 | ('right','UMINUS'), |
| 18 | ) |
| 19 | |
| 20 | # dictionary of names |
| 21 | names = { } |
| 22 | |
| 23 | def p_statement_assign(t): |
| 24 | 'statement : NAME EQUALS expression' |
| 25 | names[t[1]] = t[3] |
| 26 | |
| 27 | def p_statement_assign_2(t): |
| 28 | 'statement : NAME EQUALS NUMBER' |
| 29 | names[t[1]] = t[3] |
| 30 | |
| 31 | def p_statement_expr(t): |
| 32 | 'statement : expression' |
Nathan Binkert | e1270f8 | 2009-08-16 13:39:58 -0700 | [diff] [blame] | 33 | print(t[1]) |
Steve Reinhardt | ba2eae5 | 2006-05-22 14:29:33 -0400 | [diff] [blame] | 34 | |
| 35 | def p_expression_binop(t): |
| 36 | '''expression : expression PLUS expression |
| 37 | | expression MINUS expression |
| 38 | | expression TIMES expression |
| 39 | | expression DIVIDE expression''' |
| 40 | if t[2] == '+' : t[0] = t[1] + t[3] |
| 41 | elif t[2] == '-': t[0] = t[1] - t[3] |
| 42 | elif t[2] == '*': t[0] = t[1] * t[3] |
Nathan Binkert | e1270f8 | 2009-08-16 13:39:58 -0700 | [diff] [blame] | 43 | elif t[2] == '/': t[0] = t[1] / t[3] |
Steve Reinhardt | ba2eae5 | 2006-05-22 14:29:33 -0400 | [diff] [blame] | 44 | |
| 45 | def p_expression_uminus(t): |
| 46 | 'expression : MINUS expression %prec UMINUS' |
| 47 | t[0] = -t[2] |
| 48 | |
| 49 | def p_expression_group(t): |
| 50 | 'expression : LPAREN expression RPAREN' |
| 51 | t[0] = t[2] |
| 52 | |
| 53 | def p_expression_number(t): |
| 54 | 'expression : NUMBER' |
| 55 | t[0] = t[1] |
| 56 | |
| 57 | def p_expression_name(t): |
| 58 | 'expression : NAME' |
| 59 | try: |
| 60 | t[0] = names[t[1]] |
| 61 | except LookupError: |
Nathan Binkert | e1270f8 | 2009-08-16 13:39:58 -0700 | [diff] [blame] | 62 | print("Undefined name '%s'" % t[1]) |
Steve Reinhardt | ba2eae5 | 2006-05-22 14:29:33 -0400 | [diff] [blame] | 63 | t[0] = 0 |
| 64 | |
| 65 | def p_error(t): |
Nathan Binkert | e1270f8 | 2009-08-16 13:39:58 -0700 | [diff] [blame] | 66 | print("Syntax error at '%s'" % t.value) |
Steve Reinhardt | ba2eae5 | 2006-05-22 14:29:33 -0400 | [diff] [blame] | 67 | |
Steve Reinhardt | ba2eae5 | 2006-05-22 14:29:33 -0400 | [diff] [blame] | 68 | yacc.yacc() |
| 69 | |
| 70 | |
| 71 | |
| 72 | |