作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 flex
生成词法分析器。这是我的定义文件lexer.l
:
%{
#include <iostream>
using namespace std;
//#define YY_DECL extern "C" int yylex()
%}
staffType "grand" | "treble" | "bass" | "alto" | "tenor"
upperRomans "I" | "II" | "III" | "IV" | "V" | "VI" | "VII"
lowerRomans "i" | "ii" | "iii" | "iv" | "v" | "vi" | "vii"
quality "dim" | "halfdim" | "aug" | "maj" | "min"
%%
[ \t\n]+ { ; // Ignore arbitrary whitespace. }
{staffType} { cout << "Staff" << endl; }
{upperRomans} { cout << "Upper roman" << endl; }
{lowerRomans} { cout << "Lower roman" << endl; }
"doublebar" { cout << "End of line" << endl; }
. { cout << "Parse error" << endl; }
%%
int main(int, char**) {
// lex through the input
yylex();
}
但是,在调用之后:
flex lexer.l
我得到:
lexer.l:18: unrecognized rule
lexer.l:19: unrecognized rule
lexer.l:20: unrecognized rule
我的 flex 版本是 flex 2.5.35 Apple(flex-31)
。
我做错了什么?
最佳答案
问题在于模式中不同标记之间的空格。它必须是:
[...]
staffType "grand"|"treble"|"bass"|"alto"|"tenor"
upperRomans "I"|"II"|"III"|"IV"|"V"|"VI"|"VII"
lowerRomans "i"|"ii"|"iii"|"iv"|"v"|"vi"|"vii"
quality "dim"|"halfdim"|"aug"|"maj"|"min"
[...]
它写在 manpage of flex 中.
PATTERNS
The patterns in the input are written using an extended set of regular expressions. These are:
[...]
r|s either an r or an s
关于c++ - 柔性 : trying to generate a C++ lexer using Flex; "unrecognized rule" error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45705958/
我是一名优秀的程序员,十分优秀!