【问题标题】:flex bison windows introductionflex bison windows介绍
【发布时间】:2020-02-21 07:06:14
【问题描述】:

由于我是词法分析器和解析器的新手,所以我正在尝试阅读和理解其他代码。

这是我尝试使用的代码: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

【问题讨论】:

  • 老实说,我认为您最好研究一下flex manualbison manual 中的示例。确实(但很遗憾)这些示例并未展示如何将 bison 和 flex 一起使用,但它们确实展示了如何使用这两种工具。最重要的是,手册解释示例。您在此处链接的代码根本不是使用 flex 和 bison 的良好基础,而且它无法在没有错误的情况下编译这一事实只是一个明显的症状。

标签: c parsing bison flex-lexer lexer


【解决方案1】:

简单地说:

#include <string.h>

json.l 顶部的弹性定义部分中应该为您修复它。

在您指向的存储库中还有一个Makefile。也许你应该使用它。您似乎没有正确生成解析器文件。请参阅下面的评论。

至于剩下的警告:

warning: implicit declaration of function 'yyerror';
warning: implicit declaration of function 'yylex';

这些可以通过添加yylex()yyerror 的声明来轻松解决

%{
    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);
       ^

它们更微妙一些。我建议查看here,了解如何使用yylval 将字符串从lex 的标记正确传递到解析器的操作中。现在的问题是 yylval 是一个空的 int 但它最终被分配了 char 指针用于 NUMBERSTRING 令牌。

【讨论】:

  • @Danial 剩下的是警告。您的野牛调用中的 -dy 标志似乎很奇怪 - 请注意,您正在生成 y.tab.{c,h} 但您的词法分析器需要 #include "json.tab.h"
  • @dragosht:关于yylval 在词法分析器中类型错误的警告需要认真对待。此外,在野牛序言中需要前向声明 yylex 和 yyerror。
  • @rici 我正要建议将yylexyyerror 添加到bison。至于那些yylval 的分配——我真的不能说那是什么......
  • @dragosht:它来自仅在野牛文件中重新定义 YYSTYPE。但是现代野牛有一种更好的方式来声明语义类型,它会自动将声明放入生成的头文件中。请参阅手册中的Defining Language Semantics
  • @rici 关于正确使用yylval 的主题,我会参考这篇帖子here