yylex 的多重定义
Multiple definition of yylex
我已经尝试了下面的 lex 和 yacc 文件,它似乎对许多函数有多个定义,例如 yylex、yyrestart 等等。试了几种方法和网上的资料都无法解决。任何帮助都将不胜感激。
try1.l 文件如下,
%{
#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;}
try1.y 文件如下,
/* 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", );}
| error {printf("YACC: syntax error near line %d \n", linenum);
abort();}
;
expr :'('expr')' {$$ = ;}
| expr '+' expr {$$ = + ;}
| expr '-' expr {$$ = - ;}
| expr '*' expr {$$ = * ;}
| expr '/' expr {$$ = / ;}
| '-' expr %prec UMINUS {$$ = -;}
| INT {$$ = ;}
;
%%
/* 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 输出)通常 #include
是生成的词法分析器源。
您可以在 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
会助你一臂之力。
我已经尝试了下面的 lex 和 yacc 文件,它似乎对许多函数有多个定义,例如 yylex、yyrestart 等等。试了几种方法和网上的资料都无法解决。任何帮助都将不胜感激。
try1.l 文件如下,
%{
#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;}
try1.y 文件如下,
/* 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", );}
| error {printf("YACC: syntax error near line %d \n", linenum);
abort();}
;
expr :'('expr')' {$$ = ;}
| expr '+' expr {$$ = + ;}
| expr '-' expr {$$ = - ;}
| expr '*' expr {$$ = * ;}
| expr '/' expr {$$ = / ;}
| '-' expr %prec UMINUS {$$ = -;}
| INT {$$ = ;}
;
%%
/* 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 输出)通常 #include
是生成的词法分析器源。
您可以在 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
会助你一臂之力。