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