为bison和flex编写问题
我是一个刚接触flex和bison的新手,我有一个小问题。一旦我得到了我的lex.yy.c文件和我的tab.c文件,当我编译lex.yy.c文件时,我得到了错误:为bison和flex编写问题,bison,flex-lexer,Bison,Flex Lexer,我是一个刚接触flex和bison的新手,我有一个小问题。一旦我得到了我的lex.yy.c文件和我的tab.c文件,当我编译lex.yy.c文件时,我得到了错误: stojk_2.l: In function ‘int yylex()’: stojk_2.l:3: error: ‘PLUS’ was not declared in this scope stojk_2.l:4: error: ‘MINUS’ was not declared in this scope stojk_2.l:5:
stojk_2.l: In function ‘int yylex()’:
stojk_2.l:3: error: ‘PLUS’ was not declared in this scope
stojk_2.l:4: error: ‘MINUS’ was not declared in this scope
stojk_2.l:5: error: ‘MULT’ was not declared in this scope
stojk_2.l:6: error: ‘DIVIDE’ was not declared in this scope
stojk_2.l:8: error: ‘LPAREN’ was not declared in this scope
stojk_2.l:9: error: ‘RPAREN’ was not declared in this scope
stojk_2.l:12: error: ‘yylval’ was not declared in this scope
stojk_2.l:13: error: ‘UNSIGNEDINTEGER’ was not declared in this scope
编译tab.c文件时,会出现以下错误:
stojk_3.y: In function ‘void yyerror(char*)’:
stojk_3.y:12: error: ‘print’ was not declared in this scope
stojk_3.tab.c: At global scope:
stojk_3.tab.c:1056: error: redefinition of ‘double yylval’
stojk_3.y:8: error: ‘double yylval’ previously declared here
stojk_3.tab.c: In function ‘int yyparse()’:
stojk_3.tab.c:1253: error: ‘yylex’ was not declared in this scope
stojk_3.tab.c:1401: warning: deprecated conversion from string constant to ‘char*’
stojk_3.tab.c:1547: warning: deprecated conversion from string constant to ‘char*’
他们似乎不能见面,但我把他们放在同一个文件夹里,所以我不知道我该怎么办……任何帮助都将不胜感激
感谢大家的帮助,他们仍在努力解决问题,但以下是我的代码:
stojk_2.l
%%
"+" {return PLUS;}
"-" {return MINUS;}
"*" {return MULT;}
"/" {return DIVIDE;}
"(" {return LPAREN;}
")" {return RPAREN;}
[0-9]+ {
sscanf(yytext, "%lf", &yylval);
return UNSIGNEDINTEGER;
}
[ \t] { }
[\n] {return yytext[0];}
. {return yytext[0];}
%%
int yywrap()
{
return 0;
}
stojk_3.y
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define YYSTYPE double
YYSTYPE yylval;
void yyerror(char *s)
{
print("yyerror: %s\n", s);
}
%}
%token PLUS
%token MINUS
%token MULT
%token DIVIDE
%token LPAREN
%token RPAREN
%token UNSIGNEDINTEGER
%left PLUS MINUS
%left MULT DIVIDE
%%
lines : lines expr '\n' {printf("%g\n", $2);}
| lines '\n'
| /*empty*/
;
expr : expr PLUS expr {$$ = $1 + $3;}
| expr MINUS expr {$$ = $1 - $3;}
| expr MULT expr {$$ = $1 * $3;}
| expr DIVIDE expr {$$ = $1 / $3;}
| LPAREN expr RPAREN {$$ = $2;}
| UNSIGNEDINTEGER
;
%%
#include "lex.yy.c"
int yylex();
int yyparse(void);
int main()
{
return yyparse();
}
%{
#包括
#包括
#包括
#定义YYSTYPE双精度
YYSTYPE yylval;
无效错误(字符*s)
{
打印(“YY错误:%s\n”,s);
}
%}
%代币加号
%代币负号
%代币骡子
%代币分割
%令牌LPAREN
%令牌RPAREN
%令牌无符号整数
%左加减
%左多分裂
%%
行:行expr'\n'{printf(“%g\n”,$2);}
|行“\n”
|/*空*/
;
expr:expr加上expr{$$=$1+$3;}
|expr减去expr{$$=$1-$3;}
|expr MULT expr{$$=$1*$3;}
|expr除以expr{$$=$1/$3;}
|LPAREN expr RPAREN{$$=$2;}
|无符号整数
;
%%
#包括“lex.yy.c”
int-yylex();
int(void);
int main()
{
返回yyparse();
}
您需要从定义常量的bison
生成标题。您需要在词法分析器中包含该标题
bison -d stojk_3.y # generates stojk_3.tab.c and stojk_3.tab.h
因此,您需要在Flex代码中包含stojk_3.tab.h
print()
函数可能是printf()
的打字错误;它当然不是一个标准函数
yylval
的多重定义很可能是因为您定义了它,但不需要这样做(Bison会为您这样做)。您可能需要声明extern int yylex(void)代码>或代码中的等效项。转换投诉意味着您将一个文本字符串传递给一个接受char*
的函数(但字符串在形式上是不可修改的,因此应该声明并定义该函数以接受const char*
)
如果看不到更多的代码,就很难更加具体
根据提供的代码
正如预测的那样,print()
的问题是printf()
的打字错误。有一个强有力的参数应该是fprintf(stderr,…)
;错误应该转到stderr
,而不是stdout
您不需要定义yylval
;把那句话注释掉
我声明int-yylex(无效)代码>位于语法文件的顶部(在%{…%}
部分)
我把yyerror()
变成了一个静态函数。我得到了两个警告:
gcc -O3 -g -std=c99 -Wall -Wextra -Wstrict-prototypes stojk_3.tab.c -o stojk_3.tab
In file included from stojk_3.y:50:0:
lex.yy.c:1112:17: warning: ‘yyunput’ defined but not used [-Wunused-function]
lex.yy.c:1153:16: warning: ‘input’ defined but not used [-Wunused-function]
固定代码
%{
#包括
#包括
#包括
#定义YYSTYPE双精度
int yylex(无效);
静止的
无效错误(字符*s)
{
printf(“YY错误:%s\n”,s);
}
%}
%代币加号
%代币负号
%代币骡子
%代币分割
%令牌LPAREN
%令牌RPAREN
%令牌无符号整数
%左加减
%左多分裂
%%
行:行expr'\n'{printf(“%g\n”,$2);}
|行“\n”
|/*空*/
;
expr:expr加上expr{$$=$1+$3;}
|expr减去expr{$$=$1-$3;}
|expr MULT expr{$$=$1*$3;}
|expr除以expr{$$=$1/$3;}
|LPAREN expr RPAREN{$$=$2;}
|无符号整数
;
%%
#包括“lex.yy.c”
int yylex(无效);
int(void);
内部主(空)
{
返回yyparse();
}
我没有收到关于非常量字符指针的警告;我不确定问题出在哪里。第一组错误似乎是您未能将.tab.h文件包含在.l文件中。第二组错误看起来是您的.y文件有问题,但如果不查看.y文件,很难准确判断问题所在。刚刚添加了my.l和.y文件啊;你的#包括“lex.yy.c”
…很有趣!不是很标准。只是添加了我的.l和.y文件
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define YYSTYPE double
int yylex(void);
static
void yyerror(char *s)
{
printf("yyerror: %s\n", s);
}
%}
%token PLUS
%token MINUS
%token MULT
%token DIVIDE
%token LPAREN
%token RPAREN
%token UNSIGNEDINTEGER
%left PLUS MINUS
%left MULT DIVIDE
%%
lines : lines expr '\n' {printf("%g\n", $2);}
| lines '\n'
| /*empty*/
;
expr : expr PLUS expr {$$ = $1 + $3;}
| expr MINUS expr {$$ = $1 - $3;}
| expr MULT expr {$$ = $1 * $3;}
| expr DIVIDE expr {$$ = $1 / $3;}
| LPAREN expr RPAREN {$$ = $2;}
| UNSIGNEDINTEGER
;
%%
#include "lex.yy.c"
int yylex(void);
int yyparse(void);
int main(void)
{
return yyparse();
}