2

I try to use Rascal to compute metrics out of an AST that is created with Clang out of Objective C code. Since the structure of the Clang AST differs from the one that is used in Rascal, i think it needs some refactoring or workarounds to work with it.

One way could be to write a parser that translate the AST. An other way could be to use regular expressions with recursion. But there are a lot of elements in the Clang AST and it would take some time to write a parser or a function.

Is there a less time consuming way to traverse a Clang AST in Rascal?

In the Clang AST introduction is a small sample of the AST

T.J.
  • 35
  • 5

2 Answers2

0

I believe it would be best to write some code against the Clang AST API to print the AST in a Rascal readable form. We use this strategy for other front-ends as well.

I.e. use the information in http://clang.llvm.org/docs/RAVFrontendAction.html to make a recursive AST visitor that prints things like:

ifStatement(intConstant(1),[])

After this you can read it in the AST using ValueIO: readTextValueFile(#node, file), or you could use the ShellExec library and readTextValueString function.

This would give you a representation of the AST typed node. If you want a typed representation, then also data declarations need to be generated, as in:

data Statement = ifStatement(Expression cond, list[Statement] body);

By the way, if anybody has written a JSON or XML exporter for Clang ASTs already, you would be king because there are libraries for Rascal to directly read in these formats.

Jurgen Vinju
  • 6,393
  • 1
  • 15
  • 26
  • BTW, other people have mapped Eclipse's JDT ASTs to Rascal, C# ASTs, PHP ASTs, etc. Sometimes we even do it twice for different front-ends for the same language. Doing so does gives you the front-end's assumptions about a language, but using Rascal you can quickly analyze and convert the input to something else. – Jurgen Vinju Mar 13 '14 at 06:40
  • 1
    Clang Version <2.9 and 3.3 has a build in function -ast-dump-xml that dumps the AST in a XML format. But the format isn't the same as the one Rascal reads. – T.J. Mar 13 '14 at 15:59
0

People are always trying to move ASTs between tools, so they don't have to use the original tool to complete the work. This is the "software tool bus" concept.

This is normally just a huge headache, because each tool comes with a set of assumptions about the structure and meaning of the AST, that the other tool doesn't have or share. Consequently you have to not only build AST-conversion tools, but often fill out the "other tool".

I've never seen it done very successfully, and it really doesn't save any effort. If you have a Clang AST, and want to process it, just stick with Clang.

[I'll say of all the people that might get away with it, Jurgen is one of the most likely. If you're not Jurgen, you may have more trouble.]

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • Hi Ira. This sounds like the start of a big discussion we should have some day :-) But not here on stackoverflow. – Jurgen Vinju Mar 13 '14 at 06:35
  • I'm sorry that this is the conclusion I've drawn by looking at many systems over the years that attempted this. Maybe Rascal changes the equation. Yes, we should have that discussion over a good beer. – Ira Baxter Mar 13 '14 at 08:31
  • Yes! Let's do that and maybe we can go through some code together. – Jurgen Vinju Mar 13 '14 at 09:01