規則の途中のアクション
自分で書き下す必要があるようだ
ほぼ、まんま
#!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', 'D', ) t_A = r'A' t_B = r'B' t_C = r'C' t_D = r'D' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) lex.lex() import ply.yacc as yacc def p_foo(p): 'foo : A seen_A B C D' print "Parsed a foo", p[1],p[3],p[4],p[5] print "seen_A returned", p[2] def p_seen_A(p): "seen_A :" print "Saw an A = ", p[-1] # Access grammar symbol to left p[0] = 'some_value' # Assign value to seen_A def p_error(p): print "Syntax error at '%s'" % p.value yacc.yacc() while 1: try: s = raw_input('input > ') except EOFError: break if not s: continue yacc.parse(s)
で、
input > A B C D Saw an A = A Parsed a foo A B C D seen_A returned some_value