16498Snate@binkert.org# -----------------------------------------------------------------------------
26498Snate@binkert.org# yacc_literal.py
36498Snate@binkert.org#
46498Snate@binkert.org# Grammar with bad literal characters
56498Snate@binkert.org# -----------------------------------------------------------------------------
66498Snate@binkert.orgimport sys
76498Snate@binkert.org
86498Snate@binkert.orgif ".." not in sys.path: sys.path.insert(0,"..")
96498Snate@binkert.orgimport ply.yacc as yacc
106498Snate@binkert.org
116498Snate@binkert.orgfrom calclex import tokens
126498Snate@binkert.org
136498Snate@binkert.org# Parsing rules
146498Snate@binkert.orgprecedence = (
156498Snate@binkert.org    ('left','+','-'),
166498Snate@binkert.org    ('left','*','/'),
176498Snate@binkert.org    ('right','UMINUS'),
186498Snate@binkert.org    )
196498Snate@binkert.org
206498Snate@binkert.org# dictionary of names
216498Snate@binkert.orgnames = { }
226498Snate@binkert.org
236498Snate@binkert.orgdef p_statement_assign(t):
246498Snate@binkert.org    'statement : NAME EQUALS expression'
256498Snate@binkert.org    names[t[1]] = t[3]
266498Snate@binkert.org
276498Snate@binkert.orgdef p_statement_expr(t):
286498Snate@binkert.org    'statement : expression'
296498Snate@binkert.org    print(t[1])
306498Snate@binkert.org
316498Snate@binkert.orgdef p_expression_binop(t):
326498Snate@binkert.org    '''expression : expression '+' expression
336498Snate@binkert.org                  | expression '-' expression
346498Snate@binkert.org                  | expression '*' expression
356498Snate@binkert.org                  | expression '/' expression
366498Snate@binkert.org                  | expression '**' expression '''
376498Snate@binkert.org    if t[2] == '+'  : t[0] = t[1] + t[3]
386498Snate@binkert.org    elif t[2] == '-': t[0] = t[1] - t[3]
396498Snate@binkert.org    elif t[2] == '*': t[0] = t[1] * t[3]
406498Snate@binkert.org    elif t[2] == '/': t[0] = t[1] / t[3]
416498Snate@binkert.org
426498Snate@binkert.orgdef p_expression_uminus(t):
436498Snate@binkert.org    'expression : MINUS expression %prec UMINUS'
446498Snate@binkert.org    t[0] = -t[2]
456498Snate@binkert.org
466498Snate@binkert.orgdef p_expression_group(t):
476498Snate@binkert.org    'expression : LPAREN expression RPAREN'
486498Snate@binkert.org    t[0] = t[2]
496498Snate@binkert.org
506498Snate@binkert.orgdef p_expression_number(t):
516498Snate@binkert.org    'expression : NUMBER'
526498Snate@binkert.org    t[0] = t[1]
536498Snate@binkert.org
546498Snate@binkert.orgdef p_expression_name(t):
556498Snate@binkert.org    'expression : NAME'
566498Snate@binkert.org    try:
576498Snate@binkert.org        t[0] = names[t[1]]
586498Snate@binkert.org    except LookupError:
596498Snate@binkert.org        print("Undefined name '%s'" % t[1])
606498Snate@binkert.org        t[0] = 0
616498Snate@binkert.org
626498Snate@binkert.orgdef p_error(t):
636498Snate@binkert.org    print("Syntax error at '%s'" % t.value)
646498Snate@binkert.org
656498Snate@binkert.orgyacc.yacc()
666498Snate@binkert.org
676498Snate@binkert.org
686498Snate@binkert.org
696498Snate@binkert.org
70