I have multiple C source files and respective header files. I am trying to parse these files using a compiler, eg ANTLR. In ANTLR parser grammar, you can define your header files using the
@parser::includes
{#include"a.h"}
You can start parsing the first file eg
CommonTree tree = Parser.start("a.c");
and parser will parse the header file
a.h
but how to parse the files if you have multiple source file egbc, cc and so on with their respective header files.
C is a pig to parse --- the semantic type of a token depends on what it's been declared as. Consider:
T(*b)[4]
If T is a type name, then this is a variable declaration. If it's an identifier, it's a functional call. In order to resolve this, any C parser that expects to actually work is going to have to keep a full type environment, which means it needs to be an unpleasantly large chunk of a C compiler.
There are ANTLR parsers for C that get all this stuff right but they're not trivial to use and I don't have any experience of them, so can't comment there.
Instead you might want to go look at using external tools to parse your C into something that's easier to deal with. gcc-xml is one such; it uses gcc itself to parse source files and then spit out XML that's much easier to handle.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.