问题
I'm looking for an C source code parser that can create a comprehensive AST from it.
Preferably a Java library (I'd rather not have to use Python here -> http://code.google.com/p/pycparser/)
回答1:
It may well be easiest for you to use ANTLR and get it to generate an AST based on an existing ANTLR grammar, e.g. the source.
ANTLR has a Java API here: http://www.antlr.org/api/Java/index.html
回答2:
The Eclipse CDT project has a C parser written in Java, see my answer to a similar question.
回答3:
Our C Front End is not in Java, definitely not in Python :-} but provides robust parsers for many real dialects of C code. It goes beyond building just ASTs; it provides a preprocessor, symbol tables, local and global flow analyses, which you will need if you want to do anything to C other than just "having an AST".
It comes built on top of our DMS Software Reengineering Toolkit, which provides the infrastructure for parsing and flow analysis, can apply transforms to the AST using patterns, and can regenerate valid source code.
EDIT July 2015 (in response to comment): DMS itself is implemented in a parallel programming language, PARLANSE, which is C-like in capability but includes fine-grain parallelism constructs as well as exception handling. DMS provides a set of DSLs for defining language processing: a fully-Unicode capable lexer, BNF for grammars, attribute grammars for computing tree-shaped analyses, and source-to-source transformations useful for pattern recognition and rewriting the source code.
来源:https://stackoverflow.com/questions/9315205/c-source-code-ast-parser-in-java