abstract-syntax-tree

Given an AST, is there a working library for getting the source?

帅比萌擦擦* 提交于 2019-11-29 03:57:36
Is there a way to convert a given Python abstract syntax tree (AST) to a source code? Here is a good example of how to use Python's ast module, specifically a NodeTransformer . I was looking for a way to convert the resulting AST back to source, so the changes can be inspected visually. Ned Batchelder The Python source tree contains an implementation of this: unparse.py in the Demo/parser directory: https://github.com/python/cpython/blob/master/Tools/parser/unparse.py Have a look at http://pypi.python.org/pypi/sourcecodegen/0.6.14 A nice third-party library I found: astunparse which is based

How to get source corresponding to a Python AST node?

别等时光非礼了梦想. 提交于 2019-11-29 01:34:31
Python AST nodes have lineno and col_offset attributes, which indicate the beginning of respective code range. Is there an easy way to get also the end of the code range? A 3rd party library? EDIT: Latest code (tested in Python 3.5-3.7) is here: https://bitbucket.org/plas/thonny/src/master/thonny/ast_utils.py As I didn't find an easy way, here's a hard (and probably not optimal) way. Might crash and/or work incorrectly if there are more lineno/col_offset bugs in Python parser than those mentioned (and worked around) in the code. Tested in Python 3.3: def mark_code_ranges(node, source): """

Can I get AST from live scala code?

萝らか妹 提交于 2019-11-28 23:32:59
I said "live code" because I mean not from text source files or source strings, but from partialFunctions / lambdas. (I know Ruby1.8's parseTree and C# linq can do it) consider a partialFunction f: val f = (i: Int, j: Int) => (i + j) * 2 I hope there is some tool works like this: getBodyAstFrom(f) //=> (Infix('*'), (Infix('+'), Id('i'), Id('j')), Val('2')) I don't care the semantic things (context parsing and implicits are too complex, and unnecessary for me), I just need the syntax tree from live code, is it possible? There may be issues with inspecting other people's code, but what about my

How to write Visitor Pattern for a Abstract Syntax Tree in C#?

…衆ロ難τιáo~ 提交于 2019-11-28 23:30:20
I have 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) Pattern visitor is a design pattern that allows you to implement arbitrary operations (implemented as visitors) on the parse tree( eg. Type-checking ) without

How to make use of Clang's AST?

扶醉桌前 提交于 2019-11-28 21:06:48
问题 I am looking at making use of the Clang's AST for my C code and do some analysis over the AST. Some pointers on where to start, how to obtain the Clang's AST, tutorials or anything in this regard will be of great help!!! I have been trying to find some and I got this link which was created 2 years back. But for some reason, it is not working for me. The sample code, in the tutorial, gives me too many errors. So I am not sure, if I build the code properly or some thing is wrong with the

Restricting Python's syntax to execute user code safely. Is this a safe approach?

一世执手 提交于 2019-11-28 20:51:57
Original question: Executing mathematical user code on a python web server, what is the simplest secure way? I want to be able to run user submitted code on a python webserver. The code will be simple and mathematical in nature. As such a small subset of Python is required, my current approach is to whitelist allowable syntax by traversing Python's abstract syntax tree. Functions and names get special treatment; only explicitly whitelisted functions are allowed, and only unused names. import ast allowed_functions = set([ #math library 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh',

Scala AST in Scala [closed]

我只是一个虾纸丫 提交于 2019-11-28 20:41:09
Is there a Scala library that parses Scala and creates an Abstract Syntax Tree (AST)? Ideally I am interested in a Scala library. Plan B would be a Java library. (I know I could leverage the EBNF from the Scala Syntax Summary .) Mitch Blevins I would think the best way to access the AST is with a compiler plugin. You should read a soft introduction before diving in deep . A few existing parsers: The offical Scala compiler . The IntelliJ IDEA Scala plugin has a parser written in Scala against IntelliJ's PsiBuilder API. The Scala Netbeans plugin used a parser implemented in Rats! (which

Can I get an XML AST dump of C/C++ code with clang without using the compiler?

岁酱吖の 提交于 2019-11-28 20:24:18
I managed to compile successfully clang for windows with cmake and visual studio 10. I would like to get an XML file as AST representation of the source code. There is one option that provides the result with clang with gcc under linux (ubuntu) but doesn't work on the windows box: clang -cc1 -ast-print-xml source.c However, this is invoking the compilation stage (which I would like to avoid). Digging in the source code didn't help me so far as I am quite new to clang. I could manage to generate the binary version of the AST by using: clang -emit-ast source.c Unfortunately, this format is

Translate C# code into AST?

孤街醉人 提交于 2019-11-28 19:17:42
问题 Is it currently possible to translate C# code into an Abstract Syntax Tree? Edit: some clarification; I don't necessarily expect the compiler to generate the AST for me - a parser would be fine, although I'd like to use something "official." Lambda expressions are unfortunately not going to be sufficient given they don't allow me to use statement bodies, which is what I'm looking for. 回答1: The Roslyn project is in Visual Studio 2010 and gives you programmatic access to the Syntax Tree, among

AST from C code [closed]

て烟熏妆下的殇ゞ 提交于 2019-11-28 17:33:08
问题 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 2 years ago . I want to perform some transformations on C source code. I need a tool on linux that generates a complete AST from the source code so that I can apply my transformations on this AST and then convert it back to the C source code. I tried ELSA but it is not getting compiled. (I am using Ubuntu 8.4). Can anyone