Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Compiler noob question

Name: Anonymous 2009-10-31 0:27

hai.  I'm trying to write a compiler for the first time (just a relatively simple toy language I thought up) and I'm not sure exactly how I should store the parse tree in memory.  I'm writing the compiler in C.  Any suggestions?

Name: Anonymous 2009-10-31 0:41

What you want is an AST(Abstract Syntax Tree). Each node of the tree has a type representing some instruction, and a list of parameters. Each parameter can be another node and so on.

A simpler way to explain this would be through a Lisp list:

1 + 2 * 3 => (+ 1 (* 2 3))

the first element of the list represents the instruction, while the rest are parameters.

Other decent representations can be Recursive variants in ML/Ocaml (Haskell has them too):

type Arith_AST =
  Add of Expr * Expr |
  Sub of Expr * Expr |
  Mul of Expr * Expr |
  Div of Expr * Expr |
  Not of Expr;;
 
type Expr =
  Expr of Arith_AST |
  Int of int;;


How you choose to represent these structures in C is up to you, it's easy if you understand how it works in an abstract manner as shown in the Lisp and O'Caml examples.

Name: Anonymous 2009-10-31 1:07

>>3
Oh damn... at least I didn't spoonfeed him one of the many concrete representations possible in C, he'll have to figure out those trivialities himself.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List