从Flex输出中删除默认的YY_DECL [英] Remove default YY_DECL from Flex output

查看:220
本文介绍了从Flex输出中删除默认的YY_DECL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当手动设置编译器读取头文件的顺序时,我能够避开此问题.在这种情况下,我可以使用正确的定义来欺骗默认的珍珠",但是当我无法控制包含标题的顺序时,这...工程曲面的天才:

I was able to sidestep this problem when manually setting up the order in which header files are read by the compiler. In this case, I'm able to spoof the default "pearl" with the correct definition, but when I have no control of the order in which headers are included, this... genius of engineering surfaces:

/* Default declaration of generated scanner - a define so the user can
 * easily add parameters.
 */
#ifndef YY_DECL
#define YY_DECL_IS_OURS 1
/* %if-c-only Standard (non-C++) definition */

extern int yylex \
               (YYSTYPE * yylval_param,YYLTYPE * yylloc_param ,yyscan_t yyscanner);

#define YY_DECL int yylex \
               (YYSTYPE * yylval_param, YYLTYPE * yylloc_param , yyscan_t yyscanner)
/* %endif */
/* %if-c++-only C++ definition */
/* %endif */
#endif /* !YY_DECL */

这是在 header 文件中定义的,但是我对YY_DECL的定义被复制到了*.c文件中.

This is defined in the header file, but my definition of YY_DECL is copied into *.c file.

文档说:

(如果您的环境支持函数原型,则它将为"int yylex(void").)可以通过定义"YY_DECL"宏来更改此定义.例如,您可以使用:

(If your environment supports function prototypes, then it will be "int yylex( void )".) This definition may be changed by defining the "YY_DECL" macro. For example, you could use:

#define YY_DECL float lexscan( a, b ) float a, b;

为扫描例程命名为lexscan,返回一个浮点数,并以两个浮点数作为参数.注意,如果使用K& R样式/非原型函数声明为扫描例程提供参数,则必须使用分号(;;)终止定义.

to give the scanning routine the name lexscan, returning a float, and taking two floats as arguments. Note that if you give arguments to the scanning routine using a K&R-style/non-prototyped function declaration, you must terminate the definition with a semi-colon (`;').

是的,是的,正是我所缺少的!我将如何生活而不能够使用七十年代就死掉的非标准C语法?但是,文档公然地说明了如果声明YY_DECL会发生什么:实际上,它会被忽略,除非您设法在编译由Flex生成的任何代码之前欺骗编译器使用另一个标头.

Yes, right, just what I was missing! How would I live without being able to use non-standard C syntax that died in the seventies? Yet, the documentation blatantly lies about what happens if you declare YY_DECL: in reality it gets ignored, unless you manage to spoof a different header to the compiler before any of the code generated by Flex would have been compiled.

我现在要编写一个sed调用来修补Flex的输出.请告诉我,实际上可以在没有这种仪器"的情况下对其进行修复.

I'm at the point now where I'd just write a sed call to patch the output of Flex. Please tell me it is actually possible to fix it without this kind of "instrumentation".

推荐答案

如果要更改原型以添加包含额外状态的参数,通常使用Flex的功能,yyextra.这样避免了YY_DECL的烦恼.

If you want to change the prototype to add a parameter containing extra state, it's usually easier to use Flex's "extra data" feature, yyextra. That avoids the irritations of YY_DECL.

另一方面,如果您想更改生成的词法分析器的名称(例如,因为您要导出一个名为yylex的包装器),则可以使用一种粗略而有效的技术

On the other hand, if you want to change the name of the generated lexer (because, for example, you want to export a wrapper called yylex), then a crude but effective technique is to put

#define yylex my_name

在序言和

#undef yylex

在结语中. (显然,这些不应该放在头文件中.)

in the epilogue. (Obviously these shouldn't be placed in header files.)

我同意YY_DECL宏与配置yylex原型的理想方式相去甚远.我一直抱怨,一旦有了yylex的参数(就像使用可重入词法分析器一样),就需要正确命名它们,并且没有记录正确的名称,因此可能会发生变化,因此将来在等待中不兼容.

I agree that the YY_DECL macro is far from an ideal way of configuring the yylex prototype. My complaint has always been that once you have parameters to yylex (as you do with reentrant lexers) then you need to get their names right, and the correct names aren't documented, and therefore are subject to change, making for a future incompatibility in waiting.

这篇关于从Flex输出中删除默认的YY_DECL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