Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top