-2

I am implementing a proof of concept application for source-to-source transformation and need a C-parser with an API for manipulating/traversing the C-syntax tree (AST).

I have tried to use clang but I ran into various problems, like not being able to compile the tutorials using libclang, wrong architecture etc. Since this is a proof of concept application, I will defer clang to a different date.

Question

What are some software/libraries (implemented in any language) which can parse C code and which provide an API so I can build applications on top of them. I looked around, but I could not locate any free parsers.

The platforms I can use are anything on Windows or Mac or Linux, and any parsers written in C/C++/Java/Perl/Python/PHP will work.

Community
  • 1
  • 1
user763410
  • 502
  • 6
  • 22
  • Its a little unclear if you want a parser (for some unspecified language) which is implemented in "C/C++" with a documented AST API, or you want a parser (in any language) which can process "C/C++" (the latter being a nonsequiter, because C and C++ are really very different languages). Can you clarify? – Ira Baxter Jul 01 '13 at 18:46
  • See this link http://stackoverflow.com/a/17393852/120163 for a C++ parser that provides ASTs as XML. That same parser also has a complete API to manipulating trees. – Ira Baxter Jul 01 '13 at 18:57
  • (Previous comment written when the question asked about both C and C++) The link shows a C++ parser, but there is a corresponding C parser. The underlying engine has not only an interface for directly manipulation of AST nodes, but also has built-in support for source-to-source transformation using rewriting rules written using the target language (in this case, C) surface syntax. – Ira Baxter Jul 01 '13 at 22:20

1 Answers1

1

You could try one of the available grammars for ANTLR. ANTLR has support for creating tree walkers and you can walk/manipulate the AST manually if necessary. ANTLR V3 has several grammars available including a C preprocessor, ANSI C and GNU C.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • It seems like a good tool. I look for more before I use this as I am a little inexperienced in Java. – user763410 Jul 01 '13 at 19:28
  • ANTLR can generate a parser in several languages although it is sometimes dependent on the grammar since they can contain support code. The ANSI C grammar I linked to contains very little Java code and can easily be ported to generate a lexer and parser in C or even C# if you so choose. – Captain Obvlious Jul 01 '13 at 19:35