eliben/pycparser — pull request #1 by Syeberman (2013-06-10 17:45:07+00:00)
Title: Redeclared types

While using pycparser to parse a large, existing codebase, I immediately came upon the typedef-name problem. The changes in this pull request resolve and test for the issues encountered; in particular:

- Reusing typedef names as structure/union member names
- Reusing typedef names as variables names in inner scopes
- Reusing typedef names as parameter names in declarations and definitions
- Duplicated typedef declarations (non-standard, but apparently common and syntactically
similar to the above)

There is a corner case regarding parameter name scoping that required access to yacc.py's lookahead token (see p_direct_declarator_5 in c_parser.py for details). There are three solutions as I see it:

- Modify yacc.py to expose the lookahead token as an attribute of the parser (...but requiring future PLY updates to be merged, not copied)
- Keep track of the most-recent token via a custom tokenfunc (...but trusting that the parser's lookaheadstack is empty)
- Use the inspect module to grab the value of the parser's lookahead variable (...but direct
inspection of Python frames and local dictionaries is pretty evil)

For the purpose of this change I decided to use the inspect module, as it is the least error-prone (it can inspect lookaheadstack to ensure it's empty) and the least invasive to the codebase. I can instead make lookahead/lookaheadstack attributes, if you're willing to support the merging of PLY.