简体   繁体   English

如何编写野牛文件以自动使用C头文件中定义的标记枚举列表?

[英]How to write a bison file to automatically use a token enumeration list define in a C header file?

I am trying to build a parser with Bison/Yacc to be able to parse a flow of token done by another module. 我正在尝试使用Bison / Yacc构建一个解析器,以便能够解析另一个模块完成的令牌流。 The tokens are already listed in a enumeration type as follow: 这些令牌已经以枚举类型列出,如下所示:

// C++ header file
enum token_id {
  TokenType1         = 0x10000000,
  TokenType2         = 0x11000000,
  TokenType3         = 0x11100000,
  //... and the list go on with about 200/300 line
};

I have gone through the documentation of bison many times but I couldn't find a better solution than copying each token in the Bison file like this: 我已经遍历了bison的文档很多次,但没有找到比复制Bison文件中的每个令牌更好的解决方案了,如下所示:

/* Bison/Yacc file */
%token TokenType1 0x10000000
%token TokenType2 0x11000000
%token TokenType3 0x11100000
//...

If I have to do it like that, It will become pretty hard to maintain the file if the other module specification change (which happen quite oftenly). 如果我必须那样做,那么如果其他模块规范发生更改(经常发生),则很难维护该文件。

Could you please tell me how to do it, or point me in the good direction (any idea/comment is welcome). 您能否告诉我该怎么做,或指出正确的方向(欢迎提出任何想法/意见)。 It would greatly help me! 这将对我有很大帮助! Thanks in advance. 提前致谢。

Instead of doing : 而不是做:

/* Bison/Yacc file */
%token TokenType1 0x10000000
%token TokenType2 0x11000000
%token TokenType3 0x11100000
//...

You just need to include the file with the token type in the declaration part 您只需要在声明部分中包含带有令牌类型的文件

#include "mytoken_enum.h"
// ...
%token TokenType1
%token TokenType2
%token TokenType3 
//...

EDIT: This can not be done: 编辑:这不能完成:

As you see from the numbers above, Bison just numbers the tokens sequentially, and it is used shifted in parser lookup tables as indices, for speed simply. 从上面的数字中可以看到,Bison只是按顺序对令牌进行编号,并且在解析器查找表中将其移位用作索引,只是为了提高速度。 So Bison does not support that, I feel sure, and it would not be easy to fit with the implementation model. 因此,我敢肯定,Bison不支持这一点,并且要实现模型不容易。

Just need to wrapper to convert the real token to yacc/bison token (eg: via yylex()) 只需包装即可将真实令牌转换为yacc / bison令牌(例如:通过yylex())

The obvious method would be a small utility to convert from one format to the other. 显而易见的方法是一个小的实用程序,可以从一种格式转换为另一种格式。 If you're really making changes quite frequently, you might even consider storing the names and values in something like a SQL database, and write a couple of queries to produce the output in the correct format for each tool. 如果您确实经常进行更改,则甚至可以考虑将名称和值存储在SQL数据库之类的文件中,并编写几个查询以针对每个工具以正确的格式生成输出。

select token_name, '=' token_number ','
    from token_table

select '%token ', token_name, ' ', token_number
    from token_table

The first would require a bit of massaging, such as adding the 'enum token_id {" to the beginning, and "};" to the end, but you get the general idea. Of course, there are lots of alternatives -- XML, CSV, etc., but the general idea remains the same: store and edit as close to raw data as possible, and automate adding the extra "stuff" necessary to keep the tools happy. 第一种方法需要进行一些按摩,例如将'enum token_id {“添加到开头,然后将”};“添加到结尾,但是您已经大致了解了。当然,有很多替代方法-XML, CSV等格式,但总体思路保持不变:尽可能存储和编辑尽可能接近原始数据的文件,并自动添加必要的“材料”以使工具满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM