Suppose you wanted to make a grammar for simple arithmetic expressions. Here is how you would do it with yacc.py:
In this example, each grammar rule is defined by a Python function where the docstring to that function contains the appropriate context-free grammar specification. Each function accepts a single argument p that is a sequence containing the values of each grammar symbol in the corresponding rule. The values of p[i] are mapped to grammar symbols as shown here:# Yacc example import ply.yacc as yacc # Get the token map from the lexer. This is required. from calclex import tokens def p_expression_plus(p): 'expression : expression PLUS term' p[0] = p[1] + p[3] def p_expression_minus(p): 'expression : expression MINUS term' p[0] = p[1] - p[3] def p_expression_term(p): 'expression : term' p[0] = p[1] def p_term_times(p): 'term : term TIMES factor' p[0] = p[1] * p[3] def p_term_div(p): 'term : term DIVIDE factor' p[0] = p[1] / p[3] def p_term_factor(p): 'term : factor' p[0] = p[1] def p_factor_num(p): 'factor : NUMBER' p[0] = p[1] def p_factor_expr(p): 'factor : LPAREN expression RPAREN' p[0] = p[2] # Error rule for syntax errors def p_error(p): print "Syntax error in input!" # Build the parser yacc.yacc() # Use this if you want to build the parser using SLR instead of LALR # yacc.yacc(method="SLR") while 1: try: s = raw_input('calc > ') except EOFError: break if not s: continue result = yacc.parse(s) print result
def p_expression_plus(p): 'expression : expression PLUS term' # ^ ^ ^ ^ # p[0] p[1] p[2] p[3] p[0] = p[1] + p[3]
No comments:
Post a Comment