Bison yylex的多重定义
我试过下面的lex和yacc文件,似乎有很多函数的多个定义,比如yylex、yyrestart等等。我尝试了几种方法和在线材料,但无法解决它。任何帮助都将不胜感激 try1.l文件如下所示Bison yylex的多重定义,bison,flex-lexer,lex,Bison,Flex Lexer,Lex,我试过下面的lex和yacc文件,似乎有很多函数的多个定义,比如yylex、yyrestart等等。我尝试了几种方法和在线材料,但无法解决它。任何帮助都将不胜感激 try1.l文件如下所示 %{ #include "y.tab.h" int linenum=1; int temp_int; %} %% \n {linenum++;} [\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments
%{
#include "y.tab.h"
int linenum=1;
int temp_int;
%}
%%
\n {linenum++;}
[\t ] /* skip spaces */;
\/\/[^\n]* /* ignore comments */;
"+" {return '+';}
"-" {return '-';}
"*" {return '*';}
"/" {return '/';}
")" {return ')';}
"(" {return '(';}
[0-9]+ {sscanf(yytext, "%d", &temp_int);
yylval.int_val = temp_int;
return INT;}
. {printf("LEX: unknown input string found in line %d \n", linenum);
abort();}
%%
int yywrap(void) {return 1;}
/* Put new code here */
%{
#include <stdio.h>
#include <ctype.h>
extern int linenum;
%}
/* define all types of variables and terminals */
%union{
int int_val;
}
/* define the individual types of variables and terminals */
%token <int_val> INT
%type <int_val> expr
/* assign priorities to operators in order to
avoid shift/reduce conflicts (grammar ambiguities) */
%left '+' '-'
%left '*' '/'
%left UMINUS
/* the start variable of your program */
%start program
/* The main Grammar with its actions */
%%
program : expr {printf("Expr value = %d \n", $1);}
| error {printf("YACC: syntax error near line %d \n", linenum);
abort();}
;
expr :'('expr')' {$$ = $2;}
| expr '+' expr {$$ = $1 + $3;}
| expr '-' expr {$$ = $1 - $3;}
| expr '*' expr {$$ = $1 * $3;}
| expr '/' expr {$$ = $1 / $3;}
| '-' expr %prec UMINUS {$$ = -$2;}
| INT {$$ = $1;}
;
%%
/* link lex code */
#include "lex.yy.c"
/* insert additional code here */
int main(){
return yyparse();
}
int yyerror(char *s) {fprintf(stderr, "%s \n",s);}
try1.y文件如下所示
%{
#include "y.tab.h"
int linenum=1;
int temp_int;
%}
%%
\n {linenum++;}
[\t ] /* skip spaces */;
\/\/[^\n]* /* ignore comments */;
"+" {return '+';}
"-" {return '-';}
"*" {return '*';}
"/" {return '/';}
")" {return ')';}
"(" {return '(';}
[0-9]+ {sscanf(yytext, "%d", &temp_int);
yylval.int_val = temp_int;
return INT;}
. {printf("LEX: unknown input string found in line %d \n", linenum);
abort();}
%%
int yywrap(void) {return 1;}
/* Put new code here */
%{
#include <stdio.h>
#include <ctype.h>
extern int linenum;
%}
/* define all types of variables and terminals */
%union{
int int_val;
}
/* define the individual types of variables and terminals */
%token <int_val> INT
%type <int_val> expr
/* assign priorities to operators in order to
avoid shift/reduce conflicts (grammar ambiguities) */
%left '+' '-'
%left '*' '/'
%left UMINUS
/* the start variable of your program */
%start program
/* The main Grammar with its actions */
%%
program : expr {printf("Expr value = %d \n", $1);}
| error {printf("YACC: syntax error near line %d \n", linenum);
abort();}
;
expr :'('expr')' {$$ = $2;}
| expr '+' expr {$$ = $1 + $3;}
| expr '-' expr {$$ = $1 - $3;}
| expr '*' expr {$$ = $1 * $3;}
| expr '/' expr {$$ = $1 / $3;}
| '-' expr %prec UMINUS {$$ = -$2;}
| INT {$$ = $1;}
;
%%
/* link lex code */
#include "lex.yy.c"
/* insert additional code here */
int main(){
return yyparse();
}
int yyerror(char *s) {fprintf(stderr, "%s \n",s);}
/*在此处放置新代码*/
%{
#包括
#包括
外部内部行数;
%}
/*定义所有类型的变量和端子*/
%联合{
国际价值;
}
/*定义变量和端子的各个类型*/
%标记整数
%类型表达式
/*为操作员分配优先级,以便
避免移位/减少冲突(语法歧义)*/
%左'+''-'
%左'*''/'
%左耳门
/*程序的开始变量*/
%启动程序
/*主语法及其动作*/
%%
程序:expr{printf(“expr值=%d\n”,$1);}
|错误{printf(“YACC:第%d行附近的语法错误”,linenum);
中止();}
;
expr:“('expr')”{$$=$2;}
|expr'+'expr{$$=$1+$3;}
|expr'-'expr{$$=$1-$3;}
|expr'*'expr{$$=$1*$3;}
|expr'/'expr{$$=$1/$3;}
|“-”表达式%prec-UMINUS{$$=-$2;}
|INT{$$=$1;}
;
%%
/*链接法代码*/
#包括“lex.yy.c”
/*在此处插入其他代码*/
int main(){
返回yyparse();
}
int yyerror(char*s){fprintf(stderr,“%s\n”,s);}
输出如下:
%{
#include "y.tab.h"
int linenum=1;
int temp_int;
%}
%%
\n {linenum++;}
[\t ] /* skip spaces */;
\/\/[^\n]* /* ignore comments */;
"+" {return '+';}
"-" {return '-';}
"*" {return '*';}
"/" {return '/';}
")" {return ')';}
"(" {return '(';}
[0-9]+ {sscanf(yytext, "%d", &temp_int);
yylval.int_val = temp_int;
return INT;}
. {printf("LEX: unknown input string found in line %d \n", linenum);
abort();}
%%
int yywrap(void) {return 1;}
/* Put new code here */
%{
#include <stdio.h>
#include <ctype.h>
extern int linenum;
%}
/* define all types of variables and terminals */
%union{
int int_val;
}
/* define the individual types of variables and terminals */
%token <int_val> INT
%type <int_val> expr
/* assign priorities to operators in order to
avoid shift/reduce conflicts (grammar ambiguities) */
%left '+' '-'
%left '*' '/'
%left UMINUS
/* the start variable of your program */
%start program
/* The main Grammar with its actions */
%%
program : expr {printf("Expr value = %d \n", $1);}
| error {printf("YACC: syntax error near line %d \n", linenum);
abort();}
;
expr :'('expr')' {$$ = $2;}
| expr '+' expr {$$ = $1 + $3;}
| expr '-' expr {$$ = $1 - $3;}
| expr '*' expr {$$ = $1 * $3;}
| expr '/' expr {$$ = $1 / $3;}
| '-' expr %prec UMINUS {$$ = -$2;}
| INT {$$ = $1;}
;
%%
/* link lex code */
#include "lex.yy.c"
/* insert additional code here */
int main(){
return yyparse();
}
int yyerror(char *s) {fprintf(stderr, "%s \n",s);}
您看到这个问题是因为词法分析器的源代码包含了两次 当同时使用lex和yacc(或flex和bison)时,生成的编译器(yacc输出)通常包含生成的词法分析器源 您可以在
try1.y
和输出try1.tab.c
代码中看到这一点:
/* link lex code */
#include "lex.yy.c"
/* insert additional code here */
因此,要么从try1.tab.c
中手动删除它,要么不要将lex.yy.c
与try1.tab.c
一起编译
可能只是:
$ flex try1.l
$ bison -d try1.y
$ gcc -g -Wall try1.tab.c -o try1.exe
将帮助您继续。请不要发布错误日志的屏幕截图,它们是不可搜索的。当然,谢谢您提供的信息。