abstract-syntax-tree

How to convert source code to a xml based representation of the ast?

穿精又带淫゛_ 提交于 2019-12-03 13:29:52
问题 i wanna get a xml representation of the ast of java and c code. 3 months ago, i asked this question yet but the solutions weren't comfortable for me srcml seems to be a good solution for this problem but it does not support line numbers and columns but i need that feature. about elsa: cite: "There is ongoing effort to export the Elsa AST as an XML document; we expect to be able to advertise this in the next public release." dms... didn't understand that. especially for java, there is javaml

How do you do a python 'eval' only within an object context?

自作多情 提交于 2019-12-03 13:10:10
Is it possible to do something like c = MyObj() c.eval("func1(42)+func2(24)") in Python..i.e. have func1() and func2() be evaluated within the scope of the object 'c' (if they were member functions within that class definition)? I can't do a simple parsing, since for my application the eval strings can become arbitrarily complicated. I guess doing some magic with the ast module might do the trick, but due to the dirth of literature on ast, I'm not sure where to look: import ast class MyTransformer(ast.NodeTransformer): def visit_Name(self, node): # do a generic_visit so that child nodes are

How do I pretty-print productions and line numbers, using ANTLR4?

纵饮孤独 提交于 2019-12-03 12:53:38
I'm trying to write a piece of code that will take an ANTLR4 parser and use it to generate ASTs for inputs similar to the ones given by the -tree option on grun ( misc.TestRig ). However, I'd additionally like for the output to include all the line number/offset information. For example, instead of printing (add (int 5) '+' (int 6)) I'd like to get (add (int 5 [line 3, offset 6:7]) '+' (int 6 [line 3, offset 8:9]) [line 3, offset 5:10]) Or something similar. There aren't a tremendous number of visitor examples for ANTLR4 yet, but I am pretty sure I can do most of this by copying the default

Building own C# compiler using ANTLR: Compilation Unit

China☆狼群 提交于 2019-12-03 12:40:52
问题 // Create a scanner that reads from the input stream passed to us CSLexer lexer = new CSLexer(new ANTLRFileStream(f)); tokens.TokenSource = lexer; // Create a parser that reads from the scanner CSParser parser = new CSParser(tokens); // start parsing at the compilationUnit rule CSParser.compilation_unit_return x = parser.compilation_unit(); object ast = x.Tree; What can I do with the x which is of compilation_unit_return type, to extract its root, its classes, its methods etc? Do I have to

What are synthesized attributes in the context of creating an abstract syntax tree?

徘徊边缘 提交于 2019-12-03 12:08:55
Compilers parse source code and build an abstract syntax tree. The functions used to construct an abstract syntax tree return pointers which constitute synthesized attributes . What are they and how do they differ from inherited attributes .? edit: I don't know if this can help, but I originally heard of these terms in a French context: Attributs synthétisés, attributs hérités. Ira Baxter Attributes are additional values associated with something of central interest. In the case of ASTs, you can think of them as pairs (attribute_name, attribute_value) associated with each AST node, where the

Difference between @Delegate and @Mixin AST transformations in Groovy

无人久伴 提交于 2019-12-03 10:39:58
What's the difference between @Delegate and @Mixin AST transformations in Groovy. Maybe my question has to do with OO and when apply different patterns, but I use both and I can achieve the same behavior. class Person { String name = "Clark" def walk() { "Walk" } } @Mixin(Person) class Superhero { def fly() { "Fly" } } def superman = new Superhero() assert superman.name == "Clark" assert superman.walk() == "Walk" assert superman.fly() == "Fly" class Person { String name = "Clark" def walk() { "Walk" } } class Superhero { @Delegate Person person def fly() { "Fly" } } def superman = new

Pretty Printing AST with Minimal Parentheses

落花浮王杯 提交于 2019-12-03 09:34:44
问题 I'm implementing a pretty-printer for a JavaScript AST and I wanted to ask if someone is aware of a "proper" algorithm to automatically parenthesize expressions with minimal parentheses based on operator precedence and associativity. I haven't found any useful material on the google. What seems obvious is that an operator whose parent has a higher precedence should be parenthesized, e.g.: (x + y) * z // x + y has lower precedence However, there are also some operators which are not

Navigating and modifying ASTs built on the Free monad in Haskell

半城伤御伤魂 提交于 2019-12-03 09:28:40
问题 I'm attempting to structure an AST using the Free monad based on some helpful literature that I've read online. I have some questions about working with these kinds of ASTs in practice, which I've boiled down to the following example. Suppose my language allows for the following commands: {-# LANGUAGE DeriveFunctor #-} data Command next = DisplayChar Char next | DisplayString String next | Repeat Int (Free Command ()) next | Done deriving (Eq, Show, Functor) and I define the Free monad

What are motivations behind compiling to byte-code?

与世无争的帅哥 提交于 2019-12-03 09:13:15
问题 I'm working on my own toy programming language. For now I'm interpreting the source language from AST and I'm wondering what advantages compiling to a byte-code and then interpreting it could provide me. For now I have three things in mind: Traversing the syntax tree hundreds of time may be slower than running instructions in an array, especially if the array support O(1) random access(ie. jumping 10 instructions up and down). In typed execution environment, I have some run-time costs because

Generate an AST in C++

北城余情 提交于 2019-12-03 08:48:59
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 the tokens into the parse tree in the correct order. I'm not sure whether or not to go top-down, left-right or bottom-up, right-left. Can anyone provide me with a simple LALR(1) algorithm? I'm going to go against conventional wisdom here and say that you should build your AST manually with natural language-specific data-structures. A generic "AST