YACC : 使用了符号 **** 但未定义为令牌且没有规则
YACC : symbol **** is used but, is not defined as a token and has no rules
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
extern int yylval;
%}
%start E
%token number
%%
S :E {printf("%s",);}
;
E :E PLUS T {$$ = + ;}
|T {$$ = ;}
;
T :T STAR F {$$ = * ;}
|F {$$ = ;}
;
F :LP E RP {$$ = ;}
|number {$$ = ;}
;
%%
yylex(){
char ch;
ch = getchar();
switch(ch){
case '+':
return PLUS;
break;
case '*':
return STAR;
break;
case '(':
return LP;
break;
case ')':
return RP;
break;
default:
if(ch >= '0' && ch <= '9'){
ch -= '0';
yylval = ch;;
return (number);
}
else
return -1;
}
}
void main(){
yyparse();
}
下午,我正在做关于如何使用 yacc 的作业,问题是我不知道如何在 .y 文件中正确包含头文件。从我所看到的描述如何编写 .y 文件的各种页面来看,我在编写这个 .y 文件方面没有做错任何事情。所以,请帮助我!
这些是我收到的错误信息
embedded@embedded-P15xEMx:~/Project/Compiler$ yacc parser.y
parser.y:19.10-11: error: symbol LP is used, but is not defined as a token and has no rules
F :LP E RP {$$ = ;}
^^
parser.y:13.12-15: error: symbol PLUS is used, but is not defined as a token and has no rules
E :E PLUS T {$$ = + ;}
^^^^
parser.y:19.15-16: error: symbol RP is used, but is not defined as a token and has no rules
F :LP E RP {$$ = ;}
^^
parser.y:16.12-15: error: symbol STAR is used, but is not defined as a token and has no rules
T :T STAR F {$$ = * ;}
^^^^
这是我包含的头文件
#ifndef _PARSER_H_
#define _PARSER_H_
#include <stdio.h>
#define PLUS 1
#define STAR 2
#define LP 3
#define RP 4
#define number 5
#endif
token符号不能自己声明,必须用yacc来声明,like
%token PLUS
等等
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
extern int yylval;
%}
%start E
%token number
%%
S :E {printf("%s",);}
;
E :E PLUS T {$$ = + ;}
|T {$$ = ;}
;
T :T STAR F {$$ = * ;}
|F {$$ = ;}
;
F :LP E RP {$$ = ;}
|number {$$ = ;}
;
%%
yylex(){
char ch;
ch = getchar();
switch(ch){
case '+':
return PLUS;
break;
case '*':
return STAR;
break;
case '(':
return LP;
break;
case ')':
return RP;
break;
default:
if(ch >= '0' && ch <= '9'){
ch -= '0';
yylval = ch;;
return (number);
}
else
return -1;
}
}
void main(){
yyparse();
}
下午,我正在做关于如何使用 yacc 的作业,问题是我不知道如何在 .y 文件中正确包含头文件。从我所看到的描述如何编写 .y 文件的各种页面来看,我在编写这个 .y 文件方面没有做错任何事情。所以,请帮助我!
这些是我收到的错误信息
embedded@embedded-P15xEMx:~/Project/Compiler$ yacc parser.y
parser.y:19.10-11: error: symbol LP is used, but is not defined as a token and has no rules
F :LP E RP {$$ = ;}
^^
parser.y:13.12-15: error: symbol PLUS is used, but is not defined as a token and has no rules
E :E PLUS T {$$ = + ;}
^^^^
parser.y:19.15-16: error: symbol RP is used, but is not defined as a token and has no rules
F :LP E RP {$$ = ;}
^^
parser.y:16.12-15: error: symbol STAR is used, but is not defined as a token and has no rules
T :T STAR F {$$ = * ;}
^^^^
这是我包含的头文件
#ifndef _PARSER_H_
#define _PARSER_H_
#include <stdio.h>
#define PLUS 1
#define STAR 2
#define LP 3
#define RP 4
#define number 5
#endif
token符号不能自己声明,必须用yacc来声明,like
%token PLUS
等等