title: Bison - how to print a parse tree
body: <p>Hi I'm working on a small bison to learn how it works. The bison is supposed to parse a sentence.
  The sentence is made of expressions and expressions are made of words. </p>
  
  <p>Following is my code:</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  
  void yyerror(const char *str)
  {
      fprintf(stderr,"error: %s\n",str);
  }
  
  int yywrap()
  {
      return 1;
  }
  
  main()
  {
      yyparse();
  }
  
  %}
  
  %token ASSIGN RANGE OR AND WHITESPACE QUOTE LPAREN RPAREN NOT GREATER LESS
  
  %union 
  {
          int number;
          char *string;
  }
  
  %token &lt;number&gt; VALUE
  %token &lt;string&gt; WORD
  
  %type &lt;string&gt; term
  %type &lt;string&gt; expression
  %%
  
  query:   /* empty */
      | query expression 
      {
          printf("WOrd:%s",$2);
      }
      ;
  
  expression:
       term
      |expression term
      |expression AND term
          {
              printf("AND");
          }
      ;
  
  term:
      WORD
      {
          $$=$1;
      }
      ;
  </code></pre>
  
  <p>So, when user enters a word, it supposed to print out the word. 
  User should be able to type in:
  word,word word,word and word</p>
  
  <p>I'm not sure how to pass a word using $$ and print it out from the "query expression" rule.
  How do I do this? </p>
  
  <p>this is my flex:</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include "y.tab.h"
  %}
  %%
  [0-9]+                  yylval.number=atoi(yytext);return VALUE;
  [a-zA-Z][a-zA-Z]*       yylval.string=strdup(yytext);return WORD;
  ":"                     return ASSIGN;
  "and"|"&amp;"|"&amp;&amp;"          return AND; 
  ".."                    return RANGE;
  "-"                     return NOT;
  "|"                     return OR;
  "\""                    return QUOTE;
  "&gt;"                     return GREATER;
  "&lt;"                     return LESS;
  \n                      /* ignore end of line */;
  \t                      /* ignore end of line */;
  
  %%
  </code></pre>
  
  <p>THanks so much in advance.
  Sarah</p>