FAQ Database Discussion Community


Generate an AST in C++

c++,algorithm,parsing,abstract-syntax-tree,lalr
I'm making an interpreter in C++, so far I've got my lexer to generate tokens. The problem is I'm not sure how to generate an "walk" a parse tree. I was thinking of making my parse tree using an array of arrays, but I'm not sure how to actually insert...

How to process the stuctured language file in python

python,abstract-syntax-tree,parser-generator,ebnf,grako
I have a big structured language file like this: TASK SchM_Task { TYPE = AUTO; SCHEDULE = NON; PRIORITY = 160; ACTIVATION = 1; TIMING_PROTECTION = FALSE; AUTOSTART = FALSE; EVENT = SchM_Event; RESOURCE = SystemS_Resource; StackSize = 1024; NotUsingSchedule = FALSE; }: "BSW task for calling of bsw runnables";...

How to create AST with ANTLR4?

java,compiler-construction,antlr,abstract-syntax-tree,antlr4
I've been searching A LOT about this and I couldn't find anything useful that REALLY helps me build an AST. I already know that ANTLR4 doesn't build AST like ANTLR3 used to do. Everyone say: "Hey, use visitors!", but I couldn't find any example or more detailed explanation on HOW...

Traversal of tokens using ParserRuleContext in listener - ANTLR4

java,antlr,abstract-syntax-tree,antlr4,static-analysis
While iterating over the tokens using a Listener, I would like to know how to use the ParserRuleContext to peek at the next token or the next few tokens in the token stream? In the code below I am trying to peek at all the tokens after the current token...

How to implement if-else branch in a abstract syntax tree using C++

