c - flex bison windows介绍
问题描述
由于我是词法分析器和解析器的新手,所以我试图阅读和理解其他代码。
这是我尝试使用的代码:https ://gist.github.com/justjkk/436828
但这给了我错误。我该如何解决这个问题?
E:\flex_bison_test>gcc lex.yy.c y.tab.c -o json.exe
json.l: In function 'yylex':
json.l:34:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
json.l:38:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
json.l: In function 'strclone':
json.l:82:15: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
int len = strlen(str);
^~~~~~
json.l:82:15: warning: incompatible implicit declaration of built-in function 'strlen'
json.l:82:15: note: include '<string.h>' or provide a declaration of 'strlen'
json.l:79:1:
+#include <string.h>
%%
json.l:82:15:
int len = strlen(str);
^~~~~~
json.l:84:5: warning: implicit declaration of function 'strcpy' [-Wimplicit-function-declaration]
strcpy(clone,str);
^~~~~~
json.l:84:5: warning: incompatible implicit declaration of built-in function 'strcpy'
json.l:84:5: note: include '<string.h>' or provide a declaration of 'strcpy'
y.tab.c: In function 'yyparse':
y.tab.c:627:16: warning: implicit declaration of function 'yylex' [-Wimplicit-function-declaration]
# define YYLEX yylex ()
^~~~~
y.tab.c:1272:16: note: in expansion of macro 'YYLEX'
yychar = YYLEX;
^~~~~
y.tab.c:1540:7: warning: implicit declaration of function 'yyerror'; did you mean 'yyerrok'? [-Wimplicit-function-declaration]
yyerror (YY_("syntax error"));
^~~~~~~
yyerrok
json.y: At top level:
json.y:80:6: warning: conflicting types for 'yyerror'
void yyerror (char const *s) {
^~~~~~~
y.tab.c:1540:7: note: previous implicit declaration of 'yyerror' was here
yyerror (YY_("syntax error"));
^~~~~~~
E:\flex_bison_test>
或者这些应该保持原样。
我给出的所有命令:
flex json.l
bison -dy json.y
gcc lex.yy.c y.tab.c -o json.exe
解决方案
简单地:
#include <string.h>
在顶部的 flex 定义部分中json.l
应该为您修复它。
Makefile
您指向的存储库中还有一个。也许你应该使用它。您似乎没有正确生成解析器文件。请参阅下面的评论。
至于剩余的警告:
warning: implicit declaration of function 'yyerror';
warning: implicit declaration of function 'yylex';
这些可以通过添加声明来轻松修复,yylex()
并且yyerror
应该出现在bison
您顶部的序言部分中json.y
:
%{
int yylex();
void yyerror(const char *s);
%}
至于这些:
json.l:34:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
json.l:38:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
它们更微妙一些。我建议在这里看看如何yylval
正确地将字符串从 lex 的标记传递到解析器的操作中。现在的问题是它yylval
是一个裸露的int
,但它最终被分配了两个和令牌的char
指针。NUMBER
STRING
推荐阅读
- json - 在请求中提交过多 json 的 jmeter 中编写 Web 旅程脚本的最佳策略
- javascript - 如何在 javascript 对象中捕获引用循环?
- c# - Windows Workflow Foundation - 反序列化程序无法加载要反序列化的类型,因为类型
- reactjs - 使用 react-router-dom 时延迟加载不起作用
- javascript - 在其他脚本完成更改 dom 时运行脚本
- node.js - 将大文件从 Google Cloud Function 上传到 Google Cloud Storage 失败
- javascript - Laravel-mix@6.0.10 出现未知错误 Uncaught DOMException: Failed to construction 'WebSocket'
- android - Flutter InAppWebView 摄像头/音频权限
- reactjs - 无法在 useEffect 挂钩中使用反应表单数据
- reactjs - Vue 2 TSX -> React TSX .. 如何让它们玩得更好