p.lineno(num), p.lexpos(num), p.linespan(num), p.lexspan(num)
#!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' 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_input(p): 'input : A B C' print p.lineno(1), p.lexpos(1), p.lineno(2), p.lexpos(2) print p.linespan(1), p.lexspan(1), p.linespan(2), p.lexspan(2) 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 1 0 1 2 (1, 1) (0, 0) (1, 1) (2, 2) input > A B C 1 0 1 2 (1, 1) (0, 0) (1, 1) (2, 2)