abstract-syntax-tree

Adding nodes to Clang's AST

北战南征 提交于 2019-12-04 11:31:31
I need to insert new nodes to AST. for instance, adding a namespace to a function: Turning this - void foo(); into this - namespace bar { void foo(); } I read How to clone or create an AST Stmt node of clang? but I prefer not using source-to-source compilation Tnx The answer can be found here http://clang-developers.42468.n3.nabble.com/Adding-nodes-to-Clang-s-AST-td4054800.html However, the nodes are added to the compiled AST - for instance, in case one wants to inject a namespace to the code, he can't use the namespace directly via the code because the lexer don't know about it. 来源: https:/

Print arguments of a function using Clang AST

时光毁灭记忆、已成空白 提交于 2019-12-04 11:23:09
问题 I want to get the arguments passed to a function. for example, if I have the call printf("%d%d", i, j); the output should be %d%d i j I am able to get to function calls using VisitCallExpr() in RecursiveASTVisitor. Also able to get the number of arguments and the argument types. But I don't know how to get the arguments. bool MyRecursiveASTVisitor::VisitCallExpr (clang::CallExpr *E) { for(int i=0, j=E->getNumArgs(); i<j; i++) { llvm::errs() << "argType: " << E->getArg(i)->getType()

How to get abstract syntax tree of a `c` program in `GCC`

人走茶凉 提交于 2019-12-04 11:07:03
How can I get the abstract syntax tree of a c program in gcc? I'm trying to automatically insert OpenMP pragmas to the input c program. I need to analyze nested for loops for finding dependencies so that I can insert appropriate OpenMP pragmas. So basically what I want to do is traverse and analyze the abstract syntax tree of the input c program. How do I achieve this? You need full dataflow to find 'dependencies'. Then you will need to actually insert the OpenMP calls. What you want is a program transformation system. GCC probably has the dependency information, but it is famously difficult

ANTLR 4 and AST visitors

五迷三道 提交于 2019-12-04 10:34:21
问题 I'm trying to use ASTs with ANTLR4, with this files: Builder.java import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.TokenStream; public class Builder { public static void main(String[] args) { CharStream input = new ANTLRInputStream("ON M1==2 && M3 == 5 && (M2 > 1 || M5 <= 5.0) " + "DO P5:42 P4:10"); ExprLexer lexer = new ExprLexer(input); TokenStream tokens = new CommonTokenStream

Python AST from ANTLR Parse Tree?

末鹿安然 提交于 2019-12-04 09:53:35
I found an ANTLRv4 Python3 grammer , but it generates a parse-tree, which generally has many useless nodes. I'm looking for a known package to get a Python AST from that parse tree. Does something like this exist? EDIT: Clarification regarding use of the Python ast package: my project is in Java and I need to parse Python files. EDIT 2: By 'AST' I mean http://docs.python.org/2/library/ast.html#abstract-grammar , while by 'parse tree' I mean http://docs.python.org/2/reference/grammar.html . The following could be a start: public class AST { private final Object payload; private final List<AST>

Java library for code analysis

ε祈祈猫儿з 提交于 2019-12-04 08:41:54
Is there any Java library, that can help in building AST from the specified java source file and vice versa (generate code from the ASTree object)? I need something like this , but with an API, allowing to access the generated tree programmatically. Everything's already available within the Eclipse core. Here's a page with a small example of how to use org.eclipse.jdt.core.dom.ASTParser to create your desired AST datastructure. 来源: https://stackoverflow.com/questions/2832996/java-library-for-code-analysis

What is the easiest way to update an immutable AST?

假装没事ソ 提交于 2019-12-04 07:17:10
While working with macros, I have reached the point (I have been trying hard to avoid it) where I need to update those nodes in the AST which hold certain condition. For instance, let's say I would like to update each node: Literal(Constant(1)) with the value: Literal(Constant(2)) Those AST nodes could be anywhere in the expression tree, so I cannot use an ad-hoc pattern matcher. Obviously, the last thing I would like to do is to code a full pattern matcher which is able to cover all the compiler primitives. I have been searching in the API but I have the impression that methods such as

How to set the symbol for a raw AST when writing a scala compiler plugin?

社会主义新天地 提交于 2019-12-04 06:58:40
I'm working with a scala compiler plugin which replaces function calls like f(x) with a block { val newvalue = f(x) newvalue } This block has the same value as f(x) . The compiler plugin works right after compiler phase "typer" and do the transformation above by replacing the AST of f(x) with the AST of the block. Doing this after compiler phase "typer", the newly created block is a raw AST, without symbol and type, as well as its child ASTs. I need to call localTyper to give the raw ASTs symbols and types. val newVal = (ValDef(Modifiers(0), newTermName("newvalue"), TypeTree(a.tpe), a)) val

Convert Scala AST to source code

。_饼干妹妹 提交于 2019-12-04 05:57:38
Given a Scala AST, is there a way to generate Scala source code? I'm looking into ways to autogenerate Scala source by parsing/analyzing other Scala source. Any tips would be appreciated! I have been successfully using Scala-Refactoring by Mirko Stocker for this task. For synthetically constructing ASTs, it relies strongly on the existing Tree DSL of Scala's NSC. Although the code is a bit messy, you can find an example usage in my project ScalaCollider-UGens . I have also come across a very useful class by Johannes Rudolph . See our DMS Software Reengineering Toolkit . DMS provides a complete

Modeling an ordered tree with neo4j

孤人 提交于 2019-12-04 05:17:49
I'm just getting started with neo4j, and I understand the principles of the graph and relationships, but I'm having a little bit of trouble with certain structures I want to model. I wanted to use it on a programming language project, and store the AST of a parsed source file. From there, I plan on adding a lot of additional data and relationships to the nodes to help with analysis and tooling, but the fundamental AST is still a little difficult. The naive way of making a tree would be to simply walk the AST and copy every node in the tree to a node in neo4j, using properties to keep track of