Monday, February 1, 2010

AST example: Itsy bitsy teeny weeny compiler

Here is a very very simple example of  building (and then using) an AST. 

tiny.cup parses a very simple language of assignment statements with expressions as right-hand sides.  It builds a simple AST,which is passed back to the driver.

The driver (Tiny.java) obtains the AST from the parser, and hands it off to an evaluator. The evaluator emits simple stack code (in the form of pseudocode ... but you can see how you could modify this to be either real code in a language like C or Java or instructions to a stack-machine interpreter).

No comments:

Post a Comment