[cfe-dev] How to pretty print the abstract syntax tree for a C source file?

Simon simonhf at gmail.com
Tue Aug 23 11:48:35 PDT 2011


On Tue, Aug 23, 2011 at 10:58 AM, Ronan Keryell <
Ronan.Keryell at hpc-project.com> wrote:

> >>>>> On Tue, 23 Aug 2011 10:16:56 -0700, Simon <simonhf at gmail.com> said:
>
>    Simon> I am trying to find a tool that will parse and tell me
>    Simon> information about a C source file in a verbose manner which I
>    Simon> can digest and use in a script I am writing. The closest I
>    Simon> have come so far using gcc to generate debug info and then
>    Simon> extract the debug info and parse that.
>
> That is not a bad solution if it works for you, because you can use only
> some standard tools already available on any systems, such as objdump or
> gdb with a script...
>

It's the solution that gets closest to want I want to achieve so far. But
it's not pretty :-) It also doesn't contain the whitespace and comment data
(see below) which would be useful.

   Simon>  However, what I'd prefer is a tool which lets me get at
>    Simon> similar debug info (although obviously not memory offsets
>    Simon> etc) *without* having to compile the C source code into
>    Simon> object files. I know that clang has been re-written from
>    Simon> scratch to be a lot more flexible than gcc. Does anybody in
>    Simon> this list know how I could use clang (or another tool) to
>    Simon> obtain such debug info without compiling all the way?
>
> In the "other tools" family, gcc-xml can output the AST in an XML way.
> http://www.gccxml.org/HTML/Index.html


I really like the idea of this project. Too bad it's C++ only, unfinished
(doesn't process function bodies), and all but dead.


> In our Par4All compiler, we use another tool internally, PIPS, that
> allows to output the AST in HTML or in a textual format.
>
> There is a web service you can use to have an idea of what it can output:
> http://pips4u.org/doc/ir-navigator


I tried it out. Very nice web interface. Close to what I want except for two
things:
1. Is it possible to output file line number and character position so that
it is also part of the tree?
2. Is it possible for the tree to contain info relating to whitespace and
comments?

Ideally I'd like to be able to use a tool like PIPS to do so-called round
trip parsing where the original source code can be rebuilt exactly from the
intermediate representation. Somewhat like what this Perl module achieves:

http://search.cpan.org/~adamk/PPI-1.215/lib/PPI.pm

Thanks,
Simon


> --
>  Ronan KERYELL                      |\/
>  HPC Project                        |/)  Cell:   +33 613 143 766
>  5201 Great America Parkway #3241   K    Ronan.Keryell at hpc-project.com
>  Santa Clara, CA 95054              |\   skype:keryell
>  USA                                | \  http://hpc-project.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110823/2de1224b/attachment.html>


More information about the cfe-dev mailing list