I am trying to parse a file using Ply. I need to be able to recognize the expression format "a = 1;" (name equals number semicolon). Everything works fine as long as there is only one of the expressions per line in the input (a = 1;), but yacc gives an error when there are multiple expressions per line (a = 1; b = 2;). I have confirmed that everything is getting tokenized correctly so I am not sure what the issue is.
Here is a minimal version of my code:
tokens = (
'EQUALS',
'SEMICOLON',
'NAME',
'VALUE',
)
t_ignore = ' \t'
def t_EQUALS(t):
r"""="""
return t
def t_SEMICOLON(t):
r""";"""
return t
def t_NAME(t):
r"""\S+\s*(?==)"""
t.value = t.value.strip()
return t
def t_VALUE(t):
r"""(?<==)[^;]+"""
t.value = t.value.strip()
return t
def t_newline(t):
r"""\n"""
t.lexer.lineno += len(t.value)
def t_error(t):
print("Illegal character '%s' on line %d" % (t.value[0], t.lineno))
t.lexer.skip(1)
def t_eof(t):
return None
lexer = lex.lex()
def p_expression(p):
'''
expression : item
| empty
'''
p[0] = p[1]
def p_item(p):
'''
item : NAME EQUALS VALUE SEMICOLON
'''
p[0] = ('name', p[1], p[3])
def p_error(p):
print('Syntax error' + str(p) + " Line " + str(p.lineno))
parser = yacc.yacc()
for line in file:
print(parser.parse(line))