I'm trying to generate 3 address code corresponding to basic arithmetic expressions. I haven't worked with lex and yacc tools before much (Newbie) and I'm having trouble understanding the flow of control/command among the two i.e how the two programs are interacting.
lex.l
%{
    #include<stdio.h>
    #include"y.tab.h"
    int k=1;
%}
%%
[0-9]+ {
yylval.dval=yytext[0];
return NUM;
}
\n {return 0;}
. {return yytext[0];}
%%
void yyerror(char* str)
{
        printf("\n%s",str);
}
char *gencode(char word[],char first,char op,char second)
{
    char temp[10];
    sprintf(temp,"%d",k);
    strcat(word,temp);
    k++;
    printf("%s = %c %c %c\n",word,first,op,second);
    return word; //Returns variable name like t1,t2,t3... properly
}
int yywrap()
{
    return 1;
}
main()
{
        yyparse();
        return 0;
}
yacc.y
%{
#include<stdio.h>
int aaa;
%}
%union{
    char dval;
}
%token <dval> NUM
%type <dval> E
%left '+' '-'
%left '*' '/' '%'
%%
statement : E {printf("\nt = %c \n",$1);}
          ;
E : E '+' E 
    {   
        char word[]="t";
        char *test=gencode(word,$1,'+',$3);
        $$=test;
    }
  | E '-' E 
    {
        char word[]="t";
        char *test=gencode(word,$1,'-',$3);
        $$=test;
    }
  | E '%' E 
    {
        char word[]="t";
        char *test=gencode(word,$1,'%',$3);
        $$=test;
    }
  | E '*' E 
    {
        char word[]="t";
        char *test=gencode(word,$1,'*',$3);
        $$=test;
    }
  | E '/' E 
    {
        char word[]="t";
        char *test=gencode(word,$1,'/',$3);
        $$=test;
    }
  | '(' E ')' 
    {
        $$=$2;
    }
  | NUM 
    {
        $$=$1;
    }
  ;
%%
Problem: getting garbage value in output
Expected output for expression (2+3)*5 should be like:
t1= 2 + 3
t2= t1 * 5
Obtained output:
t1= 2 + 3
t2= garbage value * 5
I'm unable to figure out how to correct this. The variable names (eg t1,t2,t3 ) are being properly returned from gencode() method in lex.l
char *test=gencode(word,$1,'%',$3);
But I'm completely clueless about what is going wrong after that. I believe I'm not handling the $$,$1,$3 terms correctly.
Please help me understand what is going wrong, what needs to be done and how to do it. A little help and some explanation would be very helpful. Thank you.
 
     
    