这个 yacc 文件有什么问题?
我在以下代码中收到此错误 tema4.y:13.19-26: 语法错误,意外的类型,请帮助我!
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
%}
%union {
int intval;
char* strval;
char* charval;
}
%token <charval>SHR <intval>NR
%token CMP
%type <strval>str <intval>expr
%nonassoc CMP '|'
%left '+' '-'
%left '*' '`' '#'
%start s
%%
s:str {printf("%s \n",$<strval>$);}
| expr {printf("%s \n",$<intval>$)}
;
str : str '+' str {
char* s=malloc(strlen($1)+strlen($3)+1);
strcpy(s,$1);
strcat(s,$3);
$$=s;
}
| str '-' str {
char *s=malloc(strlen($1));
char *sir=malloc(strlen($1)-strlen($3));
char *pt,*ps;
int i;
strcpy(s,$1);
pt=strstr(s,$3);
if(pt) {
ps=pt+strlen($3);
strncpy(pt,ps,strlen(ps));
ps+=strlen(ps);
strncpy(sir,s,strlen($1)-strlen($3));
}
$$=sir;
}
| str '*' NR {
int i;
char* s=malloc(strlen($1)*NR+1);
for(i=0;i<$3;i++)
{
strcat(s,$1);
}
$$=s;
}
| str '#' NR {
char *s=malloc($3+1);
strcpy(s,$1);
if($3>strlen($1)) {printf("Nr prea mare\n");exit(1);}
else {s=s+strlen(s)-$3;
$$=s;}
}
| NR '`' str {
char *s=malloc($1+1);
strncpy(s,$3,$1);
$$=s;
}
| '(' str ')' {
$$=$2;
}
| SHR {
char* s=malloc(strlen($1));
strcpy(s,$1);
$$=s;
}
| expr
;
expr : str CMP str {
if(strcmp($1,$3)) $$=0;
else $$=1;
}
| '|' str '|' {
$$=strlen($2);
}
;
%%
int main(){
yyparse();
}
I'm getting this error tema4.y:13.19-26: syntax error, unexpected typetype on the following code, please help me!
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
%}
%union {
int intval;
char* strval;
char* charval;
}
%token <charval>SHR <intval>NR
%token CMP
%type <strval>str <intval>expr
%nonassoc CMP '|'
%left '+' '-'
%left '*' '`' '#'
%start s
%%
s:str {printf("%s \n",lt;strval>$);}
| expr {printf("%s \n",lt;intval>$)}
;
str : str '+' str {
char* s=malloc(strlen($1)+strlen($3)+1);
strcpy(s,$1);
strcat(s,$3);
$=s;
}
| str '-' str {
char *s=malloc(strlen($1));
char *sir=malloc(strlen($1)-strlen($3));
char *pt,*ps;
int i;
strcpy(s,$1);
pt=strstr(s,$3);
if(pt) {
ps=pt+strlen($3);
strncpy(pt,ps,strlen(ps));
ps+=strlen(ps);
strncpy(sir,s,strlen($1)-strlen($3));
}
$=sir;
}
| str '*' NR {
int i;
char* s=malloc(strlen($1)*NR+1);
for(i=0;i<$3;i++)
{
strcat(s,$1);
}
$=s;
}
| str '#' NR {
char *s=malloc($3+1);
strcpy(s,$1);
if($3>strlen($1)) {printf("Nr prea mare\n");exit(1);}
else {s=s+strlen(s)-$3;
$=s;}
}
| NR '`' str {
char *s=malloc($1+1);
strncpy(s,$3,$1);
$=s;
}
| '(' str ')' {
$=$2;
}
| SHR {
char* s=malloc(strlen($1));
strcpy(s,$1);
$=s;
}
| expr
;
expr : str CMP str {
if(strcmp($1,$3)) $=0;
else $=1;
}
| '|' str '|' {
$=strlen($2);
}
;
%%
int main(){
yyparse();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
John 已经找到了您的第一个错误的答案 -
%type
或%token
声明中只有一种类型。对于第二个错误(类型冲突),问题在于您的规则str: expr ;
-- str 是
而 expr 是< intval>
,因此默认操作({ $$ = $1; }
实际上)存在类型冲突。您还存在某个地方的归约/归约冲突 - 您需要使用 -d 运行 yacc 并查看生成的 y.output 文件以获取有关冲突的更多详细信息。
John has the answer for your first error -- only one type in a
%type
or%token
declaration. For the second error (type clash), the problem is your rulestr: expr ;
-- str is a<strval>
while expr is an<intval>
, so the default action ({ $$ = $1; }
effectively) has a type clash.You also have a reduce/reduce conflict somewhere -- you'll need to run yacc with -d and look at the resulting y.output file for more detail on what the conflict is.
据我所知,
%token
或%type
声明中只能有一个
。您可以有多个标记或非终结符,但只能有一种类型名称。尝试拆分这些行:As best I can tell you can only have one
<type>
in a%token
or%type
declaration. You can have multiple tokens or non-terminals but only one type name. Try splitting those lines: