エラーまわり

良く分かっていない

#!/usr/bin/env python

import ply.lex as lex

tokens = (
  'A',
  'B',
)

t_A = r'A'
t_B = r'B'
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'
    print "*a*"

def p_error(p):
    print "Syntax error at '%s'" % p.value
    while 1:
        tok = yacc.token()             # Get the next token
        print tok
        if not tok or tok.type == 'A': break
    yacc.restart()

yacc.yacc()


while 1:
    try:
        s = raw_input('input > ')
    except EOFError:
        break
    if not s: continue
    yacc.parse(s)

で、

input > A
*a*
input > B
Syntax error at 'B'
None
input > A B
Syntax error at 'B'
None
*a*
Traceback (most recent call last):
  File "./20080415_ply00.py", line 44, in ?
    yacc.parse(s)
  File "/usr/lib/python2.3/site-packages/ply/yacc.py", line 314, in parse
    state = goto[statestack[-1]][pname]
IndexError: list index out of range
input > A B A
Syntax error at 'B'
LexToken(A,'A',1,4)
*a*
Traceback (most recent call last):
  File "./20080415_ply00.py", line 44, in ?
    yacc.parse(s)
  File "/usr/lib/python2.3/site-packages/ply/yacc.py", line 314, in parse
    state = goto[statestack[-1]][pname]
IndexError: list index out of range


yacc.errok()

#!/usr/bin/env python

import ply.lex as lex

tokens = (
  'A',
  'B',
)

t_A = r'A'
t_B = r'B'
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'
    print "*a*"

def p_error(p):
    print "Syntax error at '%s'" % p.value
    yacc.errok()

yacc.yacc()


while 1:
    try:
        s = raw_input('input > ')
    except EOFError:
        break
    if not s: continue
    yacc.parse(s)

で、

input > A B
Syntax error at 'B'
*a*
input > A B B A
Syntax error at 'B'
Syntax error at 'B'
Syntax error at 'A'
*a*
input > A
*a*
input > B
Syntax error at 'B'
Traceback (most recent call last):
  File "./20080415_ply04.py", line 40, in ?
    yacc.parse(s)
  File "/usr/lib/python2.3/site-packages/ply/yacc.py", line 346, in parse
    tok = self.errorfunc(errtoken)
  File "./20080415_ply04.py", line 28, in p_error
    print "Syntax error at '%s'" % p.value
AttributeError: 'NoneType' object has no attribute 'value'