bison - Flex- Bison : how to create Keywords?

标签 bison flex-lexer

我正在 Flex/Bison 中编写一个小程序来标记/解析我创建的查询语言。 我想知道是否有任何方法可以在 Flex/Bison 中创建任何关键字。

我的意思是:flex 将输入分解为标记列表,但有没有办法创建关键字列表,以便每次 flex 看到它们时,它都会返回单词“keyword”。

或者以下是执行此操作的唯一方法:

"dog"|"cat"     return KEYWORD;

flex/bison 是否可以使用任何数据结构,以便每次看到该数据结构的成员时,它都会将其识别为关键字?

谢谢, 莎拉

最佳答案

我认为更好的方法是用 Bison 来处理这个问题,如下所示:

在弹性中:

"dog" { return T_DOG; }
"cat" { return T_CAT; }
...

在 bison 中,您可以设置接受任何这些标记的规则:

keyworks: T_DOG | T_CAT | ... ;

other_rule: keyworks T_ACTION;

关于bison - Flex- Bison : how to create Keywords?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10301397/

相关文章:

cmake - 某处是否有可用的 cmake 解析器/词法分析器(在 bison/flex 中)?

Bison 提示 "conflicts: 1 shift/reduce"

c - 在 C 中运行时解析 float 的标准方法是什么?

c++ - Lex 解析器 : (end of buffer or a NUL) segfault

计数 C 指令

c - Lex:仅由其他单词的字母组成的最长单词

c++ - Bison:$$ 和 $1 指向相同的内存位置(两种指针类型的 union YYSTYPE)

c++ - 弹性和 Bison : string literal

parsing - Flex/Lex 和 Yacc/Bison 有什么区别?

postgresql - Ubuntu:如何安装 flex 来制作 postgres?