cscg24-lolpython

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

yacc_badprec.py (1441B)


      1# -----------------------------------------------------------------------------
      2# yacc_badprec.py
      3#
      4# Bad precedence specifier
      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 = "blah"
     16
     17# dictionary of names
     18names = { }
     19
     20def p_statement_assign(t):
     21    'statement : NAME EQUALS expression'
     22    names[t[1]] = t[3]
     23
     24def p_statement_expr(t):
     25    'statement : expression'
     26    print t[1]
     27
     28def p_expression_binop(t):
     29    '''expression : expression PLUS expression
     30                  | expression MINUS expression
     31                  | expression TIMES expression
     32                  | expression DIVIDE expression'''
     33    if t[2] == '+'  : t[0] = t[1] + t[3]
     34    elif t[2] == '-': t[0] = t[1] - t[3]
     35    elif t[2] == '*': t[0] = t[1] * t[3]
     36    elif t[3] == '/': t[0] = t[1] / t[3]
     37
     38def p_expression_uminus(t):
     39    'expression : MINUS expression %prec UMINUS'
     40    t[0] = -t[2]
     41
     42def p_expression_group(t):
     43    'expression : LPAREN expression RPAREN'
     44    t[0] = t[2]
     45
     46def p_expression_number(t):
     47    'expression : NUMBER'
     48    t[0] = t[1]
     49
     50def p_expression_name(t):
     51    'expression : NAME'
     52    try:
     53        t[0] = names[t[1]]
     54    except LookupError:
     55        print "Undefined name '%s'" % t[1]
     56        t[0] = 0
     57
     58def p_error(t):
     59    print "Syntax error at '%s'" % t.value
     60
     61yacc.yacc()
     62
     63
     64
     65