title: I can't understand how parser to evaluate parse tree and program can't print out text file which I want it
body: <p>This is my midterm project in college. There is a input.txt and it has:</p>
  <blockquote>
  <p>( L 36 [ L 4 [ F 100 R 90 ] R 10 ] )</p>
  </blockquote>
  <p>The logic of pattern in input.txt is  first go forward 100 step (<code>F100</code>) and turn 90 degrees (<code>R90</code>). You must try this 4 times (<code>L4[ F100 R 90</code>]. Later turn 10 degree and try this 36 times (<code>L 36 [ L 4 [ F100 R 90] R10 ]</code>). Thus the program will print a turtle picture which I attached below:</p>
  <p><img src="https://i.stack.imgur.com/6YxpL.png" alt="" /></p>
  <p>Anyway I wrote the lex file and it can read input.txt. Lex  recognizes the pattern and create tokens, also I wrote the yacc file and I describe tokens and nonterminals. I tried to create a parse tree but I can't fix how parser to evaluate the tree that's why I tried to  put &quot;printf&quot; in actions block in non-terminals and I tried to understand this but I didn't. Actually  when lex and yacc compile together, it will create text file and text file has to have this pattern:</p>
  <blockquote>
  <p>f100<br />
  r90<br />
  f100<br />
  r90<br />
  f100<br />
  r90<br />
  f100<br />
  r90</p>
  </blockquote>
  <p>How can I create this with yacc?
  In this way my C++ code reads the textfile and it will create the turtle.</p>
  <h3>Lex file</h3>
  <pre><code>%{
  #include &quot;y.tab.h&quot;
  %}
  
  %%
  L                          {return LOOP;}
  [1-9][0-9]*                { return INT;}
  \[                         return LSB;  //left square brackets
  \]                         return RSB; //right square brackets
  [Rr]                       return TURN;
  [Ff]                       return STEP;
  [ \t\n]                     ; 
  .                        {ECHO; yyerror (&quot;unexpected character&quot;);}
  
  %%
  int yywrap(void)
  {
      return 1;
  }
  </code></pre>
  <h3>Yacc file</h3>
  <pre><code>%{
  void yyerror ( char* s );
  int yylex();
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  %}
  
  %union{ int num; char id;}
  %start LINE
  %token &lt;id&gt; LOOP
  %token &lt;num&gt; INT
  %token LSB
  %token RSB
  %token &lt;id&gt;TURN
  %token &lt;id&gt;STEP
  %type &lt;id&gt;LOOPN FORWARD RIGHT
  %type &lt;id&gt;S1 S2 S3
  
  %%
  LINE : LOOPN LSB LINE RSB | S1  {
     printf(&quot;LINE is read \n&quot;);
  }
  
  S1: LOOPN RSB S1 LSB RIGHT | S2   {
     printf(&quot;S1 is read \n&quot;);
  }
  
  S2: S2 RIGHT | S3 {
     printf(&quot;S2 is read \n&quot;);
  }
  
  S3 : FORWARD {
      printf(&quot;S3 is read \n&quot;);
  }
  
  LOOPN : LOOP INT {
     printf(&quot;LOOP is read \n &quot;);
  }
  
  FORWARD : STEP INT {
     printf(&quot;FORWARD is read \n&quot;);
  }
  
  RIGHT : TURN INT {
      printf(&quot; RIGHT is read \n&quot;);
  }
  %%
  
  int main (int argc, char* argv[]) {
      if(argc &gt; 1)
      {
          FILE *fp = fopen(&quot;input.txt&quot;, &quot;r&quot;);
          if(fp)
              yyin = fp;
      }
  
      yylex();
      return yyparse ();
  }
  
  void yyerror (char *s) {fprintf (stderr, &quot;%s\n&quot;, s);} 
  </code></pre>