eliben / pycparser

:snake: Complete C99 parser in pure Python
Other
3.21k stars 612 forks source link

CParser doesn't work with comments #526

Closed denismazzucato closed 7 months ago

denismazzucato commented 7 months ago

CParser doesn't seem to work well with comments:

from pycparser.c_parser import CParser

ast = CParser().parse(r'''
int main() {
  // comment
}
''')

raises "ParseError: :3:3: before: /"

In fact, one needs to activate cpp parsing with flag "-E" to obtain the ast of such code, the following works:

from pycparser import parse_file
ast = parse_file('examples/example.c', use_cpp=True, cpp_path='gcc', cpp_args=r'-E')
eliben commented 7 months ago

Please read the README file carefully, especially this section: https://github.com/eliben/pycparser?tab=readme-ov-file#31interaction-with-the-c-preprocessor