我是这个弹性和野牛的新手,我有一个小问题。一旦我得到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: 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();
}
答案 0 :(得分:1)
第一组错误看起来好像是{。1}} .l文件中的.tab.h文件。第二组错误看起来像你的.y文件有问题,但很难在不看到.y文件的情况下确切地告诉你什么。
答案 1 :(得分:0)
您需要从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 *
)。 / p>
如果没有看到更多代码,就很难更具体。
正如预测的那样,print()
的问题是printf()
的拼写错误。有一个强烈的论点应该是fprintf(stderr, ...)
;错误应该转到stderr
而不是stdout
。
您无需定义yylval
;注释掉那条线。
我在语法文件的顶部(int yylex(void);
部分)声明了%{ ... %}
。
我将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]
%{
#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();
}
我没有得到关于非常量字符指针的警告;我不确定那里有什么问题。