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
42
votes
6 answers

How to get lineno of "end-of-statement" in Python ast

I am trying to work on a script that manipulates another script in Python, the script to be modified has structure like: class SomethingRecord(Record): description = 'This records something' author = 'john smith' I use ast to locate the…
xis
  • 24,330
  • 9
  • 43
  • 59
40
votes
4 answers

What is an AST transformation?

What is an AST transformation in general? I came across these words when reading Groovy blog's. But what it is in general?
Aravinth
  • 401
  • 1
  • 4
  • 3
38
votes
1 answer

What is AST in graphql?

What is AST in graphql ? I am using graphql-js. How does it help with anything? Nothing in any documentation seems to explain what AST is
user2405589
  • 881
  • 2
  • 14
  • 32
37
votes
3 answers

How to write the Visitor Pattern for Abstract Syntax Tree in Python?

My collegue suggested me to write a visitor pattern to navigate the AST. Can anyone tell me more how would I start writing it? As far as I understand, each Node in AST would have visit() method (?) that would somehow get called (from where?). That…
36
votes
1 answer

Compiling an AST back to source code

I'm currently in the process of building a PHP Parser written in PHP, as no existing parser came up in my previous question. The parser itself works fairly well. Now obviously a parser by itself does little good (apart from static analysis). I would…
NikiC
  • 100,734
  • 37
  • 191
  • 225
34
votes
1 answer

Representing an Abstract Syntax Tree in C

I'm implementing a compiler for a simple toy language in C. I have a working scanner and parser, and a reasonable background on the conceptual function/construction of an AST. My question is related to the specific way to represent an AST in C. …
32
votes
3 answers

What does Lambda Expression Compile() method do?

I am trying to understand AST in C#. I wonder, what exactly Compile() method from this example does. // Some code skipped Expression> data = Expression.Lambda>( …
jk_
  • 5,448
  • 5
  • 25
  • 23
32
votes
1 answer

How to output the AST built using ANTLR?

I'm making a static analyzer for C. I have done the lexer and parser using ANTLR in which generates Java code. Does ANTLR build the AST for us automatically by options {output=AST;}? Or do I have to make the tree myself? If it does, then how to…
Raphael
  • 395
  • 1
  • 5
  • 7
31
votes
8 answers

Python AST with preserved comments

I can get AST without comments using import ast module = ast.parse(open('/path/to/module.py').read()) Could you show an example of getting AST with preserved comments (and whitespace)?
Andrei
  • 10,918
  • 12
  • 76
  • 110
27
votes
2 answers

Python code generator

I want to be able to perform code generation of python given an AST description. I've done static analysis of C and built AST visitors in python, so I feel relatively comfortable manipulating a syntax tree, but I've never attempted code generation…
mvanveen
  • 9,754
  • 8
  • 33
  • 42
27
votes
3 answers

What would an AST (abstract syntax tree) for an object-oriented programming language look like?

I'm reading about AST (abstract syntax trees) but all the samples I see use expressions such as: a + b * c Which could be represented in a lispy like syntax as: (+ a (* b c) ) Which will be the equivalent to: + / \ a * / \ b c My…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
27
votes
3 answers

get human readable AST from c++ code

In order to get a better understanding of some of the details of the C++ language and grammer, I would love to be able to write a small C++ program, and see the AST that a compiler generates from that. It looks like clang had this feature in the…
Lucas Meijer
  • 4,424
  • 6
  • 36
  • 53
26
votes
2 answers

Converting Coq term in AST form to polish notation using Python

Say I have an arbitrary Coq Term (in AST format using s-expressions/sexp) for example: n = n + n and I want to automatically convert it to: = n + n n by traversing the AST tree (which is simple a nested list of lists because of the sexp). Is there a…
Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
25
votes
11 answers

AST from C code

I want to perform some transformations on C source code. I need a tool on linux that generates a complete AST from the source code so that I can apply my transformations on this AST and then convert it back to the C source code. I tried ELSA but it…
Sachin Khot
  • 1,412
  • 2
  • 14
  • 18
25
votes
3 answers

Best design for generating code from an AST?

I'm working on a pretty complex DSL that I want to compile down into a few high level languages. The whole process has been a learning experience. The compiler is written in java. I was wondering if anyone knew a best practice for the design of…
Sam Washburn
  • 1,817
  • 3
  • 25
  • 43