c++,abstract-syntax-tree
I have a mini AST structure in which each node may have a left and a right child, for example: class AstNode; typedef std::shared_ptr<AstNode> AstNodePtr; class AstNode { public: AstNode() : m_children(2) { } virtual ~AstNode() { } virtual void accept(AstNodeVisitor& visitor) = 0; void addLeft(const AstNodePtr& child); void addRight(const...

How to traverse typed abstract syntax tree in OCaml compiler

compiler-construction,ocaml,abstract-syntax-tree,ocamlbuild
I'm trying to dump type information of all identifiers in an OCaml project, basically it's the same as traversing the typed abstract syntax tree(https://github.com/ocaml/ocaml/blob/trunk/typing/typedtree.mli). Since I'm new to OCaml compiler's codebase, I'm not sure whether the Compiler has provided apis so we could easily write a plugin to do the...

How can I deal with comments in my AST?

delphi,haskell,comments,abstract-syntax-tree
I am writing a Delphi code parser using Parsec, my current AST data structures look like this: module Text.DelphiParser.Ast where data TypeName = TypeName String [String] deriving (Show) type UnitName = String data ArgumentKind = Const | Var | Out | Normal deriving (Show) data Argument = Argument ArgumentKind String...

LR(1) parse straight to Abstract syntax tree

java,parsing,tree,abstract-syntax-tree,lr
So I asked a question close this recently and received an very good answer. However the steps that were described seemed more like the steps to create a concrete syntax tree. Each reduction in the LR parsing process corresponds to an internal node in the parse tree. The rule being...

replace the $ in php variable declaration

php,syntax,bison,abstract-syntax-tree
I would like to extend the php syntax, in order to tell apart mutable and immutable variables. $a should be declared mutable (as in standard php) and #b should be declared immutable. I've read Hacking PHP syntax, and I couldn't figure out where can I define that variables declared with...

Iterative update of abstract syntax tree with boost spirit

c++,boost,abstract-syntax-tree,boost-spirit,boost-spirit-qi
I have a working boost spirit parser and was thinking if it is possible to do iterative update of an abstract syntax tree with boost spirit? I have a struct similar to: struct ast; typedef boost::variant< boost::recursive_wrapper<ast> > node; struct ast { std::vector<int> value; std::vector<node> children; }; Which is being...

Copy nodes in AST

java,parsing,copy,abstract-syntax-tree,javaparser
I am using javaparser to construct an AST from Java source code. Is there a way to safely copy AST nodes? For example, if I want to duplicate a BinaryExpr to insert somewhere else in the tree, is there a built-in way to copy the node?...

Pattern matching AST nodes in Rascal

pattern-matching,abstract-syntax-tree,rascal
I have the following AST definition: data Exp = app(Exp fun, Exp body) | var(str name) | nat(int nat) | func(list[str] formal, Exp body) | cond(Exp cond, Exp then, list[tuple[Exp,Exp]] elifs, Exp otherwise) | let(list[str] vars, list[Exp] exps, Exp body) | seq(Exp lhs, Exp rhs) | mul(Exp lhs, Exp rhs)...

How to print hand-written AST?

go,code-generation,abstract-syntax-tree
I have a set of XML description of network protocol that I'm trying to generate Go code from, therefore I don't have any existing Go code to work with. All of the examples that make use of go/ast (such as go fmt) take existing code, do some transformation on the...

Clang ast-dump doesn't show some global variables

c++,clang,abstract-syntax-tree
I have a CPP file and I'm using clang to print all variables in global scope. The problem is following: command clang -cc1 -ast-dump filename.cpp doesn't show some global variables: |-VarDecl 0x2094370 N 'int' invalid |-VarDecl 0x20943e0 MAXN 'const int' invalid |-VarDecl 0x2094470 p 'int' invalid |-VarDecl 0x20944e0 u '_Bool'...

Syntax directed translation

compiler-construction,abstract-syntax-tree,context-free-grammar,semantic-analysis
I am trying to write the semantic rules for syntax directed translation of an expression following a given cfg into 3 code representation. Consider, Here, Why is || operator required ? gen() is the only thing needed it seems as it does what is required ....

RuntimeError: maximum recursion depth exceeded while calling a Python object with ast

python,python-2.7,recursion,abstract-syntax-tree
I'm using ast to get information from Python source files (as per the suggestion here). For this, I have extended ast.NodeVisitor and it runs fine with most source files. However, I'm getting a RuntimeError: maximum recursion depth exceeded while calling a Python object exception when I use this on big...

How to get binary subexpressions with javaparser

java,parsing,abstract-syntax-tree,javaparser
I am using javaparser to parse the AST for Java source files. I would like to get all binary subexpressions in the source code as individual nodes. Here is the source code I am parsing: class MyClass { public MyClass() { double x = (4 / 10.0) + 15; }...

Using python to remove all strings and comments from python code

python,abstract-syntax-tree
I'd like to count the uniqueness of my variable names in all the python code I've written. To do so, I need to strip out all the keywords and reserved words (which are known), the comments and the strings. For example, the following code: ''' long comment ''' for x...

Cannot get method added to GORM object using AST transformation

grails,groovy,gorm,abstract-syntax-tree
Here is my GORM object @UpdatedProperties class Cart { Date lastUpdated Date dateCreated String name } Here is annotation definition: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @GroovyASTTransformationClass(["UpdatedPropertiesASTTransformation"]) public @interface UpdatedProperties { } Here is the AST definition @GroovyASTTransformation(phase = CompilePhase.CLASS_GENERATION) class UpdatedPropertiesASTTransformation implements ASTTransformation{ //... public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {...

How can I compare two source code files/ ast trees?

parsing,go,compare,abstract-syntax-tree
I'm generating some source code using the templates package( is there a better method? )and part of the testing I need to check if the output matches the expected source code. I tried a string comparison but it fails due the extra spaces / new lines generated by the templates...

Three-address code and symbol tables

compiler-construction,ocaml,abstract-syntax-tree,intermediate-language,symbol-table
I am working on a hobby retargetable C compiler in OCaml and I'm building it bottom up. So far I have an annotated AST type, abridged: type 'e expr = | Int of 'e * int | Var of 'e * var | Neg of 'e * 'e expr |...

How do I translate LR(1) Parse into a Abstract syntax tree?

abstract-syntax-tree,lr,semantic-analysis,lr1,concrete-syntax-tree
I have coded a table driven LR(1) parser and it is working very well however I am having a bit of a disconnect on the stage of turing a parse into a syntax tree/abstract syntax tree. This is a project that I m very passionate about but I have really...

what compiler should I use as case study for self studying compiler principles techniques [closed]

c,compiler-construction,code-generation,abstract-syntax-tree,lexical-analysis
I decided to start studying compiler theory but the problem is that I want a compiler for any language in order to track each of lexical analyzer output. syntax tree. intermediate representation. code generation. I dont care for optimization right now I am aware of some questions similar to mine...

Creating and Implementing Abstract Syntax Tree in Assembly 86x

assembly,x86,abstract-syntax-tree
so i recently started learning Assembly language and im having trouble creating Abstract Syntax Trees (AST) and then implementing them in Assembly. So lets say i have this equation: z = (3 - 2*x)*x - 2*y + 1. So would the following AST be correct, as i know that there...

Implementing a Metric Suite using ASTParser in Java

java,eclipse,abstract-syntax-tree,cyclomatic-complexity
Background - Question below I am at the start of implementing a metric suite in Java for Java however I am concerned that my approach is not appropriate. Currently I am using the JDT's ASTParser for every file within a directory. This started off well and I was able to...

Why are distinct Type objects being generated by Clang's parser for the same type?

c++,types,clang,abstract-syntax-tree
I have the following snippet that I'm parsing using Clang's API for a tool I'm writing: namespace NS { struct X { }; } struct Y { NS::X foo(); }; I use a (type that derives from) RecursiveASTVisitor to visit the AST. When the VisitCXXRecordDecl() function is invoked, I can...

JDT ASTParser - How to resolve constant value?

java,abstract-syntax-tree,jdt
I have a visitor which should output caller type, its name, parameter type, corresponding type in method declaration and if it's a constant variable, its value public boolean visit(MethodInvocation e) { Expression methodExpr = e.getExpression(); String caller = methodExpr != null ? methodExpr.resolveTypeBinding().getQualifiedName() : e.resolveTypeBinding().getQualifiedName(); String methodName = e.getName().getFullyQualifiedName(); System.out.println("Caller:...

Import string that looks like a list “[0448521958, +61439800915]” from JSON into Python and make it an actual list?

python,json,list,abstract-syntax-tree
I am extracting a string out of a JSON document using python that is being sent by an app in development. This question is similar to some other questions, but I'm having trouble just using x = ast.literal_eval('[0448521958, +61439800915]') due to the plus sign. I'm trying to get each phone...

ast.literal_eval not working (python string of list to list)

python,string,list,eval,abstract-syntax-tree
I am trying to convert the following string of a list back to a list. [('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])] I have tried both eval() and ast.literal_eval but for some reason when I print the type of my converted string it is still a string (but with the quotations removed) I have...

PEGJS: Generating an AST for a predicate first syntax

javascript,parsing,grammar,abstract-syntax-tree,pegjs
I'm back to exploring pegjs and clearly have not grasped the core concept yet. I'm trying to parse a "query language" that starts with a predicate and then a list of operands (which could include another predicate). So a simple example would be: OR( "string1" "string2" ) I would like...

Python ast parsing exception

python-2.7,abstract-syntax-tree,python-3.4
I was using ast module of python3.4 to get the imports and function calls within a file. It works correctly if I run the code on a file which has python3.4 syntax but throws an exception if I try to parse a file of older python2.7 version (for print statements,...

Printing Abstract Syntax Tree, infinite recursion issue

c,recursion,stack,abstract-syntax-tree
For the final project in my C programming class, we are implementing a reverse polish notation calculator which can either evaluate an expression for correctness, return the corresponding infix expression, or print out mock assembly code. To do so, we are to implement both a stack and a Abstract Syntax...

What's the best way to implement AST using visitor pattern with return value?

c++,abstract-syntax-tree,visitor
I'm trying to implement a simple abstract syntax tree (AST) in C++ using the visitor pattern. Usually a visitor pattern does not handle return value. But in my AST there are expressions nodes which care about the return type and value of its children node. For example, I have a...