Does anyone know if there are any clean, standalone libraries for
parsing, verifying and then generating full ASTs, from C source code?
So far, my research has shown up a few tools like ctool, which
generates annotated parse trees but don't do full type analysis and
program verification; things like elsa, which is really designed for
C++ and as such is grotesquely overcomplicated for what I want (and
also, I can't make it build); and compiler frontends that are actually
attached to compilers, such as pcc, sdcc, ack, gcc etc, which are
unfortunately so tightly integrated to the compiler backend that
ripping out the existing
backend and replacing it with mine is a huge amount of work.
I'm sure there *must* be something like this out there, somewhere. Can
anyone suggest anything I should have a look at?
(Open source, please!)
[Don't hold your breath waiting for free tools here. Sometimes you only
get what you pay for. -John]
You may want to take another look at Elsa. I've been successful using
Elsa as a C front end for LLVM. I can compile most of C (e.g. bzip2,
etc.) to a running program after a couple of weeks of work.
What sort of problems have you encountered building Elsa? It compiled
cleanly for me an my Linux box.
-Rich
I get two problems, possibly related: the Perl script that munges the
output of flex loses a line when inserting a #define, that breaks the
code (it's the closing line of a comment...); and then once fixed, I get
multiple definitions of yyFlexLexer::yywrap() when linking.
This looks to me very much as if the authors are using a different
version of flex than I am (2.5.34), and their Perl script is making
assumptions that aren't valid for my flex's C++ boilerplate.
(Interesting you mention using Elsa as a front end for LLVM. How's Clang
doing these days? What's your front-end like compared to it?)
...
Naturally, about an hour after posting my message, I found a rather
promising library called sparse:
http://www.kernel.org/pub/software/devel/sparse/
This is a full ANSI C frontend that appears to meet my requirements
ideally; it even comes with two example clients that are compilers! It
even breaks down the AST expression trees into 3op pseudoinstructions.
I'm not entirely sure that's what I want, but I'll do some
experimentation. Unfortunately, it's completely undocumented and I don't
actually know if it's complete or not.
Anything else interesting out there?
The Cilk project used to distribute C2C a tool to do exactly that.
The Cilk project project is alive, but looks like the C2C tool is gone.
( i think i have a copy of it)
Cilk: http://supertech.csail.mit.edu/cilk/
May be C2C is incorporated in the latest Cilk distribution
There is another tool: jscpp.
http://www.die-schoens.de/prg/
> Does anyone know if there are any clean, standalone libraries for
> parsing, verifying and then generating full ASTs, from C source code?
You could look at CIL: http://manju.cs.berkeley.edu/cil/
Torben
> I'm working on a C based tool that needs to operate of a full C AST ---
> basically, it can be thought of as a code generator for a really twisted
> CPU architecture.
>
> Does anyone know if there are any clean, standalone libraries for
> parsing, verifying and then generating full ASTs, from C source code?
I only have ToPas, a C parser written in Delphi. If you are interested,
have a look at SourceForge. Support for an AST could be added...
Another chance: TextTransformer. Ask the author for his C++ parser, used
in an C++ to Java translator. TextTransformer can (be made to) create an
AST, and also can produce C++ code for the parser, in the prof version.
DoDi
I'm not sure I understand your requirements right, but unless you
absolutedly need the parser to be written in C, the `ckit' might be
a good candidate. See http://www.smlnj.org/doc/ckit/index.html
Stefan
Thanks for all the replies; currently, sparse seems to be looking good
--- it even decomposes the expression trees into pseudo-code using
virtual registers in SSA form with liveness tracking, which means that
all I need to do is to bolt a noddy register allocator on the top and
I'm pretty much good to go. Very convenient.
However, it *is* all under construction and is totally undocumented,
which means I've got no idea if there are any gotchas coming up: but if
so, I've got plenty of other tools to look at. Thanks!
> I'm working on a C based tool that needs to operate of a full C AST ---
> basically, it can be thought of as a code generator for a really twisted
> CPU architecture.
>
> Does anyone know if there are any clean, standalone libraries for
> parsing, verifying and then generating full ASTs, from C source code?
[..]
You may try cparser (http://sourceforge.net/projects/cparser), our C99
frontend for libFirm. It's written in C99 and parses C99, most of GNU C and
some MS C extensions. It can at least compile the SPEC2000.
Currently you will additionaly need libFirm, because cparser uses it to do
the necessary constant folding.
We will drop this dependency in the next versions ...
best regards,
--
Michael Beck mm....@gmx.net
Quite well. You should try it! ;-)
--Owen
I'll second the recommendation of c2c. Its code is easy to understand
and plug into.