title: How create an interpreter to that prints strings in Flex/Bison
body: <p>I created a small interpreter using flex/bison.</p>
  
  <p>This just can print a number, but I want to know how can add a string print?</p>
  
  <hr>
  
  <p>lexer :</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "y.tab.h"
  %}
  %%
  
  &lt;INITIAL&gt;[s|S][h|H][o|O][w|W]               {return show;}
  &lt;INITIAL&gt;[0-9a-zA-z]+                       {yylval.num=atoi(yytext);return string;}
  &lt;INITIAL&gt;[\-\+\=\;\*\/]                     {return yytext[0];}
  %%
  int yywrap (void) {return 1;}
  </code></pre>
  
  <hr>
  
  <p>yacc :</p>
  
  <pre><code>%{
  void yyerror(char *s);
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  %}
  %union {int num;}
  %start line
  %token show
  %token &lt;num&gt; number
  %type &lt;num&gt; line exp term
  
  %%
  line    : show exp ';'          {printf("showing : %d\n",$2);}
          | line show exp ';'     {printf("showing : %d\n",$3);}  
          ;
  exp     : term                  {$$ = $1;}
          | exp '+' term          {$$ = $1 + $3;}
          | exp '-' term          {$$ = $1 - $3;}
          | exp '*' term          {$$ = $1 * $3;}
          | exp '/' term          {$$ = $1 / $3;}
          ;
  term    : number                {$$ = $1;}
  %%
  int main (void)
  {
      return yyparse();
  }
  void yyerror (char *s)
  {
      printf("-%s at %s !\n",s );
  }
  </code></pre>
  
  <hr>
  
  <p>test data :</p>
  
  <pre><code>show 5;
  show 5+5;
  show 5*2-5+1;
  </code></pre>
  
  <hr>
  
  <p>I want add string code to the lexer :</p>
  
  <pre><code>&lt;INITIAL&gt;\"                                 {BEGIN(STRING);}
  &lt;STRING&gt;\"                                  {BEGIN(INITIAL);}
  </code></pre>
  
  <p>Now how to use from <code>content of in &lt;STRING&gt;</code>?</p>
  
  <p>Can you help me to complete my interpreter?</p>
  
  <p>I need to add this examples to my interpreter :</p>
  
  <pre><code>show "hello erfan";//hello erfan
  show "hello ".5;//hello 5
  </code></pre>
  
  <p>Please help me.</p>