yacc_badprec3.py revision 6498
112855Sgabeblack@google.com# -----------------------------------------------------------------------------
212855Sgabeblack@google.com# yacc_badprec3.py
312855Sgabeblack@google.com#
412855Sgabeblack@google.com# Bad precedence
512855Sgabeblack@google.com# -----------------------------------------------------------------------------
612855Sgabeblack@google.comimport sys
712855Sgabeblack@google.com
812855Sgabeblack@google.comif ".." not in sys.path: sys.path.insert(0,"..")
912855Sgabeblack@google.comimport ply.yacc as yacc
1012855Sgabeblack@google.com
1112855Sgabeblack@google.comfrom calclex import tokens
1212855Sgabeblack@google.com
1312855Sgabeblack@google.com# Parsing rules
1412855Sgabeblack@google.comprecedence = (
1512855Sgabeblack@google.com    ('left','PLUS','MINUS'),
1612855Sgabeblack@google.com    ('left','TIMES','DIVIDE','MINUS'),
1712855Sgabeblack@google.com    ('right','UMINUS'),
1812855Sgabeblack@google.com    )
1912855Sgabeblack@google.com
2012855Sgabeblack@google.com# dictionary of names
2112855Sgabeblack@google.comnames = { }
2212855Sgabeblack@google.com
2312855Sgabeblack@google.comdef p_statement_assign(t):
2412855Sgabeblack@google.com    'statement : NAME EQUALS expression'
2512855Sgabeblack@google.com    names[t[1]] = t[3]
2612855Sgabeblack@google.com
2712855Sgabeblack@google.comdef p_statement_expr(t):
2812855Sgabeblack@google.com    'statement : expression'
2912855Sgabeblack@google.com    print(t[1])
3012855Sgabeblack@google.com
3112855Sgabeblack@google.comdef p_expression_binop(t):
3212855Sgabeblack@google.com    '''expression : expression PLUS expression
3312855Sgabeblack@google.com                  | expression MINUS expression
3412855Sgabeblack@google.com                  | expression TIMES expression
3512855Sgabeblack@google.com                  | expression DIVIDE expression'''
3612855Sgabeblack@google.com    if t[2] == '+'  : t[0] = t[1] + t[3]
3712855Sgabeblack@google.com    elif t[2] == '-': t[0] = t[1] - t[3]
3812855Sgabeblack@google.com    elif t[2] == '*': t[0] = t[1] * t[3]
3912855Sgabeblack@google.com    elif t[3] == '/': t[0] = t[1] / t[3]
4012855Sgabeblack@google.com
4112855Sgabeblack@google.comdef p_expression_uminus(t):
4212855Sgabeblack@google.com    'expression : MINUS expression %prec UMINUS'
4312855Sgabeblack@google.com    t[0] = -t[2]
4412855Sgabeblack@google.com
4512855Sgabeblack@google.comdef p_expression_group(t):
4612855Sgabeblack@google.com    'expression : LPAREN expression RPAREN'
4712855Sgabeblack@google.com    t[0] = t[2]
4812855Sgabeblack@google.com
4912855Sgabeblack@google.comdef p_expression_number(t):
5012855Sgabeblack@google.com    'expression : NUMBER'
5112855Sgabeblack@google.com    t[0] = t[1]
5212855Sgabeblack@google.com
5312855Sgabeblack@google.comdef p_expression_name(t):
5412855Sgabeblack@google.com    'expression : NAME'
5512855Sgabeblack@google.com    try:
5612855Sgabeblack@google.com        t[0] = names[t[1]]
5712855Sgabeblack@google.com    except LookupError:
5812855Sgabeblack@google.com        print("Undefined name '%s'" % t[1])
5912855Sgabeblack@google.com        t[0] = 0
6012855Sgabeblack@google.com
6112855Sgabeblack@google.comdef p_error(t):
6212855Sgabeblack@google.com    print("Syntax error at '%s'" % t.value)
6312855Sgabeblack@google.com
6412855Sgabeblack@google.comyacc.yacc()
6512855Sgabeblack@google.com
6612855Sgabeblack@google.com
6712855Sgabeblack@google.com
6812855Sgabeblack@google.com
6912855Sgabeblack@google.com