Closed dargueta closed 3 years ago
Hi, cparser is currently not able to parse C code with preprocessing directives. In order to mitigate this, you must call preprocessor and then parse preprocessed C code.
However, rule line_directive
was indeed buggy, and I fixed that on fix_preprocessing branch. Here's an example how you can parse C code with preprocessor directives:
from cparser import CParser
from cparser.parser import preprocess_file
import tempfile
code = """
#include <stdio.h>
#ifndef CUSTOM_PI
#define CUSTOM_PI 3.14159265358979323846
#endif
int main() {
float a = CUSTOM_PI;
return 0;
}
"""
cparser = CParser()
with tempfile.NamedTemporaryFile("w+", suffix=".c") as f:
f.write(code)
f.flush()
pp_code = preprocess_file(f.name, cpp_path="cpp")
ast = cparser.parse(pp_code)
Another example can be found examples.
Something appears to be wrong with the grammar, because directives cause syntax errors. To reproduce:
I also tried it with
#define
,#endif
,#undef
and it failed with the same kind of error. The problem seems to be here in the grammar:Environment: