ast

Abstract Syntax Tree

I have an AST derived from the ANTLR Parser Generator for Java. What I want to do is somehow construct a control flow graph of the source code, where each statement or expression is a unique Node. I understand there must be some recursiveness to this identification, I was wondering what you would suggest as the best option and if ANTLR h...

javac.exe AST programmatic access example

Is it possible to access the Abstract Syntax Tree(AST) inside the javac.exe programmatically? Could you provide an example? ...

Eclipse Abstract Syntax Tree Programmatic Access

Could you provide an example of accessing the Eclipse Abstract Syntax Tree programmatically for a given piece of code? eg getting the AST for: Class1.java package parseable; public class Class1 { /** * @param args */ public static void main(String[] args) { System.out.println("Hello world!"); } } ...

AST from C code

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 suggest a better tool/applica...

Coalescing regular expressions in PHP

Suppose I have the following two strings containing regular expressions. How do I coalesce them? More specifically, I want to have the two expressions as alternatives. $a = '# /[a-z] #i'; $b = '/ Moo /x'; $c = preg_magic_coalesce('|', $a, $b); // Desired result should be equivalent to: // '/ \/[a-zA-Z] |Moo/' Of course, doing this as ...

What should my AST look like for easy transforms?

I have a minimal toy language similar to javascript. I generate an AST to try out some optimization techniques like escape analysis, type inference. I tried a few approaches like generalizing operator tokens instead of a class/function for each one, keeping type information on every node... But I still don't feel like I am going anywhere...

Can I use AST transformations in Groovy to extend its syntax?

I have seen examples of how you can use the Groovy AST transformations to extend the language, e.g. to log before and after a method call as shown here. However, would it also be possible to use this framework to extend the syntax of the language itself? For instance, what if I wanted to be able to parse and transform the following into ...

Cannot get Groovy AST example to work

So, I am trying to learn how to use (and extend) Groovy, and I am following the example from this page. Basically, it shows how to define an annotation for Groovy code that lets you hook in to the compiler process. The example revolves around writing and annotation that will cause lines to be printed before and after method calls. My c...

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

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

Writing a TemplateLanguage/VewEngine

Aside from getting any real work done, I have an itch. My itch is to write a view engine that closely mimics a template system from another language (Template Toolkit/Perl). This is one of those if I had time/do it to learn something new kind of projects. I've spent time looking at CoCo/R and ANTLR, and honestly, it makes my brain hurt,...

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

How to Represent Classes in an Abstract Syntax Tree Based Interpreter

I have read the related questions, but none of them appears to address the question directly. I am working on writing a PHP script interpreter. I have the AST generating proper nodes for everything except classes. Handling classes is a bit different than handling functions, so I am looking for how to handle classes that are standalone...

How can I obtain the full AST in Python?

I like the options offered by the _ast module, it's really powerful. Is there a way of getting the full AST from it? For example, if I get the AST of the following code : import os os.listdir(".") by using : ast = compile(source_string,"<string>","exec",_ast.PyCF_ONLY_AST) the body of the ast object will have two elements, an i...

How to manipulate C# AST?

I am working on a Reverse Engineering school project, which requires to translate manipulate AST of compiled C# project. I have seen the post on "Translate C# code into AST?" in this website, but it doesn't look like the one I am looking for. According to what I knew, currently C# doesn't provide a library class that does sth like that f...

ASTVisitor in Eclipse

Hi, I am learning how to do an ASTVisitor for a project, so I started using Eclipse, which has a comprehensive API for that. I have downloaded the Packed Example Project from this website: http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation%5FAST/index.html But I realised that the codes do not have a main() m...

Microsoft CCI - resources, references for writing compilers

Some time ago, I was working on compiler, I've used System.Reflection to generate code (IL) from my AST. Now, I've an idea for another compiler that I'd like to work on (it will be another pet project, nothing that will be used in production code, at least, not now). As you know, pet projects have one big advantage over production cod...

What is System.Linq.Expressions in C# used for?

Is LINQ a new feature in .NET 4.0? Older versions like .NET 3.5 doesn't have? I am interested to know about this as it seemed to be somehow related to my the project I am working on. What is LINQ useful for? It seemed to be able to build Expression Tree. What is an actually Expression Tree? Is LINQ able to extract info like class, met...

What is the max LINQ Expression Trees can do?

What is the maximun that LINQ expression Tree can do? Can it define a class? How about a method, with all the declared name, modifiers, parametertype and return type? Must the program always define the tree himself? Is it possible to generate the tree from a given C# file? Where can I get resources to learn about writing basic to adv...

transforming Jython's source / ast

I've got a problem to solve in Jython. The function I've got looks like this: ok = whatever1(x, ...) self.assertTrue("whatever1 failed: "+x...(), ok) ok = whatever2(x, ...) self.assertTrue("whatever2 failed: "+x...(), ok) [ many many lines ] ... There are many tests that look like this, they contain mostly ok=... tests, but there ar...

How do you turn an unquoted Python function/lambda into AST? 2.6

This seems like it should be easy, but I can't find the answer anywhere - nor able to derive one myself. How do you turn an unquoted python function/lambda into an AST? Here is what I'd like to be able to do. import ast class Walker(ast.NodeVisitor): pass # ... # note, this doesnt work as ast.parse wants a string tree = ast...