abstract-syntax-tree

Parse a .py file, read the AST, modify it, then write back the modified source code

假装没事ソ 提交于 2019-11-26 00:35:36
问题 I want to programmatically edit python source code. Basically I want to read a .py file, generate the AST, and then write back the modified python source code (i.e. another .py file). There are ways to parse/compile python source code using standard python modules, such as ast or compiler. However, I don\'t think any of them support ways to modify the source code (e.g. delete this function declaration) and then write back the modifying python source code. UPDATE: The reason I want to do this

How to output the AST built using ANTLR?

∥☆過路亽.° 提交于 2019-11-26 00:33:09
问题 I\'m making a static analyzer for C. I have done the lexer and parser using ANTLR in which generates Java code. Does ANTLR build the AST for us automatically by options {output=AST;} ? Or do I have to make the tree myself? If it does, then how to spit out the nodes on that AST? I am currently thinking that the nodes on that AST will be used for making SSA, followed by data flow analysis in order to make the static analyzer. Am I on the right path? 回答1: Raphael wrote: Does antlr build the AST

get human readable AST from c++ code

狂风中的少年 提交于 2019-11-25 23:50:33
问题 In order to get a better understanding of some of the details of the C++ language and grammer, I would love to be able to write a small C++ program, and see the AST that a compiler generates from that. It looks like clang had this feature in the past (-emit-asm), but it has removed. Is there an easy way to do this today? 回答1: clang still has that functionality: The commands are -ast-dump and -ast-dump-xml Note: -ast-dump-xml will only work when you build clang in debug mode. http://clang.llvm

What kinds of patterns could I enforce on the code to make it easier to translate to another programming language? [closed]

空扰寡人 提交于 2019-11-25 22:45:10
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . I am setting out to do a side project that has the goal of translating code from one programming language to another. The languages I am starting with are PHP and Python (Python to PHP should be easier to start with), but ideally I would be able to add other languages with

Compiling an AST back to source code

ぃ、小莉子 提交于 2019-11-25 22:39:55
问题 I\'m currently in the process of building a PHP Parser written in PHP, as no existing parser came up in my previous question. The parser itself works fairly well. Now obviously a parser by itself does little good (apart from static analysis). I would like to apply transformations to the AST and then compile it back to source code. Applying the transformations isn\'t much of a problem, a normal Visitor pattern should do. What my problem currently is, is how to compile the AST back to source.

Using python's eval() vs. ast.literal_eval()?

醉酒当歌 提交于 2019-11-25 22:20:12
问题 I have a situation with some code where eval() came up as a possible solution. Now I have never had to use eval() before but, I have come across plenty of information about the potential danger it can cause. That said, I\'m very wary about using it. My situation is that I have input being given by a user: datamap = raw_input(\'Provide some data here: \') Where datamap needs to be a dictionary. I searched around and found that eval() could work this out. I thought that I might be able to check