title: Parse tree using Bison
body: <p>I am new to Bison. I am trying to implement a parser. The main problem that I am facing is with the parse tree. I am getting an error related to the tree which I am unable to eliminate. It says "unknown type name 'tree' ". Here is my y file. I would be glad if anyone could help me sort out this. I need the parser very badly. Thanks in advance.</p>
  
  <pre><code>%{
   #include &lt;stdio.h&gt;
   #include &lt;string.h&gt;
  
   void yyerror(const char *str)
   {
      fprintf(stderr,"error FAIL: %s\n",str);
   }
  
  int yywrap()
  {
      return 1;
  } 
  
  enum treetype {operator_node, variable_node};
  typedef struct tree {
  enum treetype nodetype;
  union {
   struct {struct tree *left, *right; char operator;} an_operator;
    char *a_variable;
  } body;
  } tree;
  static tree *make_op (tree *l, char o, tree *r) {
  tree *result= (tree*) malloc (sizeof(tree));
  result-&gt;nodetype= operator_node;
  result-&gt;body.an_operator.left= l;
  result-&gt;body.an_operator.operator= o;
  result-&gt;body.an_operator.right= r;
  return result;
  }
  static void printtree (tree *t, int level) {
  #define step 4
  if (t)
   switch (t-&gt;nodetype)
   {
     case operator_node:
      printtree (t-&gt;body.an_operator.right, level+step);
      printf ("%*c%c\n", level, ' ', t-&gt;body.an_operator.operator);
      printtree (t-&gt;body.an_operator.left, level+step);
      break;
     case variable_node:
      printf ("%*c%c\n", level, ' ', t-&gt;body.a_variable);
   }
  } 
  
  %}
  
  %union {
  char* a_variable;
  tree* a_tree;
  }
  
  %start file
  %token &lt;a_variable&gt; TOKDIGIT TOKFLOAT TOKID TOKSEMICOLON TOLCOLON TOKCOMMA TOKUNRECOG   TOKCOMMENT TOKDOT TOKMINUS TOKCOLON
  %type &lt;a_tree&gt; field object file ID
  %right TOKMINUS
  
  %%
  
  file   :    
     |    object file { printtree($1, 1); }
      ;
  object :     field object {$$ = make_op($1, '', $2);}
    |   field {$$ = $1 ; }
      ;
  field  :    ID TOKCOLON field {$$ = make_op ($1, ':', $3); }
    | ID TOKCOMMA field {$$ = make_op ($1, ',', $3); }
    | ID TOKSEMICOLON field {$$ = make_op ($1, ';', $3); }
      ;   
  ID     :   TOKID { $$ = $1; }
         ;    
  
  %%
  </code></pre>