您好我写了我的lex文件如下:
%{
#include <stdio.h>
int yylex(void);
void yyerror(char *);
%}
%%
[ \t (if) {return IF_TOKEN ;}
(else) {return ELSE_TOKEN ;}
(while) {return WHILE_TOKEN ;}
(FOR) {return FOR_TOKEN ;}
(BREAK) {return BREAK_TOKEN ;}
(float) {return FLOAT_TOKEN ;}
(int) {return INT_TOKEN ;}
(long) {return LONG_TOKEN ;}
(return) {return RETURN_TOKEN ;}
(defFunction) {return DEFFUNCTION_TOKEN ;}
(defClass) {return DEFCLASS_TOKEN ;}
(\() {return PAROPEN_TOKEN ;}
(\)) {return PARCLOS_TOKEN ;}
(\{) {return CBROPEN_TOKEN ;}
(\}) {return CBRCLOS_TOKEN ;}
(<) {return LESSTHN_TOKEN ;}
(>) {return GRTRTHN_TOKEN ;}
(=) {return EQUALTO_TOKEN ;}
(!=) {return NEQUALTO_TOKEN ;}
(\+) {return SUM_TOKEN ;}
(-) {return MINUS_TOKEN ;}
(\*) {return STAR_TOKEN ;}
(\/) {return SLASH_TOKEN ;}
(%) {return REMAIN_TOKEN ;}
(\[) {return BRAOPEN_TOKEN ;}
(\]) {return BRACLOS_TOKEN ;}
(";") {return SEMICOL_TOKEN ;}
([A-Za-z&_$][A-Za-z$_0-9]*) {return ID ;}
(0|[-]?[1-9][0-9]*) {return NUMBER ;}
. {return ERROR ;}
%%
int yywrap (void) {
return 1;
}
int main (int argc, char** argv) {
yyin = fopen (argv[1], "r");
yylex();
fclose (yyin);
return 0;
}
我开始编写我的yacc文件,如下所示:
%{
#include <stdio.h>
int yylex(void) ;
void yyerror(char *);
%}
%token IF_TOKEN ELSE_TOKEN WHILE_TOKEN FOR_TOKEN BREAK_TOKEN FLOAT_TOKEN INT_TOKEN LONG_TOKEN RETURN_TOKEN DEFFUNCTION_TOKEN DEFCLASS_TOKEN PAROPEN_TOKEN PARCLOS_TOKEN CBROPEN_TOKEN CBR_CLOSTOKEN LESSTHN_TOKEN GRTRTHN_TOKEN EQUALTO_TOKEN NEQUALTO_TOKEN SUM_TOKEN MINUS_TOKEN STAR_TOKEN SLASH_TOKEN REMAIN_TOKEN BRAOPEN_TOKEN BRACLOS_TOKEN SEMICOL_TOKEN ID NUMBER ERROR
%%
program : expr ;
expr : NUMBER ;
%%
void yyerror(char *s){
fprintf(stderr, "%s\n", s);
}
int main(void){
yyparse();
return 0;
}
当我想用命令编译yacc文件时: yacc example.y 它返回定义为默认值的解析器名称:&#34;解析&#34; 我想给它一个数字来检查它是否正确然后完成我的yacc文件。