title: Concatenating string in yacc
body: <p>I can't seem to figure out how to concatenate two string in yacc.</p>
  
  <p>Here is the lex code</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  #include "y.tab.h"
  %}
  %%
  [0-9]+ {yylval.intval=atoi(yytext); return NR;}
  [a-zA-Z]+ {yylval.strval=yytext; return STR;}
  "0exit" {return 0;}
  [ \t] ;
  \n {return 0;}
  . {return yytext[0];}
  </code></pre>
  
  <p>And here i have the basics to add two strings</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  %}
  %union {
  int intval;
  char* strval;
  }
  %token STR NR
  %type &lt;intval&gt;NR
  %type &lt;strval&gt;STR
  %type &lt;strval&gt;operatie
  %left '+' '-'
  %right '='
  %start S
  %%
  S   : S operatie        {}
      | operatie          {printf("%s\n",$&lt;strval&gt;$);}
      ;
  
  operatie    :   STR '+' STR {   char* s=malloc(sizeof(char)*(strlen($1)+strlen($3)+1));
                                  strcpy(s,$1); strcat(s,$3);
                                  $$=s;}
              ;
  %%
  int main(){
   yyparse();
  }    
  </code></pre>
  
  <p>The code works, the problem is that the output is something like this: 
  If i input </p>
  
  <blockquote>
    <p>aaaa + bbbb</p>
  </blockquote>
  
  <p>i get the output </p>
  
  <blockquote>
    <p>aaaa + bbbbbbbb</p>
  </blockquote>