I’m writing a C parser using PLY, and recently ran into a problem. This code:
typedef int my_type; my_type x;
Is correct C code, because my_type is defined as a type previously to being used as such. I handle it by filling a type symbol table in the parser that gets used by the lexer to differentiate between types and simple identifiers.
However, while the type declaration rule ends with SEMI (the ‘;’ token), PLY shifts the token my_type from the second line before deciding it’s done with the first one. Because of this, I have no chance to pass the update in the type symbol table to the lexer and it sees my_type as an identifier and not a type.
Any ideas for a fix ?
The full code is at: http://code.google.com/p/pycparser/source/browse/trunk/src/c_parser.py Not sure how I can create a smaller example out of this.
Edit:
Problem solved. See my solution below.
With some help from Dave Beazley (PLY’s creator), my problem was solved.
The idea is to use special sub-rules and do the actions in them. In my case, I split the
declarationrule to:decl_bodyis always reduced before the token after SEMI is shifted in, so my action gets executed at the correct time.