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 match TemplateTypeParm node in Clang AST with AST_Matchers?

I'm trying to get the RHS template type of a TypeAliasDecl. Example: using AliasOfType = AliasedType; // AliasedType itself is a template I can retrieve AliasOfType in the AST using clang::ast_matchers::typeAliasDecl. I want to retrieve AliasedType…
Eric
  • 730
  • 4
  • 16
1
vote
0 answers

Getting the value of a 'ctx' object of an AST node

I have a config file, config.py, and I'm trying to read the value of an attribute without using importlib.import_module() (or imp.load_source()). So, I did this: # trying to just get the attribute 'comment' from the file import ast fd =…
Siva Kumar
  • 11
  • 1
1
vote
1 answer

How can I get a constructed tree in ply using the documentation example?

In the documentation they show this example to build a tree: def p_expression_binop(p): '''expression : expression PLUS expression | expression MINUS expression | expression TIMES expression …
1
vote
1 answer

IndentationError during `ast.parse` and `ast.walk` of a function which is a method inside class

I think I'm aware of the usual causes for IndentationError like described in IndentationError: unindent does not match any outer indentation level for example. This doesn't apply here. Also, I know about textwrap.dedent but it doesn't feel like it's…
1
vote
2 answers

How does/would the following snippet work for generation of an AST?

We're writing a compiler for Al Aho's compilers class, and we're considering the following code for the generation of our AST. Here is some background. We want to implement scoping rules as a stack of name-id mappings, and we want to push a set of…
alexgolec
  • 26,898
  • 33
  • 107
  • 159
1
vote
0 answers

How to find functions with same reference address in typescript AST

example: import { method } from 'lib' const a = method const b = a const c = method // a b c is the same function In AST, I can get the variable name method from ImportDeclaration, but how can I find a b c ? My purpose is to record where method…
philo
  • 502
  • 1
  • 4
  • 13
1
vote
1 answer

How to rename all variables according to rule set with AST

Is there a tool that is capable of iterating over all variables and function arguments in a Python file and replace them according to a style rule? For example, given a function in myModule.py def myFunc(Variable): Tmp = sin(Variable) …
Frank-Rene Schäfer
  • 3,182
  • 27
  • 51
1
vote
1 answer

Count the number of if statements in a file

I'm trying to read in a file of code and count the number of if-statements. How would I be able to go about doing this using AST.
1
vote
2 answers

How to get the DevlRefExpr operand out of UnaryOperator in the Clang LLVM AST?

I have this code here: class MemcpyMatcher : public MatchFinder::MatchCallback { public: MemcpyMatcher(map * replacements) : replacements(replacements) {} /* Callback method for the MatchFinder. * @param…
Galaxy
  • 2,363
  • 2
  • 25
  • 59
1
vote
1 answer

Remove unused imports with ast package?

After parsing a file and removing some functions, is it possible to also remove any now-unused imports before writing the new file?
Elliot Chance
  • 5,526
  • 10
  • 49
  • 80
1
vote
2 answers

Adding Intentional Ambiguity to Parser/Interpreter

I've written a parser that properly takes an expression and creates an AST from it. My interpreter then takes that AST, evaluates it, and then returns a solution. However, I'd like the parser (or the interpreter) to account for ambiguity (lack of…
1
vote
0 answers

Exit condition for AST with DFS

I'm trying to write a general purpose AST builder with a DFS approach. The principle of the grammars is rather simple: An expression can have one or more options, each option consists of a list of nodes where each node can be either a terminal or an…
Cabadath
  • 897
  • 3
  • 13
  • 23
1
vote
2 answers

How to check if string is valid python code (runtime error)

Is there a possibility to check on Runtime errors? It is clear from a string that pri1nt is a function and it is not defined in this string. import ast def is_valid_python(code): try: ast.parse(code) except SyntaxError: return False …
Oleg Dats
  • 3,933
  • 9
  • 38
  • 61
1
vote
1 answer

How to replace expression by String in JavaParser AST

Suppose I have expression "(a == b || a == c) && (d == e)". How can I replace subexpression by custom String e.g. how can I replace a == b by a.equals(b)?
matoni
  • 2,479
  • 21
  • 39
1
vote
1 answer

AST how to deal with empty nodes

I am building an expression evaluator using Java, JFlex(lexer gen) and Jacc(parser gen). I need to: generate the lexer generate the parser generate the AST display the AST graph evaluate expression I was able to create the lexer and the parser and…
Anonimista
  • 185
  • 1
  • 11
1 2 3
99
100