4.2. Abstract Syntax Trees in C


AST type

Create a type of tree nodes. Here is a type that is suitable for your interpreter.

typedef struct astnode
{
  int kind;   /* This tells which kind of expression this is. */
  int mark;   /* Used to mark nodes */
  ...

  union {
    int         intval;
    const char* strval;
    struct {
      struct ast* s1;
      struct ast* s2;
      struct ast* s3;
    } subtrees;
  } fields;
}
ASTNODE;

typedef ASTNODE* AST;

If t has type AST, then t->kind is the kind of node. Normally, you would test that in a function that deals with trees.

See Assignment 3 for details on abstract syntax trees.