【问题标题】:yydestruct too few arguments to function call (flex&bison)yydestruct 函数调用的参数太少 (flex&bison)
【发布时间】:2020-07-11 17:47:52
【问题描述】:

我正在尝试制作一个可重入的 flex&bison 解析器,但出现了这个奇怪的错误:

too few arguments to function call, expected 5, have 4

我可以看到 Bison 生成的代码是这样的:

static void
yydestruct (const char *yymsg,
            yysymbol_kind_t yykind, YYSTYPE *yyvaluep, void *scanner, struct BisonOutput *out)
{  ...some code... }

和

int
yyparse (void *scanner, struct BisonOutput *out)
{
  ...some code...
 yydestruct ("Cleanup: discarding lookahead",
                  yytoken, &yylval, out);   // <--- here void*scanner parameter is clearly missing 
  ...some code...
}

我的代码是这样的:

%define api.pure full
%lex-param {void *scanner}
%parse-param {void *scanner, struct BisonOutput *out}
%{
struct BisonOutput{
    int out; 
};
#include "syntax_parser.h"
#include "lex.yy.h"
#include <stdio.h>
%}
%define api.value.type union
%token <int> NUM
...bunch of other tokens...
%%
...bunch of grammar rules...
%%
... main function and such ...

而Flex代码如下:

%{ 
#include "syntax_parser.h"
%} 
%option reentrant  bison-bridge noyywrap
blanks          [ \t\n]+ 
number          [0-9]+
%option noyywrap  
%% 
... bunch of rules ...

我真的迷路了。为什么野牛不将scanner 插入yydestruct,尽管在yyparse 中明确使用它?

【问题讨论】:

  • syntax_parser.h 和 lex.yy.h 是什么?

标签: c bison flex-lexer reentrancy


【解决方案1】:

您不能在%*-param 声明中放置两个参数。生成所需参数集的正确方法是:

%param { void* scanner }
%parse-param { struct BisonOutput* out }

Bison 不会真正解析 { 和 } 之间的代码。它所做的只是识别它假定的最后一个标识符是参数的名称。它还假设代码是单个参数的语法正确声明,并且它被插入原型中。由于它实际上是两个参数,因此可以毫无问题地将其插入原型中,但由于仅将一个参数插入到函数调用中,因此这些与原型不匹配。

(真的,void* scanner 应该是yyscan_t scanner,之前是typedef void* yyscan_t;。但也许并不是真的更好。)

您也可以考虑将struct BisonOutput 的声明放入%code requires(或%code provides)块中,以便它自动包含在bison 生成的头文件中。

【讨论】:

    猜你喜欢
    • 2017-04-01
    • 1970-01-01
    • 2011-11-15
    • 2017-10-25
    • 1970-01-01
    • 2017-08-18
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多