abstract-syntax-tree

How can I build an AST using ANTLR4? [duplicate]

一个人想着一个人 提交于 2019-11-26 17:50:56
问题 This question already has answers here : How to create AST with ANTLR4? (2 answers) Closed last year . I have an ANTLR3 grammar that builds an abstract syntax tree. I'm looking into upgrading to ANTLR4. However, it appears that ANTLR4 only builds parse trees and not abstract syntax trees. For example, the output=AST option is no longer recognized. Furthermore neither "AST" nor "abstract syntax" appears in the text of "The Definitive ANTLR4 reference" . I'm wondering if I'm missing something.

What's the difference between parse tree and AST?

爱⌒轻易说出口 提交于 2019-11-26 17:29:57
问题 Are they generated by different phases of a compiling process? Or are they just different names for the same thing? 回答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 -> ^('=' ID expr) | NEWLINE -> ; expr : multExpr (( '+'^ | '-'^ ) multExpr)* ; multExpr : atom ('*'^ atom)* ;

Eclipse create CompilationUnit from .java file

我们两清 提交于 2019-11-26 17:16:11
问题 How can I load a .java file into a CompilationUnit? For example, lets say I have a A.java file in my current project. I would like to load it into a CompilationUnit and then pass it to the ASTParser. It is not an option just to load it as a plain text since it seems that in that case I will not get the binding information in the AST. 回答1: You can load the projects using jdt and eclipse core libraries. Using the following code you can load all the projects in the workspace. IWorkspace

Python 3, Are there any known security holes in ast.literal_eval(node_or_string)?

流过昼夜 提交于 2019-11-26 13:56:45
问题 Are there any known ways for ast.literal_eval(node_or_string)'s evaluation to not actually be safe? If yes, are patches available for them? (I already know about PyPy[sandbox], which is presumably more secure, but unless the answers are yes then no, my needs are minor enough that I won't be going that far.) 回答1: The documentation states it is safe, and there is no bug relative to security of literal_eval in the bug tracker, so you can probably assume it is safe. Also, according to the source,

Why does ast.literal_eval('5 * 7') fail?

江枫思渺然 提交于 2019-11-26 12:30:38
问题 Why does the literal evaluation of 5 * 7 fail, while 5 + 7 doesn\'t? import ast print(ast.literal_eval(\'5 + 7\')) # -> 12 print(ast.literal_eval(\'5 * 7\')) # -> Traceback (most recent call last): ... ValueError: malformed node or string: <_ast.BinOp object at ...> The documentation doesn\'t explain this. I found that problem after answering this question on SO: Getting the result of a string. 回答1: ast.literal_eval() accepts + in the evaluated data because 5+2j (complex number * ) are valid

Malformed String ValueError ast.literal_eval() with String representation of Tuple

冷暖自知 提交于 2019-11-26 09:35:28
问题 I\'m trying to read in a string representation of a Tuple from a file, and add the tuple to a list. Here\'s the relevant code. raw_data = userfile.read().split(\'\\n\') for a in raw_data : print a btc_history.append(ast.literal_eval(a)) Here is the output: (Decimal(\'11.66985\'), Decimal(\'0E-8\')) Traceback (most recent call last): File \"./goxnotify.py\", line 74, in <module> main() File \"./goxnotify.py\", line 68, in main local.load_user_file(username,btc_history) File \"/home/unix-dude

Constructing an Abstract Syntax Tree with a list of Tokens

青春壹個敷衍的年華 提交于 2019-11-26 08:46:55
问题 I want to construct an AST from a list of tokens. I\'m making a scripting language and I\'ve already done the lexical analysis part, but I have no idea how to create an AST. So the question is, how do I take something like this: WORD, int WORD, x SYMBOL, = NUMBER, 5 SYMBOL, ; and convert it into an Abstract Syntax Tree? Preferably, I\'d like to do so without a library like ANTLR or whatever, I\'d rather try and do it from scratch myself. However, if it\'s a really complex task, I don\'t mind

What is the difference between an Abstract Syntax Tree and a Concrete Syntax Tree?

一个人想着一个人 提交于 2019-11-26 05:42:26
问题 I\'ve been reading a bit about how interpreters/compilers work, and one area where I\'m getting confused is the difference between an AST and a CST. My understanding is that the parser makes a CST, hands it to the semantic analyzer which turns it into an AST. However, my understanding is that the semantic analyzer simply ensures that rules are followed. I don\'t really understand why it would actually make any changes to make it abstract rather than concrete. Is there something that I\'m

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

匆匆过客 提交于 2019-11-26 02:20:55
问题 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 question is How an AST for a class in a OOPL would look like? My naive attempt is for this Java code: class Person { String name; int age; public String toString() { return \"name\"; } } Is: ;Hand written (classDeclaration Person (varDeclaration String name)

How to create AST with ANTLR4?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 01:35:34
问题 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 can I do this... I have a grammar must like C, but with every commands written in Portuguese (portuga programming language). I can easily generate the parse tree using ANTLR4. My question is: What I need to do