abstract-syntax-tree

Understanding -fdump-tree output gcc with GraphViz

天涯浪子 提交于 2021-02-07 03:08:24
问题 I've created a tree dump how described here: How can I dump an abstract syntax tree generated by gcc into a .dot file? for this dummy script: int fact(int n) { if (n<=1) { return 1; } return n * fact(n-1); } int main(void) { int a = 4; int res = fact(a); return res; } And the image what I've got: As I know gcc is not the best way to learn AST representation. But anyway it would be nice to understand what image's content means. Especially what % sign here means and a FREQ:0 statement? 回答1: The

How do I work with the AST in Irony now?

北城余情 提交于 2021-02-07 02:41:33
问题 I have a grammar that works and parses in the Irony console just fine, but I don't get anything in the AST treeview. I was following along with the BASIC->Javascript article found here: http://www.codeproject.com/Articles/25069/JSBasic-A-BASIC-to-JavaScript-Compiler, but it seems that the Ast stuff has all been moved/removed. I found the Irony.Interpreter .dll, which has some Ast stuff in it, but it seems all tied up in the Expression sample implementation. What am I missing here? I want to

Clang AST Interpretation

独自空忆成欢 提交于 2021-02-05 09:28:16
问题 I am trying to interpret parts of the Clang AST you can see in the picture below. In short I am trying to do is to check if two variables are the same at different program points. After inspecting the AST, I noticed that the only commonality between the AST sections are the sections circled in blue. Can anyone help me as to what these hex numbers correspond to in the AST? I understand that the first block corresponds to a Variable Declaration and the second block corresponds to a Expression.

How to find out if (the source code of) a function contains a loop?

允我心安 提交于 2021-02-04 14:51:26
问题 Let's say, I have a bunch of functions a , b , c , d and e and I want to find out if they directly use a loop: def a(): for i in range(3): print(i**2) def b(): i = 0 while i < 3: print(i**2) i += 1 def c(): print("\n".join([str(i**2) for i in range(3)])) def d(): print("\n".join(["0", "1", "4"])) def e(): "for" I want to write a function uses_loop so I can expect these assertions to pass: assert uses_loop(a) == True assert uses_loop(b) == True assert uses_loop(c) == False assert uses_loop(d)

How to find out if (the source code of) a function contains a loop?

匆匆过客 提交于 2021-02-04 14:51:26
问题 Let's say, I have a bunch of functions a , b , c , d and e and I want to find out if they directly use a loop: def a(): for i in range(3): print(i**2) def b(): i = 0 while i < 3: print(i**2) i += 1 def c(): print("\n".join([str(i**2) for i in range(3)])) def d(): print("\n".join(["0", "1", "4"])) def e(): "for" I want to write a function uses_loop so I can expect these assertions to pass: assert uses_loop(a) == True assert uses_loop(b) == True assert uses_loop(c) == False assert uses_loop(d)

Python Libcst: Unable to generate code from the node in visitor class

你。 提交于 2021-01-29 05:14:33
问题 I am searching for a way to get code from the node in the visitor. Example: import libcst code_example = """ from ast import parse threshold = 1 print(threshold) """ class CodeVisitor(libcst.CSTVisitor): def visit_Assign(self, node: libcst.Assign) -> bool: print(node) return True demo = libcst.parse_module(code_example) demo.visit(CodeVisitor()) In the above code I want to get the code(i.e. threshold = 1) of the node . But it seems like libcst doesn't provide that support. I further looked

Generate ast from constituent elements without using ast.parse in python

二次信任 提交于 2021-01-28 08:58:51
问题 Using the python ast module, it is possible to generate a simple abstract syntax tree as follows: import ast module = ast.parse('x=3') This generates a Module object for which the source code can be retrieved using the astor library as follows: import astor astor.to_source(module) Generating an output of 'x = 3\n' Is it possible to constitute the exact same module object from its constituent elements without using the ast.parse method such that the astor.to_source method can generate the same

how to write a Python debugger/editor

本小妞迷上赌 提交于 2021-01-28 08:53:59
问题 Sorry for the kind of general question. More details about what I want: I want the user to be able to write some Python code and execute it. Once there is an exception which is not handled, I want the debugger to pause the execution, show information about the current state/environment/stack/exception and make it possible to edit the code . I only want to have the special code block editable where the exception occurred and nothing else (for now). I.e. if it occurred inside a for loop, I only

How to get AST as a list in R

浪子不回头ぞ 提交于 2021-01-28 00:26:57
问题 I have strings, which describe mathematical formulas and I would like to convert it into lists of meaningful parts. The function ast_ does know how to parse it, to displays it as an Abstract Syntax Tree but does not return the AST. I am looking for a function which does return the tree. bb <- "(media.urin_A + media.urin_B)/2" lazyeval::ast_(rlang::parse_expr(bb)) > lazyeval::ast_(rlang::parse_expr(bb)) ┗ () ┗ `/ ┗ () ┗ `( ┗ () ┗ `+ ┗ `media.urin_A ┗ `media.urin_B ┗ 2 回答1: You can construct

Apply distributive law on AST (or RPN) => disjunctive normal form

可紊 提交于 2021-01-27 20:32:52
问题 I have expressions like the following: {1000} AND ({1001} OR {1002} OR {1003}) Allowed operators are OR and AND, expressions can be nested using parenthesis. I already managed to tokenize this string and to convert it to an abstract syntax tree (AST) using the Shunting Yard algorithm, implemented in PHP 5.3. The above expression results in the following: 1000 1001 1002 | 1003 | & & / \ 1000 | / \ | 1003 / \ 1001 1002 When traversing this tree I want to output the final combinations of numbers