title: use yytext in bison code
body: <p>When I trying to compile my code in yacc&amp;lex I get this error:
  <a href="https://i.stack.imgur.com/MLKBz.png%60" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MLKBz.png%60" alt="this error"></a></p>
  
  <p>yacc Code:</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #define YYSTYPE struct node*
  typedef struct node{
      char *token;
      struct node *left;
      struct node *right;
  } node;
  node* mknode(char* token, node *left, node *right);
  void Printtree(node *tree);
  int yyerror();
  %}
  %token NUM PLUS MINUS
  %left PLUS MINUS
  %%
  S:exp {printf("OK\n"); Printtree($1);};
  exp:exp PLUS exp {$$=mknode("+",$1,$3);}
      | exp MINUS exp{$$=mknode("-",$1,$3);}
      | NUM {$$=mknode(yytext, NULL, NULL);};
  %%
  #include "lex.yy.c"
  int main(){
      return yyparse();
  }
  node *mknode (char *token, node *left, node *right){
      node *newnode = (node*)malloc(sizeof(node));
      char *newstr = (char*)malloc(sizeof(token)+1);
      strcpy (newstr, token);
      newnode-&gt;left=left;
      newnode-&gt;right=right;
      newnode-&gt;token=newstr;
      return newnode;
  }
  void Printtree(node* tree){
      printf("%s\n", tree-&gt;token);
      if (tree-&gt;left)
          Printtree(tree-&gt;left);
      if (tree-&gt;right)
          Printtree(tree-&gt;right);
  }
  int yyerror(){
      printf("ERROR\n");
      return 0;
  }
  </code></pre>
  
  <p>and the lex Code</p>
  
  <pre><code>%%
  [0-9]+ return NUM;
  \+ return PLUS;
  \- return MINUS;
  %%
  </code></pre>
  
  <p>when i trying to change the yytext to $1 is compiled but when i run the code and type for example 5+6 is saying: (segmentation fault (core dumped))</p>
  
  <p>using ubuntu 64:</p>
  
  <p>lex compile with flex version lex 2.6.0:</p>
  
  <pre><code>lex subProject.lex
  </code></pre>
  
  <p>and yacc compile with bison version bison(GNU Bison) 3.0.4:</p>
  
  <pre><code>yacc subProject.yacc
  </code></pre>
  
  <p>and the error maker:</p>
  
  <pre><code>cc subProject -o y.tab.c -ll -Ly
  </code></pre>