Search code examples
parsingply

How to check that input string could not be reduced to start symbol in PLY?


I'm using PLY to create a parser for a programming language. The problem is that the parser returns None even if it has not reduced the input string to the starting symbol.

Short Example to demonstrate the problem

Starting symbol: program

Input: {+

def p_program(p):
    'program : LBRACE PLUS RBRACE'
    pass

Here, the parser should return some kind of error that EOF was reached and the string could not be reduced. Instead, it just sends None to p_error() which is standard for signalling EOF.

How do I get to know that the stack could not be reduced and EOF was reached?

Additional details from parser.out

state 1

    (0) S' -> program .



state 2

    (1) program -> LBRACE . PLUS RBRACE

    PLUS            shift and go to state 3


state 3

    (1) program -> LBRACE PLUS . RBRACE

    RBRACE          shift and go to state 4


state 4

    (1) program -> LBRACE PLUS RBRACE .

    $end            reduce using rule 1 (program -> LBRACE PLUS RBRACE .)

Solution

  • If p_error is called with None, then you know that the input could not be reduced to the start symbol because a premature EOF was found.

    p_error is only called if there is an error; with a correctly written grammar, it will not be called if the parse succeeded by reducing to the start symbol.