abstract-syntax-tree

build AST in antlr4

独自空忆成欢 提交于 2019-11-28 01:23:12
问题 I was wondering whether we could build an AST using Antlr version 4. I couldn't find any reference on building it using antlr4. One SO answer says that it would be easy to use antlr4 which produces only parse tree but my question is what about the efficiency ? It forces us to crawl whole parse tree instead of an abstract syntax tree which is not efficient way to walk through the whole tree and perform task using visitors. 回答1: There are two key items I'd like to point out first: Efficiency

boost::spirit access position iterator from semantic actions

荒凉一梦 提交于 2019-11-27 23:05:10
Lets say I have code like this (line numbers for reference): 1: 2:function FuncName_1 { 3: var Var_1 = 3; 4: var Var_2 = 4; 5: ... I want to write a grammar that parses such text, puts all indentifiers (function and variable names) infos into a tree (utree?). Each node should preserve: line_num, column_num and symbol value. example: root: FuncName_1 (line:2,col:10) children[0]: Var_1 (line:3, col:8) children[1]: Var_1 (line:4, col:9) I want to put it into the tree because I plan to traverse through that tree and for each node I must know the 'context': (all parent nodes of current nodes). E.g,

Given an AST, is there a working library for getting the source?

一曲冷凌霜 提交于 2019-11-27 21:45:20
问题 Is there a way to convert a given Python abstract syntax tree (AST) to a source code? Here is a good example of how to use Python's ast module, specifically a NodeTransformer . I was looking for a way to convert the resulting AST back to source, so the changes can be inspected visually. 回答1: The Python source tree contains an implementation of this: unparse.py in the Demo/parser directory: https://github.com/python/cpython/blob/master/Tools/parser/unparse.py 回答2: A nice third-party library I

How to write Visitor Pattern for a Abstract Syntax Tree in C#?

一曲冷凌霜 提交于 2019-11-27 20:45:30
问题 I have 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 about concludes my understanding. To simplify everything, suppose I have nodes Root, Expression, Number, Op and the tree looks like this: Root | Op(+) / \ / \ Number(5) \ Op(*) / \ / \ / \ Number(2) Number(444) 回答1: Pattern visitor is a design pattern that allows you

What's the difference between parse trees and abstract syntax trees?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 19:49:11
问题 I found the two terms in a compiler design book, and I'd like to know what each stands for, and how they are different. I searched on the internet and found that parse trees are also called concrete syntax trees (CSTs). 回答1: This is based on the Expression Evaluator grammar by Terrence Parr. The grammar for this example: grammar Expr002; options { output=AST; ASTLabelType=CommonTree; // type of $stat.tree ref etc... } prog : ( stat )+ ; stat : expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^('

How does a simple calculator with parentheses work?

雨燕双飞 提交于 2019-11-27 19:24:46
I want to learn how calculators work. For example, say we have inputs in infix notation like this: 1 + 2 x 10 - 2 The parser would have to respect common rules in math. In the above example this means: 1 + (2 x 10) - 2 = 19 (rather than 3 x 10 - 2 = 28) And then consider this: 1 + 2 x ((2 / 9) + 7) - 2 Does it involve an Abstract Syntax Tree? A binary tree? How is the order of operations ensured to be mathematically correct? Must I use the shunting-yard algorithm to convert this to postfix notation? And then, how would I parse it in postfix notation? Why convert in the first place? Is there a

How can I use the java Eclipse Abstract Syntax Tree in a project outside Eclipse? (ie not an eclipse plugin)

ぃ、小莉子 提交于 2019-11-27 17:44:31
How can I use the java Eclipse Abstract Syntax Tree in a project outside Eclipse? (ie not an eclipse plugin) All the Eclipse AST examples that I've seen are for eclipse plugins. Is there a way (ie an example) of a project that uses the eclipse AST for a non-eclipse project. Below is the code I used to do this given a Java 1.5 file. I'm very new to this and spent today browsing around, and trying things out to get the code below working. public void processJavaFile(File file) { String source = FileUtils.readFileToString(file); Document document = new Document(source); ASTParser parser =

What is JavaScript AST, how to play with it?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 17:20:05
Abstract Syntax Tree.. I always heard that compile to SpiderMonkey AST on Github. So, is that a actual standard of JS syntax tree? And there's V8, is V8 using the same kind of AST? How can I play with that? SpiderMonkey offers the parser api . This is probably the easiest way to get your hands on the syntax objects. There's also open js-js parsers like Esprima (which is ECMAScript, really, but it's right up the alley) 1.You can take a look at AST explorer . An online tool to explore the ASTs generated by more than 10 parsers. It is a good tool to learn AST tree of a language. AST explorer

How do you write an arithmetic expression parser in JavaScript, without using eval or a constructor function?

不想你离开。 提交于 2019-11-27 16:41:54
问题 Given a string: var str1 = "25*5+5*7"; Without using eval or the constructor function in JavaScript, how would I be able to write a function called "output" that takes in the string and outputs the arithmetic value of the string, which in this case is 160? 回答1: Here's a full precedence expression evaluator following the recursive parsing idea I linked-to in a comment on the OP's question. To do this, first I wrote a simple BNF grammar for the expressions I wanted to process: sum = product |

How to get source corresponding to a Python AST node?

邮差的信 提交于 2019-11-27 16:02:53
问题 Python AST nodes have lineno and col_offset attributes, which indicate the beginning of respective code range. Is there an easy way to get also the end of the code range? A 3rd party library? 回答1: EDIT: Latest code (tested in Python 3.5-3.7) is here: https://bitbucket.org/plas/thonny/src/master/thonny/ast_utils.py As I didn't find an easy way, here's a hard (and probably not optimal) way. Might crash and/or work incorrectly if there are more lineno/col_offset bugs in Python parser than those