abstract-syntax-tree

Downcast traits inside Rc for AST manipulation

邮差的信 提交于 2019-11-27 06:38:11
问题 I'm trying to manipulate ASTs in Rust. There will be lots of manipulations, and I want my trees to be immutable, so to save time all references will be Rc s. My tree nodes will look like this: enum Condition { Equals(Rc<Expression>, Rc<Expression>), LessThan(Rc<Expression>, Rc<Expression>), ... } enum Expression { Plus(Rc<Expression>, Rc<Expression>), ... } I want to replace a random node of a given type with another node of the same type. To do generic operations on trees I've made a trait:

VariableDeclarationFragment node resolveBindind() returns null in eclipse/jdt/ast

社会主义新天地 提交于 2019-11-27 04:51:22
问题 I'm trying to try out eclipse jdt/ast following this article. This is the java code as an input: class Hello { int hello() { int a = 0, b = 3; /* hello */ { b = a * 3; } return a; } public static void main(String[] args) { int z = 0, i = 3; /* hello */ { i = z * 3; } } } With ASTView, it shows that VariableDeclarationFragment has corresponding binding. However, in this visitor code for VariableDeclarationFragment node , I always get null value for 4 local variables (a,b,z,i) as resolveBinding

Evaluating a mathematical expression (function) for a large number of input values fast

僤鯓⒐⒋嵵緔 提交于 2019-11-27 02:49:21
问题 The following questions Evaluating a mathematical expression in a string Equation parsing in Python Safe way to parse user-supplied mathematical formula in Python Evaluate math equations from unsafe user input in Python and their respective answers made me think how I could parse a single mathematical expression (in general terms along the lines of this answer https://stackoverflow.com/a/594294/1672565) given by a (more or less trusted) user efficiently for 20k to 30k input values coming from

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

两盒软妹~` 提交于 2019-11-27 02:01:31
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 . ast.literal_eval() accepts + in the evaluated data because 5+2j (complex number * ) are valid literals. The same applies to - . To keep the code simple, no attempt is made to exclude + or - as a binary

AST interpreter?

隐身守侯 提交于 2019-11-27 01:41:43
问题 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 回答1:

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

て烟熏妆下的殇ゞ 提交于 2019-11-27 00:29:15
问题 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

How can I use the java Eclipse Abstract Syntax Tree in a project outside Eclipse? (ie not an eclipse plugin)

99封情书 提交于 2019-11-26 22:36:05
问题 How can I use the java Eclipse Abstract Syntax Tree in a project outside Eclipse? (ie not an eclipse plugin) All the Eclipse AST examples that I've seen are for eclipse plugins. Is there a way (ie an example) of a project that uses the eclipse AST for a non-eclipse project. 回答1: Below is the code I used to do this given a Java 1.5 file. I'm very new to this and spent today browsing around, and trying things out to get the code below working. public void processJavaFile(File file) { String

boost::spirit access position iterator from semantic actions

一曲冷凌霜 提交于 2019-11-26 21:17:02
问题 Lets say I have code like this (line numbers for reference): 1: 2:function FuncName_1 { 3: var Var_1 = 3; 4: var Var_2 = 4; 5: ... I want to write a grammar that parses such text, puts all indentifiers (function and variable names) infos into a tree (utree?). Each node should preserve: line_num, column_num and symbol value. example: root: FuncName_1 (line:2,col:10) children[0]: Var_1 (line:3, col:8) children[1]: Var_1 (line:4, col:9) I want to put it into the tree because I plan to traverse

How does a simple calculator with parentheses work?

旧城冷巷雨未停 提交于 2019-11-26 19:52:17
问题 I want to learn how calculators work. For example, say we have inputs in infix notation like this: 1 + 2 x 10 - 2 The parser would have to respect common rules in math. In the above example this means: 1 + (2 x 10) - 2 = 19 (rather than 3 x 10 - 2 = 28) And then consider this: 1 + 2 x ((2 / 9) + 7) - 2 Does it involve an Abstract Syntax Tree? A binary tree? How is the order of operations ensured to be mathematically correct? Must I use the shunting-yard algorithm to convert this to postfix

What is JavaScript AST, how to play with it?

强颜欢笑 提交于 2019-11-26 18:58:06
问题 Abstract Syntax Tree.. I always heard that compile to SpiderMonkey AST on Github. So, is that a actual standard of JS syntax tree? And there's V8, is V8 using the same kind of AST? How can I play with that? 回答1: SpiderMonkey offers the parser api. This is probably the easiest way to get your hands on the syntax objects. There's also open js-js parsers like Esprima (which is ECMAScript, really, but it's right up the alley) 回答2: 1.You can take a look at AST explorer. An online tool to explore