cscg24-lolpython

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

yacc_nodoc.py (1501B)


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