[antlr-interest] just want to translate input into differently formatted output

Thomas Tempelmann tt at tempel.org
Sun Nov 11 06:26:48 PST 2007


Hi,
I'm new to Antlr. I have a rather simple task and wonder how Antlr can best
help me with it:

I want to scan various C header files containing struct { } declarations in
order to turn those structs into a differently formatted text in xml form.

For example, the following:

struct tag {
  byte  id; // note 1
  byte  len; /* note
                2 */
  int16 info;
};

shall be converted into:

<struct label="tag" >
  <field type="byte" label="id"" >
    note 1
  </field>
  <field type="byte" label="len" >
    note
    2
  </field>
  <field type="int16" label="info" />
</stuct>

(basically, I want to output the type, label and comment of each struct
field)

Furthermore, any other stuff in the C file I do not want passed to the
output.

While I could probably achieve this even with a regex find/replace pattern,
I like to use a proper C language parser (using regex might act wrong with
matchable text in a comment or in a string literal)

On the input side this has all been done already - there are C parsers for
Antlr and I can also modify them to my needs.

The question is the output. I have a vague idea that I'll have to add
actions to some of the grammar elements, but I'm not clear on this yet.

Are there any examples in this directions? Or can you suggest even totally
different tools that do this easily?

Thomas




More information about the antlr-interest mailing list