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
0 answers

Avoiding reinterpret_cast in difficult cases of polymorphism

There exists an interface #pragma once #include #include enum type_node { NONTERMINAL; TERMINAL; } class Node { public: Node(type_node); virtual ~Node(); type_node node_type; } Node::Node(type_node TYPE) { …
Ivor Denham-Dyson
  • 655
  • 1
  • 5
  • 24
1
vote
1 answer

How to serialize a TypeScript AST node & its dependencies

If references are statically defined (are not dynamically created), does the TypeScript compiler API offer a way to easily serialize a node and its dependencies? For instance: file-a.ts export const a = () => { …
1
vote
0 answers

how to update an AST without breaking the code when it contains comments

given this code, how do i print it with the extra import spec without breaking comments nodes. In below code I load a package from a string using the package loader API. I try to consume the resulting AST to insert a new node, an import…
user4466350
1
vote
1 answer

How to write Elixir macro similar to Ecto's field/2?

I'm learning Elixir and came across such situation: I have an Ecto schema and I want to make a function like "get_by" that takes a column name and it's value as an arguments like this: get_by(:id, 7) So the working version of the function would be…
m.cichacz
  • 749
  • 9
  • 21
1
vote
1 answer

How could I search references to a field on a AST or a CompilationUnit in eclipse?

Hi, I'm developing an Eclipse plugin. I need to find all the references in the source using AST's or jdt.core.dom or something like that. I need this references like ASTNodes in order to get the parent node and check several things in…
1
vote
1 answer

ast.Inspect not walking *ast.UnaryExpr

I'm trying to inspect Go source code in order to make a tool. For this I'm using the ast.Inspect function. I need to know how channels are being used inside a function/method. I have this as an example code to inspect: package main func B(ch chan…
SegFault
  • 59
  • 4
1
vote
2 answers

Convert Arithmetic Expression Tree to string without unnecessary parenthesis?

Assume I build a Abstract Syntax Tree of simple arithmetic operators, like Div(left,right), Add(left,right), Prod(left,right),Sum(left,right), Sub(left,right). However when I want to convert the AST to string, I found it is hard to remove those…
worldterminator
  • 2,968
  • 6
  • 33
  • 52
1
vote
0 answers

Building AST automatically

I have a parser for a legacy programming language. The AST is not present in the parser and I need to build an AST automatically when an attributes are added to the node. For E.g., Let's consider the below Node along with some custom nodes…
1
vote
1 answer

Python: How to validate function calls without executing code

I want to validate if python function calls are valid without executing them. Let's say I have the following code: def func(a, b): print(a, b) cond = True if cond: func(1, 2) else: func(1) If cond = True, then everything will run just…
Vinay Sharma
  • 164
  • 1
  • 7
1
vote
1 answer

Haskell Ast -> IO ()

I've been working on an assignment about parsing in Haskell. I've made the function to parse a string to an abstract syntax tree an this is working as it should. Short example of how the parser works with the code I've previously made: tokenize ::…
DonnumS
  • 196
  • 1
  • 15
1
vote
3 answers

Extract all variables from a string of Python code (regex or AST)

I want to find and extract all the variables in a string that contains Python code. I only want to extract the variables (and variables with subscripts) but not function calls. For example, from the following string: code = 'foo + bar[1] +…
AstrOne
  • 3,569
  • 7
  • 32
  • 54
1
vote
0 answers

Custom ESLint rule that can search the AST tree for nodes that follow the first node

I am trying to write a custom eslint rule that makes sure that for every react context that is created, that a display name is set. const myContext = React.createContext() myContext.displayName = "myName" It is fairly trivial for me to write a rule…
davorb
  • 613
  • 1
  • 9
  • 17
1
vote
0 answers

What does Canonicalization phase do in Groovy compiler design?

while I am reading groovy compile-time meta-programming, I read about different phases of compiler. They have mentioned one line statement as "Canonicalization: Complete building the AST". Can someone please help me to understand Canonicalization…
1
vote
1 answer

How to identify if a class definition node is an extension of another class

I need to identify if a class is an extension of another class when I get into its ast node. As the parenthesis with a super-class inside does not make part of the class name I'm not being able to identify that. if isinstance(class,…
1
vote
2 answers

ast execution produces different result in a function vs module scope

Edit: the problem is not related to ast module. it can be reproduced with just using exec: y = None exec("y = 5.0") print(y) # prints 5.0 vs def foo(): y = None exec("y = 5.0") print(y) foo() # prints None Original question: I am…
ikamen
  • 3,175
  • 1
  • 25
  • 47