abstract-syntax-tree

Visiting nodes in a syntax tree with Python ast module

╄→гoц情女王★ 提交于 2019-12-03 08:40:20
问题 I'm playing with python ast (abstract syntax tree). I wrote the following and it visited all nodes of the AST. import ast class Py2Neko(ast.NodeVisitor): def generic_visit(self, node): print type(node).__name__ ast.NodeVisitor.generic_visit(self, node) def visit_Name(self, node): print 'Name :', node.id def visit_Num(self, node): print 'Num :', node.__dict__['n'] def visit_Str(self, node): print "Str :", node.s if __name__ == '__main__': node = ast.parse("a = 1 + 2") print ast.dump(node) v =

Generating an Abstract Syntax Tree for java source code using ANTLR

喜夏-厌秋 提交于 2019-12-03 08:40:05
问题 How Can I Generate an AST from java src code Using ANTLR? any help? 回答1: The setps to generate java src AST using antlr4 are: Install antlr4 you can use this link to do that. After installation download the JAVA grammar from here. Now generate Java8Lexer and Java8Parser using the command: antlr4 -visitor Java8.g4 This will generate several files such as Java8BaseListener.java Java8BaseVisitor.java Java8Lexer.java Java8Lexer.tokens Java8Listener.java Java8Parser.java Java8.tokens Java8Visitor

How to extract functions used in a python code file?

二次信任 提交于 2019-12-03 08:28:39
I would like to create a list of all the functions used in a code file. For example if we have following code in a file named 'add_random.py' ` import numpy as np from numpy import linalg def foo(): print np.random.rand(4) + np.random.randn(4) print linalg.norm(np.random.rand(4)) ` I would like to extract the following list: [numpy.random.rand, np.random.randn, np.linalg.norm, np.random.rand] The list contains the functions used in the code with their actual name in the form of 'module.submodule.function'. Is there something built in python language that can help me do this? You can extract

Using the Eclipse AST

谁说我不能喝 提交于 2019-12-03 07:50:45
问题 I have recently come into the need of modifying some Java code (adding methods, changing the signatures of some fields and removing methods) and I think that all of this can be accomplished though the use of the Eclipse SDK's AST. I know from some research how to parse in a source file but I don't know how to do the things mentioned above. Does anyone know a good tutorial or could someone give me a brief explanation on how to resolve these issues? Thanks a lot, ExtremeCoder Edit: I have now

Python ast to dot graph

一个人想着一个人 提交于 2019-12-03 07:44:03
问题 I'm analyzing the AST generated by python code for "fun and profit", and I would like to have something more graphical than "ast.dump" to actually see the AST generated. In theory is already a tree, so it shouldn't be too hard to create a graph, but I don't understand how I could do it. ast.walk seems to walk with a BFS strategy, and the visitX methods I can't really see the parent or I don't seem to find a way to create a graph... It seems like the only way is to write my own DFS walk

Access the Abstract Syntax Tree of V8 Engine

无人久伴 提交于 2019-12-03 07:42:39
问题 Is it possible to access the AST of the v8 engine, for a given JavaScript code? I'm working on a JavaScript Static Analyzer using V8 engine. 回答1: This is pretty old but maybe the answer helps someone stumbling upon this. The answer is yes, assuming you are willing to modify V8 and compile your own version of it. If so, then in compiler.cc you find a spot where MakeCode is called throughout MakeFunctionInfo which transforms the AST that is stored in the passed in CompilationInfo object into

Modify existing yaml file and add new data and comments

若如初见. 提交于 2019-12-03 06:57:34
I recently saw that the go yaml lib has new version (V3) with the nodes capabilities (which in my opinion is a killer feature :) ) which can helps a lots with modifying yamls without changing the structure of the file But since it is fairly new (from last week ) I didn't find some helpful docs and example for the context which I need (add new object/node and to keep the file structure the same without removing the comments etc) what I need is to manipulate yaml file for example lets say I’ve this yaml file version: 1 type: verbose kind : bfr # my list of applications applications: - name: app1

How can we get the Syntax Tree of TypeScript?

别等时光非礼了梦想. 提交于 2019-12-03 06:53:30
问题 Is there a process on getting a syntax tree of a compiler. We had been assigned on a project that needs to access typescript's syntax tree (which is opensource so we could see the whole compiler's code). But we don't know how to get it. I've been reading some articles in the Internet but I can't really find a user-friendly article or which is written in lehman's term. I believe some mentioned that the first step we need to do is to find the parsing step. But after that we had no idea what to

How can I reuse definition (AST) subtrees in a macro?

筅森魡賤 提交于 2019-12-03 05:56:47
I am working in a Scala embedded DSL and macros are becoming a main tool for achieving my purposes. I am getting an error while trying to reuse a subtree from the incoming macro expression into the resulting one. The situation is quite complex, but (I hope) I have simplified it for its understanding. Suppose we have this code: val y = transform { val x = 3 x } println(y) // prints 3 where 'transform' is the involved macro. Although it could seem it does absolutely nothing, it is really transforming the shown block into this expression: 3 match { case x => x } It is done with this macro

Parsing SQL like syntax, design pattern

人盡茶涼 提交于 2019-12-03 05:45:54
I am trying mock sql syntax to build a simple sql like interface to a key-value storage. The values are essentially POJOs An example would be select A.B.C from OBJ_POOL where A.B.X = 45 AND A.B.Y > '88' AND A.B.Z != 'abc'; OBJ_POOL is just a list of POJOs of the same class. In this example A would be the base class. Class A Class B String C Integer X String Y String Z Now A.B.C is equivalent A.getB().getC() I am using Antlr to parse the above statement to get an AST, and then hoping to use Apache BeanUtils to reflectively get/set the field names. I wrote the grammar thats builds an AST Now I