三个地址代码语句的 yacc 和 lex 代码
这是我的 lex 代码: 三.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;
%%
我的 yacc 是
三.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++);
}
但是当我编译这个
三.y:19.40-41: $3 of `line' 没有声明类型
Three.y:21.20-21: $$ of `exp' 没有声明类型
三.y:21.60-61: `exp' 的 $$ 没有声明类型
3.y:21.63-64: `exp' 的 $1 没有声明类型
3.y:21.66-67: `exp' 的 $3 没有声明类型类型
三.y:21.100-101: ‘exp’ 的 $$ 没有声明类型
三.y:21.103-104: ‘exp’ 的 $1 没有声明类型
三.y:21.106-107: ‘exp’ 的 $3 有没有声明类型
Three.y:22.21-22: `exp' 的 $$ 没有声明类型
Three.y:22.61-62: `exp' 的 $$ 没有声明类型
Three.y:22.64-65: ` exp' 的 $1 exp' 没有声明类型
3.y:22.67-68: 'exp' 的 $3 没有声明类型
3.y:22.104-105: 'exp' 的 1 美元没有声明类型
3.y:22.107-108: 'exp' 的 3 美元 没有声明类型`exp' 没有声明类型
Three.y:23.22-23: `exp' 的 $$ 没有声明类型
Three.y:23.62-63: `exp' 的 $$ 没有声明类型
Three.y:23.65-66 : `exp' 的 $1 没有声明类型
Three.y:23.68-69: `exp' 的 $3 没有声明类型
Three.y:23.102-103: `exp' 的 $$ 没有声明类型
Three.y:23.105 -106: `exp' 的 $1 没有声明类型
Three.y:23.108-109: `exp' 的 $3 没有声明类型
Three.y:24.21-22: `exp' 的 $$ 没有声明类型
Three.y :24.61-62: `exp' 的 $$ 没有声明类型
3.y:24.64-65: `exp' 的 $1 没有声明类型
3.y:24.67-68: `exp' 的 $3 没有声明类型
3 .y:24.102-103: `exp' 的 $$ 没有声明类型
3.y:24.105-106: `exp' 的 $1 没有声明类型
3.y:24.108-109: `exp' 的 $3 没有声明 类型类型
三.y:25.10-11: $$ of `exp' 没有声明类型
pla 帮我解决这个问题..
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将该行添加
到您的代码中,您需要定义 exp.
add the line
to your code, you need to define the exp.
当您使用 %union 作为 yystype 时,当您引用 yylval 时,您必须说明要使用联合体的哪个成员。因此,
当你说你的工会时,
你应该说类似 $1.a_var 的东西:
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
when you say
you should say instead something like $1.a_var:
据我所知,
line
和exp
的语义值类型似乎不是在您的代码中指定。
所以,我认为指定像
%type这样的类型行 exp
将解决错误。
希望这有帮助
As far as I see, the types of the semantic values of
line
andexp
seem not to bespecified in your code.
So, I think specifying the types like
%type <a_var> line exp
will solvethe error.
Hope this helps