flex 和 bison 的常见代币
我有一个包含令牌声明的文件。h:
#define ID 257
#define NUM 258
...
在我的 Flex 代码中,我返回此值或符号之一(例如“+”、“-”、“*”)。一切正常。
野牛文件中的问题。 如果我写这样的东西: exp: ID '+' ID 我会收到错误,因为 bison 不知道有关 ID 的任何信息。 添加行 %token ID 不会有帮助,因为在这种情况下我会遇到编译错误(预处理器将 ID 更改为 257,我将得到 257=257)
I have one file with declarations of my tokens declarations.h:
#define ID 257
#define NUM 258
...
In my flex code i return one of this values or symbol(for example '+', '-', '*'). And everything works.
The problem in bison file.
If i write something like that:
exp: ID '+' ID
i'll get error, because bison doesn't know anything about ID.
Adding the line %token ID will not help, because in that case i'll have compilation error(preprocessor will change ID by 257 and i'll get 257=257)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您让 Bison 创建代币列表;您的词法分析器使用 Bison 生成的列表。
然后,您的词法分析器使用
grammar.tab.h
:MacOS X 10.7.2 上的 Bison 2.4.3 将令牌编号生成为
enum
,而不是一系列#define
值 - 将标记名称放入调试器的符号表中(一个非常好的主意!)。You get Bison to create the list of tokens; your lexer uses the list generated by Bison.
Your lexer then uses
grammar.tab.h
:Bison 2.4.3 on MacOS X 10.7.2 generates the token numbers as an
enum
, not as a series of#define
values - to get the token names into the symbol table for debuggers (a very good idea!).