我有以下Lexer.l和Parser.y文件。
Lexer.l
%{
#include "Parser.h"
%}
%option yylineno
%option outfile="Lexer.cpp" header-file="Lexer.h"
%option warn nodefault
%option reentrant noyywrap never-interactive nounistd
%option bison-bridge
Parser.y
%{
#include "Parser.h"
#include "Lexer.h"
extern int yyerror(yyscan_t scanner, const char *msg)
{printf("\r\nError: %s", msg); return 1;}
%}
%code requires {
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void* yyscan_t;
#endif
}
%output "Parser.cpp"
%defines "Parser.h"
%define api.pure
%pure-parser
%lex-param { yyscan_t scanner }
%parse-param {yyscan_t scanner }
一切正常。
现在我正在尝试获取令牌的列和行;当我使用 @ 1.first_line 时,我收到以下错误:
'yylex':函数不带3个参数
'yyerror':函数不带3个参数
对于yyerror,我查看了它的编译器要求并实现了它。
但是,对于yylex,我不知道该返回什么。 我试着用2参数实现看看yylex来做类似的东西,但似乎根本没有为yylex实现。
有什么想法吗?
答案 0 :(得分:1)
如果您使用option bison-bridge
并且您的解析器有@
引用,则需要添加
%option bison-locations
到你的flex文件。 (您可以使用它而不是bison-bridge,但我认为两者兼而有之。)来自flex
manual:
--bison-locations, %option bison-locations
%locations
。这个
表示yylex
将传递一个附加参数yylloc
。
此选项隐含%option bison-bridge
。