EOF error in parser YACC
Asked Answered
C

1

8

I am trying to parse a string using the yacc parser provided in the PLY library for Python. The parser itself is very long, but the problem that i am having is that it always gives me the same error, no matter what kind of string i put. The error is this:

yacc: Parse error in input. EOF

And the lexer is running perfectly, so i think the parser is the problem. But i do not understand this error, so i do not even know where to look first to solve this problem

Any ideas? Thank you very much!

Clipper answered 22/11, 2011 at 0:53 Comment(2)
How is the parser getting its next token? Does that mechanism work if you call it "by hand"?Cruiser
From within the python interactive REPL, import whichever packages you need to import, and then call yy_scan_string("an input string") (or whatever it is called in your Python version of it).Cruiser
E
8

All parsers specified in PLY are expected to have a single top-level rule that gets reduced as a result of parsing the entire input text. For example, if parsing a program, the top level rule might be something like this:

def p_program(p):
    '''
    program : declarations
    '''

def p_declarations(p):
    '''
    declarations : declarations declaration
                 | declaration
    '''
...

If you get a "EOF" error in the parser, it means that it reached the end of the input without reducing the top-level grammar rule. That is, the parsing stack is non-empty and there are no more rules that can be reduced. Since the stack is non-empty, the parser will try to shift more symbols and fail due to an EOF.

One potential cause of this error is to have an improperly specified starting rule in your grammar. Make sure the first p_rule(p) function in the file is the start rule.

Epidote answered 28/11, 2011 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.