yacc_unused.py revision 6498:e21e9ab5fad0
112863Sgabeblack@google.com# -----------------------------------------------------------------------------
212863Sgabeblack@google.com# yacc_unused.py
312863Sgabeblack@google.com#
412863Sgabeblack@google.com# A grammar with an unused rule
512863Sgabeblack@google.com# -----------------------------------------------------------------------------
612863Sgabeblack@google.comimport sys
712863Sgabeblack@google.com
812863Sgabeblack@google.comif ".." not in sys.path: sys.path.insert(0,"..")
912863Sgabeblack@google.comimport ply.yacc as yacc
1012863Sgabeblack@google.com
1112863Sgabeblack@google.comfrom calclex import tokens
1212863Sgabeblack@google.com
1312863Sgabeblack@google.com# Parsing rules
1412863Sgabeblack@google.comprecedence = (
1512863Sgabeblack@google.com    ('left','PLUS','MINUS'),
1612863Sgabeblack@google.com    ('left','TIMES','DIVIDE'),
1712863Sgabeblack@google.com    ('right','UMINUS'),
1812863Sgabeblack@google.com    )
1912863Sgabeblack@google.com
2012863Sgabeblack@google.com# dictionary of names
2112863Sgabeblack@google.comnames = { }
2212863Sgabeblack@google.com
2312863Sgabeblack@google.comdef p_statement_assign(t):
2412863Sgabeblack@google.com    'statement : NAME EQUALS expression'
2512863Sgabeblack@google.com    names[t[1]] = t[3]
2612863Sgabeblack@google.com
2712863Sgabeblack@google.comdef p_statement_expr(t):
2812863Sgabeblack@google.com    'statement : expression'
2912863Sgabeblack@google.com    print(t[1])
3012863Sgabeblack@google.com
3112863Sgabeblack@google.comdef p_expression_binop(t):
3212863Sgabeblack@google.com    '''expression : expression PLUS expression
3312950Sgabeblack@google.com                  | expression MINUS expression
3413046Sgabeblack@google.com                  | expression TIMES expression
3513035Sgabeblack@google.com                  | expression DIVIDE expression'''
3613035Sgabeblack@google.com    if t[2] == '+'  : t[0] = t[1] + t[3]
3713035Sgabeblack@google.com    elif t[2] == '-': t[0] = t[1] - t[3]
3813053Sgabeblack@google.com    elif t[2] == '*': t[0] = t[1] * t[3]
3912950Sgabeblack@google.com    elif t[2] == '/': t[0] = t[1] / t[3]
4012950Sgabeblack@google.com
4112950Sgabeblack@google.comdef p_expression_uminus(t):
4212950Sgabeblack@google.com    'expression : MINUS expression %prec UMINUS'
4313053Sgabeblack@google.com    t[0] = -t[2]
4413053Sgabeblack@google.com
4513053Sgabeblack@google.comdef p_expression_group(t):
4613053Sgabeblack@google.com    'expression : LPAREN expression RPAREN'
4713059Sgabeblack@google.com    t[0] = t[2]
4813053Sgabeblack@google.com
4913053Sgabeblack@google.comdef p_expression_number(t):
5013053Sgabeblack@google.com    'expression : NUMBER'
5112950Sgabeblack@google.com    t[0] = t[1]
5212863Sgabeblack@google.com
5312863Sgabeblack@google.comdef p_expression_name(t):
5413035Sgabeblack@google.com    'expression : NAME'
5513035Sgabeblack@google.com    try:
5613035Sgabeblack@google.com        t[0] = names[t[1]]
5713035Sgabeblack@google.com    except LookupError:
5813035Sgabeblack@google.com        print("Undefined name '%s'" % t[1])
5913035Sgabeblack@google.com        t[0] = 0
6013035Sgabeblack@google.com
6113035Sgabeblack@google.comdef p_expr_list(t):
6213035Sgabeblack@google.com    'exprlist : exprlist COMMA expression'
6313035Sgabeblack@google.com    pass
6413035Sgabeblack@google.com
6513035Sgabeblack@google.comdef p_expr_list_2(t):
6613035Sgabeblack@google.com    'exprlist : expression'
6713035Sgabeblack@google.com    pass
6813035Sgabeblack@google.com
6913035Sgabeblack@google.com
7013035Sgabeblack@google.comdef p_error(t):
7112863Sgabeblack@google.com    print("Syntax error at '%s'" % t.value)
7212863Sgabeblack@google.com
7312863Sgabeblack@google.comyacc.yacc()
7412863Sgabeblack@google.com
7512950Sgabeblack@google.com
7612950Sgabeblack@google.com
7712863Sgabeblack@google.com
7813035Sgabeblack@google.com