I'm looking into parsing a C-file with pycparser and I'm trying to get the source line number from the AST generated by pycparser. Is this possible?
Asked
Active
Viewed 877 times
2 Answers
1
It is possible with the "coord" Object. Have a look at the coord class in the plyparser.py
https://bitbucket.org/eliben/pycparser/src/b169b693a194/pycparser/plyparser.py?at=default

Adrian
- 21
- 5
0
I couldn't exactly figure out how to use the Coord class, but having read a bit at that part of the code, it turns out, that there is a .show()
method for AST nodes, which accepts a showcoord
boolean argument; so you can write in your Python code:
ast_node.show(showcoord=True)
... and this will print out the structure of the node, annotated with filename and line number and column number - something like:
Decl: my_array, ['const'], [], ['extern'], [] (at my_header.h:41:20)
ArrayDecl: [] (at my_header.h:41:20)
PtrDecl: [] (at my_header.h:41:18)
TypeDecl: my_array, ['const'], None (at my_header.h:41:20)
IdentifierType: ['void'] (at my_header.h:41:14)
...

sdbbs
- 4,270
- 5
- 32
- 87