Questions tagged [abstract-syntax-tree]

Abstract syntax trees (ASTs) represent the recursive structure of a formal document (program source code).

Abstract Syntax Trees (abbreviated "AST") represent the structure of a formal document (often a computer source program). Nodes in the tree represent syntactically significant chunks of document (function definitions, declarations, statements, expressions and subexpressions). Children of a node represent the parts of that chunk (for a function definition node, the children are likely to be "name", "signature" and "body").

ASTs are widely used in program analysis and transformation systems, as well as classical tools such as compilers.

They are typically constructed by a parsing activity, which is driven by the BNF rules that describe the formal document structure. If one captures precisely how the parser matches the BNF rules to the formal document, the result is a so-called concrete syntax tree (CST). Usually CSTs contain a lot of unnecessary detail (such as parentheses and keywords) that are not needed by the tool using the tree, since the tree nodes themselves essentially represent grammar rules and this information is thus redundant. So parsers often construct ASTs during the parsing process to produce relatively compact trees compared to what a CST would be. See What is the difference between an Abstract Syntax Tree and a Concrete Syntax Tree? for more discussion on this topic.

There is an "ast" tag for StackOverflow; you should use the "abstract-syntax-tree" tag instead.

2945 questions
1
vote
1 answer

How to return a generic type in Java AST

I am writing a program to modify Java abstract syntax tree (AST), but I don't know how to generate a generic return type. Who can help me? The generated method looks like this: public static List converter() { return new…
NewGeek
  • 41
  • 4
1
vote
1 answer

How to get the variable and its size after Declaration?

I am working on clang Libtooling. I wanted to get the information on array and its size ,when its again used. I am facing difficulty on how to implement it. This is my testfile void test() { int array[5]; array[4]=8; } till now I was able to get…
Jon marsh
  • 279
  • 7
  • 12
1
vote
0 answers

Finding all variables created by assignment - Not working for pairlist

I'm currently doing Advanced-R, 18 Expressions. Topic is about 18.5.2 Finding all variables created by assignment, but the given code doesn't work in the case of pairlist. I followed all the given codes, but the results are not quite same with what…
HyeonPhil Youn
  • 428
  • 4
  • 11
1
vote
0 answers

Clang tool SIGBUS on multiple files using a clang::rewriter

I want to run some source to source transformation using clang. Instead of using the commonoptions for input Im using a JSONCompilationDatabase. It runs fine for a single file but it receives a SIGBUS when run on multiple files and specifically…
EvanKaraf
  • 11
  • 1
1
vote
2 answers

Writing an ESLint rule, how to find an exported class, by its identifier?

I am writing an ESLint rule for a plugin. I have the following test code: const test = require('./test'); module.exports.foo = class Foo {} module.exports.test = class Test {} I have the following rule: module.exports = { create: (context) =>…
Gary
  • 3,891
  • 8
  • 38
  • 60
1
vote
1 answer

How to replace left recursion in boost spirit x3?

I’m currently trying to parse recursive expressions like ***a and a*** with boost spirit x3. So I defined my abstract syntax tree as follows: namespace client { namespace ast { struct pointer; struct type : x3::variant< …
1
vote
2 answers

Is there an elegant way to traverse Clang AST Statements?

I am trying to traverse all function definitions and extract information from them. I have to iterate over all statements in the function body, and depending on the type, execute a specific function. For the moment I have an ugly if-else block. Is…
kleii
  • 21
  • 4
1
vote
1 answer

How to get the type of a variable with scalameta if the decltpe is empty?

If I have the following type Defn.Var(mods, pats, decltpe, rhs) in scalameta it might happen that decltype is set to None for a variable like this: var x = 10 I still want to know the exact type of the variable x which Scala has inferred without…
Baradé
  • 1,290
  • 1
  • 15
  • 35
1
vote
2 answers

Identify the functions in python

I am trying to parse the python file using 'ast' and I am able to successfully do it. Now I need to differentiate the default methods of 'dict' and 'list' Example : I can have a method like below classa = new TestClassA classa.test() But If I…
Harry
  • 3,072
  • 6
  • 43
  • 100
1
vote
1 answer

How to avoid down-casting when return types are not known at compile time?

Suppose I have a abstract base class called Node. class Node { public: Node() { leftChild = NULL; rightChild = NULL; }; Node * leftChild, *rightChild; void attach(Node * LC, Node * RC) { leftChild = LC; …
Ivor Denham-Dyson
  • 655
  • 1
  • 5
  • 24
1
vote
0 answers

How can I code for calculating a CompilationUnit lines of code in

I would like to calcualate a compilation unit lines of code intuitively. How can I use the method of CompilationUnit: CompilationUnit.getLineNumber([THELASTLINE].getPosition()) to get the lines of code of this compilationUnit. How can I get the ast…
Yi.
  • 515
  • 1
  • 7
  • 19
1
vote
2 answers

NullPointerException thrown while attempting to build AST from Jenkinsfile source

I'm attempting to parse a Jenkinsfile into an AST so I can perform validation on it (rather than using complex regular expressions). In my groovysh session: groovy:000> filePath = "/Users/ashleyconnor/Projects/Jenkins/Jenkinsfile" ===>…
Ashley
  • 2,256
  • 1
  • 33
  • 62
1
vote
1 answer

When is a line of Ruby parsed? evaluated? executed?

I was a little surprised to discover that person is defined by the following line of code, even when params[:person_id] doesn't exist: person = Person.find(params[:person_id]) if params[:person_id] I kind of expected that Ruby would first check the…
Matthew
  • 1,300
  • 12
  • 30
1
vote
0 answers

How can I convert a Parse Tree generated by ANTLR4 to Abstract Syntax Tree?

I have written a Python code to obtain the Parse Tree of a given Java source code using ANTLR4. I wish to convert this Parse Tree to AST and following that I wish to apply tree edit distance algorithm to different ASTs. The question is, how can I…
alpt
  • 61
  • 1
  • 5
1
vote
0 answers

Eclipse JDT resolve binding not working with copy of ASTNode

I have a code where I am replacing a logical expression with its negated form. Like, replacing A || B with !A && !B. For this, I am using JDT ASTNode's copySubtree() function to make a copy of the original node. Then modifying the copy and replacing…
1 2 3
99
100