Python Best Practices: Abstract Syntax Trees

Modifying Abstract Syntax Trees

I would like to be able to build and modify an ast and then optionally write it out as python byte code for execution later without overhead.

I have been hacking around with the ast docs for python3.0 and python2.6, but I can't seem to find any good sources on best practices for this type of code.


What are some best practices and guidelines for modifying abstract syntax trees in python?


Unknown states that byteplay is a good example of such a library.

Also, benford cites GeniuSQL which uses abstract syntax trees to transform python code to SQL.

-------------Problems Reply------------

Other than the manual and the source code, you are on your own. This subject and python bytecode are very undocumented.

Alternatively you could try using this python bytecode library which I have heard good thing about but haven't tried it yet:

I think geniusql is doing something along those lines to translate an ast into sql... There was a talk on it but I can't find it - and I'm not allowed to link anyway :-(

Category:python Views:0 Time:2009-05-26

Related post

  • python: get the abstract syntax tree of imported function? 2011-01-27

    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. Tha

  • Library for programming Abstract Syntax Trees in Python 2009-12-23

    I'm creating a tree to represent a simple language. I'm very familiar with Abstract Syntax Trees, and have worked on frameworks for building and using them in C++. Is there a standard python library for specifying or manipulating arbitrary ASTs? Fail

  • Standard format for concrete and abstract syntax trees 2009-02-17

    I have an idea for a hobby project which performs some code analysis and manipulation. This project will require both the concrete and abstract syntax trees of a given source file. Additionally, bi-directional references between the two trees would b

  • How to construct an abstract syntax tree 2009-11-12

    I have a general idea of what an AST is, but I want to know how to construct one. If you're given a grammar and a parse tree, how do you construct the AST? How do you do it if you're given a grammar and an expression? --------------Solutions---------

  • What are synthesized attributes in the context of creating an abstract syntax tree? 2011-04-24

    Compilers parse source code and build an abstract syntax tree. The functions used to construct an abstract syntax tree return pointers which constitute synthesized attributes. What are they and how do they differ from inherited attributes.? edit: I d

  • Eclipse Abstract Syntax Tree Programmatic Access 2008-10-13

    Could you provide an example of accessing the Eclipse Abstract Syntax Tree programmatically for a given piece of code? eg getting the AST for: package parseable; public class Class1 { /** * @param args */ public static void main(String[]

  • What is the difference between an Abstract Syntax Tree and a Concrete Syntax Tree? 2009-12-11

    I've been reading a bit about how interpreters/compilers work, and one area where I'm getting confused is the difference between an AST and a CST. My understanding is that the parser makes a CST, hands it to the semantic analyzer which turns it into

  • Runtime error using the Eclipse Abstract Syntax Tree 2009-12-14

    I'm trying to use AST parser in a non-plugin environment. The code compiles, but I get the following runtime error: Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/core/resources/IResource at org.eclipse.jdt.core.dom.ASTParser.

  • string to abstract syntax tree 2009-12-29

    I would like to convert a string containing a valid Erlang expression to its abstract syntax tree representation, without any success so far. Below is an example of what I would like to do. After compiling, alling z:z(). generates module zed, which b

  • Representing C#3 code as an Abstract Syntax Tree in XML 2010-05-05

    I was wondering if there is something similar to GCC_XML for C#3; basically a way to represent a program's entire syntactic structure in XML. Once the representation is created, I was hoping to parse it into an XDocument and interpret or query it fro

  • What's the use of abstract syntax trees? 2010-10-05

    I am learning on my own about writing an interpreter for a programming language, and I have read about Abstract Syntax Trees. I have an idea of what they are, but I do not see their use. Why are ASTs useful? --------------Solutions------------- They

  • Recursive descent parsing and abstract syntax trees 2011-03-29

    I'm hard-coding a recursive decent parser, mostly for learning purposes and, I've run into some trouble. I'll use this short excerpt from the CSS3 grammar as an example: simple_selector = type_selector | universal; type_selector = [ namespace_prefix

  • What's the difference between parse trees and abstract syntax trees? 2011-05-11

    I found the two terms in a compiler design book, and I'd like to know what each stands for, and how they are different. I searched on the internet and found that parse trees are also called concrete syntax trees (CSTs). --------------Solutions-------

  • Abstract-syntax tree for subset of C 2011-05-24

    For teaching purpose we are building a javascript step by step interpreter for (a subset of) C code. Basically we have : int,float..., arrays, functions, for, while... no pointers. The javascript interpreter is done and allow us to explain how a bool

  • Would would an AST (abstract syntax tree) for an object-oriented programming language look like? 2011-06-16

    I'm reading about AST (abstract syntax trees) but all the samples I see use expressions such as: a + b * c Which could be represented in a lispy like syntax as: (+ a (* b c) ) Which will be the equivalent to: + / \ a * / \ b c My question is How a AS

  • How to reflect modified abstract syntax tree in JDT back to original java source file.? 2011-08-07

    Kindly refer "write it down" section of article I am parsing a java source code file which has method with contracts written using cofoja. Now when I create

  • Generating an Abstract Syntax Tree for java source code using ANTLR 2012-02-05

    How Can I Generate an AST from java src code Using ANTLR? any help? --------------Solutions------------- OK, here is the steps: go to ANTLR Site and download the latest version now it's 3.4 Download the Java.g and the JavaTreeParser.g files from here

  • Alternative to Eclipse's Abstract Syntax Tree parser for code manipulation 2012-02-20

    Background I am writing a program that will do some bulk renaming of members and functions in a directory of java source code to de-obfuscate the code based on a look-up table .csv file passed in to the program. What this is for is the source code I

  • Semantic Rules / Abstract Syntax Tree Rules 2012-03-05

    First of all, are the Semantic Rules and Abstract Syntax Tree Rules the same? Now, if i have a language specifications, and i have CFG, then how do i go about constructing Abstract Syntax Tree Rules. Any source is appreciated. Thanks. --------------S

Copyright (C), All Rights Reserved.

processed in 0.070 (s). 11 q(s)