yacc_nodoc.py revision 2632
12139SN/A# -----------------------------------------------------------------------------
22139SN/A# yacc_nodoc.py
32139SN/A#
42139SN/A# Rule with a missing doc-string
52139SN/A# -----------------------------------------------------------------------------
62139SN/Aimport sys
72139SN/Asys.tracebacklimit = 0
82139SN/A
92139SN/Afrom calclex import tokens
102139SN/A
112139SN/A# Parsing rules
122139SN/Aprecedence = (
132139SN/A    ('left','PLUS','MINUS'),
142139SN/A    ('left','TIMES','DIVIDE'),
152139SN/A    ('right','UMINUS'),
162139SN/A    )
172139SN/A
182139SN/A# dictionary of names
192139SN/Anames = { }
202139SN/A
212139SN/Adef p_statement_assign(t):
222139SN/A    'statement : NAME EQUALS expression'
232139SN/A    names[t[1]] = t[3]
242139SN/A
252139SN/Adef p_statement_expr(t):
262139SN/A    print t[1]
272139SN/A
282665Ssaidi@eecs.umich.edudef p_expression_binop(t):
292665Ssaidi@eecs.umich.edu    '''expression : expression PLUS expression
302139SN/A                  | expression MINUS expression
314202Sbinkertn@umich.edu                  | expression TIMES expression
328961Sgblack@eecs.umich.edu                  | expression DIVIDE expression'''
3310196SCurtis.Dunham@arm.com    if t[2] == '+'  : t[0] = t[1] + t[3]
342139SN/A    elif t[2] == '-': t[0] = t[1] - t[3]
354202Sbinkertn@umich.edu    elif t[2] == '*': t[0] = t[1] * t[3]
362152SN/A    elif t[3] == '/': t[0] = t[1] / t[3]
372152SN/A
382139SN/Adef p_expression_uminus(t):
392139SN/A    'expression : MINUS expression %prec UMINUS'
402139SN/A    t[0] = -t[2]
412139SN/A
422139SN/Adef p_expression_group(t):
432152SN/A    'expression : LPAREN expression RPAREN'
442152SN/A    t[0] = t[2]
452139SN/A
462139SN/Adef p_expression_number(t):
472139SN/A    'expression : NUMBER'
489020Sgblack@eecs.umich.edu    t[0] = t[1]
494781Snate@binkert.org
507799Sgblack@eecs.umich.edudef p_expression_name(t):
514781Snate@binkert.org    'expression : NAME'
524781Snate@binkert.org    try:
533170Sstever@eecs.umich.edu        t[0] = names[t[1]]
545664Sgblack@eecs.umich.edu    except LookupError:
558105Sgblack@eecs.umich.edu        print "Undefined name '%s'" % t[1]
566179Sksewell@umich.edu        t[0] = 0
574781Snate@binkert.org
586329Sgblack@eecs.umich.edudef p_error(t):
594781Snate@binkert.org    print "Syntax error at '%s'" % t.value
604781Snate@binkert.org
614781Snate@binkert.orgimport yacc
624781Snate@binkert.orgyacc.yacc()
634781Snate@binkert.org
644781Snate@binkert.org
652139SN/A
662139SN/A
673546Sgblack@eecs.umich.edu