Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C compiler front end wanted

34 views
Skip to first unread message

David Given

unread,
Jun 15, 2008, 8:18:59 AM6/15/08
to
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?

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]

Richard Pennington

unread,
Jun 15, 2008, 4:18:35 PM6/15/08
to
David Given wrote:
> I'm working on a C based tool that needs to operate of a full C AST ---
[snip]

> 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);
[snip]

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

David Given

unread,
Jun 15, 2008, 5:09:47 PM6/15/08
to
Richard Pennington wrote:
[...]

> 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.

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?

Sammy

unread,
Jun 15, 2008, 7:23:48 PM6/15/08
to
> any clean, standalone libraries for parsing, verifying and then generating
> full ASTs, from C source code?

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/

Torben Ægidius Mogensen

unread,
Jun 16, 2008, 3:42:15 AM6/16/08
to

David Given <d...@cowlark.com> writes:

> 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

Hans-Peter Diettrich

unread,
Jun 15, 2008, 3:59:25 PM6/15/08
to
David Given schrieb:

> 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

Stefan Monnier

unread,
Jun 17, 2008, 4:19:15 PM6/17/08
to
> 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.

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

David Given

unread,
Jun 19, 2008, 7:55:45 PM6/19/08
to
David Given wrote:
[...]

> Does anyone know if there are any clean, standalone libraries for
> parsing, verifying and then generating full ASTs, from C source code?

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!

Michael Beck

unread,
Jun 20, 2008, 8:43:41 AM6/20/08
to
David Given wrote:

> 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

there...@gmail.com

unread,
Jun 20, 2008, 3:55:42 PM6/20/08
to
On Jun 15, 2:09 pm, David Given <d...@cowlark.com> wrote:
> (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?)

Quite well. You should try it! ;-)

--Owen

eliben

unread,
Jul 4, 2008, 10:13:35 AM7/4/08
to

I'll second the recommendation of c2c. Its code is easy to understand
and plug into.

0 new messages