pycparser icon indicating copy to clipboard operation
pycparser copied to clipboard

Unable to parse labels at end of block

Open LuciaMartinezGavier opened this issue 1 year ago • 1 comments

Raises pycparser.plyparser.ParseError when a label is at the end of a block. For example, the following code fails:

from pycparser import CParser

ast =  CParser().parse(
    """
    int main(){
        label:
    }
    """
)

As this is valid C, I think it shouldn't throw that exception.

Adding a ; (or any other code line) after the label fixes this issue

from pycparser import CParser

ast =  CParser().parse(
    """
    int main(){
        label:;
    }
    """
)

LuciaMartinezGavier avatar Mar 13 '24 20:03 LuciaMartinezGavier

Thanks for the report.

Strictly reading the standard, a labeled-statement requires a statement following the label:, e.g. see section A.2.3 in https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

gcc also issues a warning here in "strict" mode.

I don't object to adding the ability to support this in pycparser, if a PR can be made that doesn't complicate the parser too much.

eliben avatar Mar 14 '24 13:03 eliben