cscg24-lolpython

CSCG 2024 Challenge 'Can I Haz Lolpython?'
git clone https://git.sinitax.com/sinitax/cscg24-lolpython
Log | Files | Refs | sfeed.txt

yacc_notfunc.py (1471B)


      1# -----------------------------------------------------------------------------
      2# yacc_notfunc.py
      3#
      4# p_rule not defined as a function
      5# -----------------------------------------------------------------------------
      6import sys
      7sys.tracebacklimit = 0
      8
      9sys.path.insert(0,"..")
     10import ply.yacc as yacc
     11
     12from calclex import tokens
     13
     14# Parsing rules
     15precedence = (
     16    ('left','PLUS','MINUS'),
     17    ('left','TIMES','DIVIDE'),
     18    ('right','UMINUS'),
     19    )
     20
     21# dictionary of names
     22names = { }
     23
     24p_statement_assign = "Blah"
     25
     26def p_statement_expr(t):
     27    'statement : expression'
     28    print t[1]
     29
     30def p_expression_binop(t):
     31    '''expression : expression PLUS expression
     32                  | expression MINUS expression
     33                  | expression TIMES expression
     34                  | expression DIVIDE expression'''
     35    if t[2] == '+'  : t[0] = t[1] + t[3]
     36    elif t[2] == '-': t[0] = t[1] - t[3]
     37    elif t[2] == '*': t[0] = t[1] * t[3]
     38    elif t[3] == '/': t[0] = t[1] / t[3]
     39
     40def p_expression_uminus(t):
     41    'expression : MINUS expression %prec UMINUS'
     42    t[0] = -t[2]
     43
     44def p_expression_group(t):
     45    'expression : LPAREN expression RPAREN'
     46    t[0] = t[2]
     47
     48def p_expression_number(t):
     49    'expression : NUMBER'
     50    t[0] = t[1]
     51
     52def p_expression_name(t):
     53    'expression : NAME'
     54    try:
     55        t[0] = names[t[1]]
     56    except LookupError:
     57        print "Undefined name '%s'" % t[1]
     58        t[0] = 0
     59
     60def p_error(t):
     61    print "Syntax error at '%s'" % t.value
     62
     63yacc.yacc()
     64
     65
     66
     67