What would an AST (abstract syntax tree) for an object-oriented programming language look like?

AST is an abstraction of the CST (concrete syntax tree, or, parse tree). The concrete syntax tree is the tree resulting from the productions (in the grammar) used to parse the file. So your AST is basically derived from your grammar definition, but has for transformed Exp / | \ / | \ * Ident … Read more

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

Pythoscope does this to the test cases it automatically generates as does the 2to3 tool for python 2.6 (it converts python 2.x source into python 3.x source). Both these tools uses the lib2to3 library which is an implementation of the python parser/compiler machinery that can preserve comments in source when it’s round tripped from source … Read more

What is the difference between an Abstract Syntax Tree and a Concrete Syntax Tree?

A concrete syntax tree represents the source text exactly in parsed form. In general, it conforms to the context-free grammar defining the source language. However, the concrete grammar and tree have a lot of things that are necessary to make source text unambiguously parseable, but do not contribute to actual meaning. For example, to implement … Read more

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

I’ve been building tools (DMS Software Reengineering Toolkit) to do general purpose program manipulation (with language translation being a special case) since 1995, supported by a strong team of computer scientists. DMS provides generic parsing, AST building, symbol tables, control and data flow analysis, application of translation rules, regeneration of source text with comments, etc., … Read more

Compiling an AST back to source code

The problem of converting an AST back into source code is generally called “prettyprinting”. There are two subtle variations: regenerating the text matching the original as much as possible (I call this “fidelity printing”), and (nice) prettyprinting, which generates nicely formatted text. And how you print matters depending on whether coders will be working on … Read more