abstract-syntax-tree

Simple example of how to use ast.NodeVisitor?

自古美人都是妖i 提交于 2019-12-17 15:22:35
问题 Does anyone have a simple example using ast.NodeVisitor to walk the abstract syntax tree in Python 2.6? The difference between visit and generic_visit is unclear to me, and I cannot find any example using google codesearch or plain google. 回答1: ast.visit -- unless you override it in a subclass, of course -- when called to visit an ast.Node of class foo , calls self.visit_foo if that method exists, otherwise self.generic_visit . The latter, again in its implementation in class ast itself, just

IronPython compile() does not accept AST object

房东的猫 提交于 2019-12-14 03:59:38
问题 In the documentation it says, 'source' can be either str or AST object When trying to compile my ast root: dl = compile(newRoot, '<string>', 'eval') I get this Exception: expected str, got Module I am using the last version of IronPython. Is there an idea why this does not work? all the examples I found seem to do it this way with no issues. Is there a workaround to compile an AST object? Thanks!!!! PD: I found this issue but seems to have no activity: http://ironpython.codeplex.com/workitem

Traversal of tokens using ParserRuleContext in listener - ANTLR4

独自空忆成欢 提交于 2019-12-14 03:48:57
问题 While iterating over the tokens using a Listener, I would like to know how to use the ParserRuleContext to peek at the next token or the next few tokens in the token stream? In the code below I am trying to peek at all the tokens after the current token till the EOF: @Override public void enterSemicolon(JavaParser.SemicolonContext ctx) { Token tok, semiColon = ctx.getStart(); int currentIndex = semiColon.getStartIndex(); int reqInd = currentIndex+1; TokenSource tokSrc= semiColon

How to find whether a member variable is used in a method using code in eclipse jdt?

拜拜、爱过 提交于 2019-12-13 21:16:29
问题 I have to find all the methods in a class that use a particular member variable. (like "References" in eclipse but I want to implement using code...)I use AST visitor pattern that visits FieldDeclaration to get the name and type of all the member variables. I also use visitor pattern that visits MethodDeclaration nodes to get the content of each method using getBody(). Now I have the field variable name, type and member method details. I thought I can use a string search on the content of

How to generate stable id for AST nodes in functional programming?

回眸只為那壹抹淺笑 提交于 2019-12-13 14:05:24
问题 I want to substitute a specific AST node into another, and this substituted node is specified by interactive user input. In non-functional programming, you can use mutable data structure, and each AST node have a object reference, so when I need to reference to a specific node, I can use this reference. But in functional programming, use IORef is not recommended, so I need to generate id for each AST node, and I want this id to be stable , which means: when a node is not changed, the

C source code AST parser in Java [closed]

蹲街弑〆低调 提交于 2019-12-13 12:21:52
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I'm looking for an C source code parser that can create a comprehensive AST from it. Preferably a Java library (I'd rather not have to use Python here -> http://code.google.com/p/pycparser/) 回答1: It may well be easiest for you to use ANTLR and get it to generate an AST based on an existing ANTLR grammar, e.g.

python: get the abstract syntax tree of imported function?

ⅰ亾dé卋堺 提交于 2019-12-13 12:00:23
问题 Let's say I've already imported a python module in the interpreter. How can I get the abstract syntax tree of the imported module (and any functions and classes within it) within the interpreter? I don't want to have to re-parse the source files. Thanks! 回答1: Maybe you find some inspiration in this recipe: http://code.activestate.com/recipes/533146-ast-pretty-printer/ A function that outputs a human-readable version of a Python AST. Or use compiler combined with inspect (which, of course,

How to generate function definitions from ANTLR AST in java?

耗尽温柔 提交于 2019-12-13 09:34:10
问题 I have used Python3 grammar and have built an AST. My source string is just a small function of Python. My code is as follows: public class Main { public static void main(String[] args) { String source = "def sum( arg1, arg2 ):\r\n" + " total = arg1 + arg2\r\n" + " print \"Inside the function : \", total\r\n" + " return total;\n"; Python3Lexer lexer = new Python3Lexer(CharStreams.fromString(source)); Python3Parser parser = new Python3Parser(new CommonTokenStream(lexer)); ParseTreeWalker

Abstract Syntax Tree for this case?

北城以北 提交于 2019-12-13 08:09:38
问题 I am trying to create an abstract syntax Tree for the following 2 C/C++/Java code pieces: 1) return j++-200*20-++A*7 2) return j++-200*20-A++*7 Can someone please explain their difference when it comes to their AST? 回答1: return j++ - 200*20 - ++A*7 and return j++ - 200*20 - A++*7 will have identical ASTs down to the node involving A . The first will have a pre-increment A node, while the second will have a post-increment A node. 来源: https://stackoverflow.com/questions/40700677/abstract-syntax

How to modify an AST from YOSYS? And how to synthesis a modified AST to Verilog code?

ⅰ亾dé卋堺 提交于 2019-12-13 06:12:42
问题 We know that we can get AST textfile of Verilog code. Now I want to modify the AST to get some new features, Is ANTLR right for this job,or which software should I use? Or How should I do? Then, I want to synthesis the modified AST to generate Verilog code? Can YOSYS finish this Job? What should I do? Can you tell me in detail? Thanks for your help! 回答1: ANTLR parses, but is not particularly good at supporting modifications to the AST or regenerating the source code accurately. Our DMS