yacc and lex code for three address code statements
here is my lex code: three.l
%{
#include "y.tab.h"
#include "string.h"
%}
%%
[a-zA-Z]+ { yylval.a_var=(char *)malloc(sizeof(char *));
strcpy(yylval.a_var,yytext);
return var;}
[-+/*,] {return yytext[0];}
[\t\n] return开发者_如何学编程 0;
%%
my yacc is
three.y
%{
#include "stdio.h"
#include "string.h"
int yywrap()
{
return 1;
}
static int i;
int d;
char temp();
%}
%left '+' '-'
%left '*' '/'
%union{char *a_var;}
%token id
%token <a_var>var
%start line
%%
line:var '=' exp {printf("%c=t%c\n",$1,$3);}
;
exp:exp '+' exp {$$=temp();if(i<=1){printf("t%c=%c+%c\n",$$,$1,$3);} else{printf("t%c=t%c+%c\n",$$,$1,$3);} }
|exp '-' exp {$$=temp();if(i<=1){printf("t%c=%c-%c\n",$$,$1,$3); } else{printf("t%c=t%c-%c\n",$$,$1,$3);} }
|exp '*' exp {$$=temp();if(i<=1){printf("t%c=%c*%c\n",$$,$1,$3); } else{printf("t%c=t%c*%c\n",$$,$1,$3);} }
|exp '/' exp {$$=temp();if(i<=1){printf("t%c=%c/%c\n",$$,$1,$3); } else {printf("t%c=t%c/%c\n",$$,$1,$3);} }
|var {$$=$1;}
;
%%
main()
{
yyparse();
return 0;
}
int yyerror(char *s)
{
fprintf(stderr,"%s\n",s);
}
char temp()
{
return('1'+ i++);
}
but when i compile this
three.y:19.40-41: $3 of `line' has no declared type
three.y:21.20-21: $$ of `exp' has no declared type
three.y:21.60-61: $$ of `exp' has no declared type
three.y:21.63-64: $1 of `exp' has no declared type
three.y:21.66-67: $3 of `exp' has no declared type
three.y:21.100-101: $$ of `exp' has no declared type
three.y:21.103-104: $1 of `exp' has no declared type
three.y:21.106-107: $3 of `exp' has no declared type
three.y:22.21-22: $$ of `exp' has no declared type
three.y:22.61-62: $$ of `exp' has no declared type
three.y:22.64-65: $1 of `exp' has no declared type
three.y:22.67-68: $3 of `exp' has no declared type
three.y:22.104-105: $1 of `exp' has no declared type
three.y:22.107-108: $3 of `exp' has no declared type
three.y:23.22-23: $$ of `exp' has no declared type
three.y:23.62-63: $$ of `exp' has no declared type
three.y:23.65-66: $1 of `exp' has no declared type
three.y:23.68-69: $3 of `exp' has no declared type
three.y:23.102-103: $$ of `exp' has no declared type
three.y:23.105-106: $1 of `exp' has no declared type
three.y:23.108-109: $3 of `exp' has no declared type
three.y:24.21-22: $$ of `exp' has no declared type
three.y:24.61-62: $$ of `exp' has no declared type
three.y:24.64-65: $1 of `exp' has no declared type
three.y:24.67-68: $3 of `exp' has no declared type
three.y:24.102-103: $$ of `exp' has no declared type
three.y:24.105-106: $1 of `exp' has no declared type
three.y:24.108-109: $3 of `exp' has no declared type
three.y:25.10-11: $$ of `exp' has no declared type
pla help me to solve this..
add the line
%type <a_var> exp
to your code, you need to define the exp.
When you use a %union for your yystype, then when you refer to yylval you have to say which member of the union to use. So with your union
%union{char *a_var;}
when you say
line:var '=' exp {printf("%c=t%c\n",$1,$3);}
you should say instead something like $1.a_var:
line:var '=' exp {printf("%c=t%c\n",$1.a_var,$3.a_var);}
As far as I see, the types of the semantic values of line
and exp
seem not to be
specified in your code.
So, I think specifying the types like %type <a_var> line exp
will solve
the error.
Hope this helps
精彩评论