abstract-syntax-tree

Recursive descent parser questions

若如初见. 提交于 2019-12-10 13:23:15
问题 I have two questions about how to write a recursive descent parser: The first is what when you have a nonterminal that can match one of a few different nonterminals? How do you check which way is correct? Second, how do you build an AST? Using YACC, I can just write a piece of code to execute for every instance of a nonterminal and it has special variables which refer to the "values" of the rules. How do you do a similar thing in a recursive descent parser? 回答1: You try them in order, then

Groovy AST Transformation - replace method

做~自己de王妃 提交于 2019-12-10 11:38:09
问题 I am trying to replace a method of a class using AST transformations. I first check to see if the method exists and then remove it (based on this). MethodNode methodNode = parentClass.getMethod(methodName, Parameter.EMPTY_ARRAY) if (methodNode != null) { parentClass.getMethods().remove(methodNode) } I see the size change on the collection, but the method is still accessible on the class node. After removing the method I would then like to add a new one of the same name: parentClass.addMethod

Getting all the nodes from Python AST that correspond to a particular variable with a given name

筅森魡賤 提交于 2019-12-10 10:28:14
问题 Consider the code below: 1 | x = 20 2 | 3 | def f(): 4 | x = 0 5 | for x in range(10): 6 | x += 10 7 | return x 8 | f() 9 | 10| for x in range(10): 11| pass 12| x += 1 13| print(x) The value of x after execution of the code above is 10 . Now, how can I get all the nodes with class Name whose id s are x and refer to the x that's being used in lines 1, 10, 12 and 13? In other words, the x inside of f is different from the rest of the x s. Is it possible to get their AST nodes, having only the

How do I generate an AST from a string of C++ using Clang?

痞子三分冷 提交于 2019-12-10 02:39:54
问题 I am trying to use Clang to manipulate C++ source-code, but I am having trouble discovering the API. I would like to take a string of C++ source-code and generate an AST from it; something like: auto myAst = clang::parse("auto x = 1 + 1;"); Is there a minimal working example of this? 回答1: You can try the next code: std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("auto x = 1 + 1;")); TranslationUnitDecl *DC = AST->getASTContext().getTranslationUnitDecl(); if (DC) { llvm::errs() << "----

How to retrieve a Control Flow Graph for python code?

≡放荡痞女 提交于 2019-12-09 19:23:08
问题 I would like to dump the Control Flow Graph of a given python code, similar to the option given by gcc compiler option: -fdump-tree-cfg for c code. I succeeded getting the AST (Abstract Syntax Trees) of a python code, but it seams quite complex and hassle to get the Control Flow Graph from AST phase. Is there an easier way to retrieve directly the Control Flow Graph of a python code? any suggestions? oh by the way I'm using python3.5 Thank you all! P.S I really don't know what kind of

Representing C#3 code as an Abstract Syntax Tree in XML

北城以北 提交于 2019-12-09 18:42:29
问题 I was wondering if there is something similar to GCC_XML for C#3; basically a way to represent a program's entire syntactic structure in XML. Once the representation is created, I was hoping to parse it into an XDocument and interpret or query it from there. Are there tools out there for this? 回答1: Our DMS Software Reengineering Toolkit can do this with C# 2/3/4. (EDIT 2014: and now C# 5) DMS has compiler accurate parsers for C# (as well as Java and many other languages). It automatically

Convert Scala AST to source code

夙愿已清 提交于 2019-12-09 17:41:19
问题 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! 回答1: 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

What are synthesized attributes in the context of creating an abstract syntax tree?

☆樱花仙子☆ 提交于 2019-12-09 10:09:02
问题 Compilers parse source code and build an abstract syntax tree. The functions used to construct an abstract syntax tree return pointers which constitute synthesized attributes . What are they and how do they differ from inherited attributes .? edit: I don't know if this can help, but I originally heard of these terms in a French context: Attributs synthétisés, attributs hérités. 回答1: Attributes are additional values associated with something of central interest. In the case of ASTs, you can

How can I reuse definition (AST) subtrees in a macro?

早过忘川 提交于 2019-12-09 05:28:03
问题 I am working in a Scala embedded DSL and macros are becoming a main tool for achieving my purposes. I am getting an error while trying to reuse a subtree from the incoming macro expression into the resulting one. The situation is quite complex, but (I hope) I have simplified it for its understanding. Suppose we have this code: val y = transform { val x = 3 x } println(y) // prints 3 where 'transform' is the involved macro. Although it could seem it does absolutely nothing, it is really

How to exclude headers from AST in clang?

孤街浪徒 提交于 2019-12-09 04:18:29
问题 I'm generating AST using clang. I've got following file (lambda.cpp) to parse: #include <iostream> void my_lambda() { auto lambda = [](auto x, auto y) {return x + y;}; std::cout << "fabricati diem"; } I'm parsing this using following command: clang -Xclang -ast-dump -fsyntax-only lambda.cpp The problem is that clang parses also headers content. As a result, I've got quite big (~3000 lines) file with useless (for me) content. How to exclude headers when generating AST? 回答1: clang-check might