title: Representing an Abstract Syntax Tree in C
body: <p>I'm implementing a compiler for a simple toy language in C.  I have a working scanner and parser, and a reasonable background on the conceptual function/construction of an AST.  My question is related to the specific way to represent an AST in C.  I've come across three styles pretty frequently in different texts/resources online:</p>
  
  <p><strong>One struct per type of node.</strong></p>
  
  <p>This has a base node "class"(struct) that is the first field in all the child structs.  The base node contains an enum that stores the type of node(constant, binary operator, assignment, etc).  Members of the struct are accessed using a set of macros, with one set per struct.  It looks something like this:</p>
  
  <pre><code>struct ast_node_base {
      enum {CONSTANT, ADD, SUB, ASSIGNMENT} class;
  };
  
  struct ast_node_constant {
      struct ast_node_base *base;
      int value;
  };
  
  struct ast_node_add {
      struct ast_node_base *base;
      struct ast_node_base *left;
      struct ast_node_base *right;
  };
  
  struct ast_node_assign {
      struct ast_node_base *base;
      struct ast_node_base *left;
      struct ast_node_base *right;
  };
  
  #define CLASS(node) ((ast_node_base*)node)-&gt;class;
  
  #define ADD_LEFT(node) ((ast_node_add*)node)-&gt;left;
  #define ADD_RIGHT(node) ((ast_node_add*)node)-&gt;right;
  
  #define ASSIGN_LEFT(node) ((ast_node_assign*)node)-&gt;left;
  #define ASSIGN_RIGHT(node) ((ast_node_assign*)node)-&gt;right;
  </code></pre>
  
  <p><strong>One struct per layout of node.</strong></p>
  
  <p>This appears to be mostly the same as the above layout, except instead of having ast_node_add and ast_node_assign it would have an ast_node_binary to represent both, because the layout of the two structs is the same and they only differ by the contents of base->class.  The advantage to this seems to be a more uniform set of macros(LEFT(node) for all nodes with a left and right instead of one pair of macros per), but the disadvantage seems that the C type checking won't be as useful(there would be no way to detect an ast_node_assign where there should only be an ast_node_add, for example).</p>
  
  <p><strong>One struct total, with a union to hold different types of node data.</strong></p>
  
  <p>A better explanation of this than I can give can be found <a href="http://lambda.uta.edu/cse5317/notes/node25.html">here</a>.  Using the types from the previous example it would look like:</p>
  
  <pre><code>struct ast_node {
    enum { CONSTANT, ADD, SUB, ASSIGNMENT } class;
    union { int                                 value;
            struct { struct ast_node* left;    
                     struct ast_node* right;  } op;
  };
  </code></pre>
  
  <p>I'm inclined to like the third option the most because it makes recursive traversal much easier(in that lots of pointer casting is avoided in favor of the union), but it also doesn't take advantage of C type checking.  The first option seems the most dangerous in that it relies on pointers to structs being cast to access the member of any node(even different members of the same node requiring different cases to access(base vs. left)), but these casts are type checked so that might be moot.  The second option to me seems like the worst of both worlds, although maybe I'm missing something.</p>
  
  <p><strong>Which of these three schemes are the best, and why?  Is there a better fourth option I haven't come across yet?</strong>  I'm assuming none of them are a "one size fits all" solution, so if it matters the language I'm implementing is a statically typed imperative language, almost a small subset of C.</p>
  
  <p>A specific question I have about the third(union) layout.  <strong>If I use only the value field, will there be empty space following the value to accommodate for the possibility of op being written to?</strong></p>