abstract-syntax-tree

javac.exe AST programmatic access example

…衆ロ難τιáo~ 提交于 2019-11-28 16:55:15
Is it possible to access the Abstract Syntax Tree(AST) inside the javac.exe programmatically? Could you provide an example? jamesh Yes, it is possible, but only since Java 6. Peter von der Ahé talks about the two JSRs in this interview . Of JSR 199: The JSR 199 Compiler API consists of three things: The first one basically allows you to invoke a compiler via the API. Second, the API allows you to customize how the compiler finds and writes out files. I mean files in the abstract sense, since the files the compiler deals with aren't necessarily on the file system. JSR 199's file abstraction

TypeScript: get syntax tree

佐手、 提交于 2019-11-28 16:39:57
I had read "whole internet", but can't find any examples about getting syntax tree (just like in Esprima) from TypeScrypt source. I mean how can i get object like this ( Esprima Parser example) { "type": "Program", "body": [ { "type": "VariableDeclaration", "declarations": [ { "type": "VariableDeclarator", "id": { "type": "Identifier", "name": "answer" }, "init": { "type": "BinaryExpression", "operator": "*", "left": { "type": "Literal", "value": 6, "raw": "6" }, "right": { "type": "Literal", "value": 7, "raw": "7" } } } ], "kind": "var" } ] } from javascript code var answer = 6 * 7; only for

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

徘徊边缘 提交于 2019-11-28 15:58:16
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). 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)* ; atom

Tracing Python expression evaluation step by step

爱⌒轻易说出口 提交于 2019-11-28 09:12:27
I'm trying to write Python expression evaluation visualizer, that will show how Python expressions are evaluated step by step (for education purposes). Philip Guo's Python Tutor is great, but it evaluates Python program line by line, and I found that students sometimes do not understand how one-line expressions like sorted([4, 2, 3, 1] + [5, 6])[1] == 2 are evaluated and I'd like to visualize this process. (It seems that nobody did it yet — at least I found nothing.) The ideal solution will create a sequence of strings like this: sorted([4, 2, 3, 1] + [5, 6])[1] == 2 sorted( >> [4, 2, 3, 1] +

ANTLR Grammar for expressions

六月ゝ 毕业季﹏ 提交于 2019-11-28 08:59:39
问题 I'm trying to implement a expression handling grammar (that deals with nested parenthesis and stuff). I have the following so far, but they can't deal with some cases (successful/failure cases appear after the following code block). Anyone know what's going on? Note: The varname += and varname = stuff are just some additional AST generation helper stuff in XText. Don't worry about them for now. ... NilExpression returns Expression: 'nil'; FalseExpression returns Expression: 'false';

Java Tree parser output for ANTLR

夙愿已清 提交于 2019-11-28 07:52:18
I've found a sample template in the ANTLR website, its the Javatreeparser.g, which the site says could produce the AST that I need, but since I'm new to ANTLR, how do I make it show? What I've done so far is placing the grammar file together with my existing java grammar. But I have no idea on how to use and output the AST that I need from the file. How do I do it? Bart Kiers I've found a sample template in the ANTLR website, its the Javatreeparser.g, which the site says could produce the AST that I need, No, the combined grammar Java.g from the ANTLR wiki produces a lexer and parser for Java

AST interpreter?

好久不见. 提交于 2019-11-28 07:03:40
I have an AST (abstract syntax tree) and now i want to test my compiler by giving it 2 or more numbers and expect an output with the result of math operations (like a calculator). My question is, what is the best way to build the interpreter? The visiting of the AST nodes is recursive, so i don't know how many encapsulated calculations exists until i get to the end of the tree. But since this is done iteration by iteration, how can i make all the operations in the end? Thank you Intepreters are pretty easy to code, once you have an AST: int interpret(tree t) { /* left to right, top down scan

antlr generate ast for c and parse the ast

点点圈 提交于 2019-11-28 06:01:38
问题 I am doing static analyze on c program.And I search the antlr website ,there seems to be no appropriate grammar file that produce ast for c program.Does it mean I have to do it myself from the very start.Or is there a quicker method.I also need a tree parser that can traverse the ast created by the parser. 回答1: You indicated you want to do static analysis to detect buffer overflow. First, writing a grammar for C is harder than it looks. There's all that stuff in the standard, and then there's

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

五迷三道 提交于 2019-11-28 05:00:55
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 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) Can anyone think of how the visitor pattern would visit this tree to produce output: 5 + 2 * 444 Thanks, Boda Cydo. Wikipedia has a great

Getting AST for C++?

感情迁移 提交于 2019-11-28 02:54:22
问题 I'm looking to get an AST for C++ that I can then parse with an external program. What programs are out there that are good for generating an AST for C++? I don't care what language it is implemented in or the output format (so long as it is readily parseable). My overall goal is to transform a C++ unit test bed to its corresponding C# wrapper test bed. 回答1: You can use clang and especially libclang to parse C++ code. It's a very high quality, hand written library for lexing, parsing and