gpt4 book ai didi

c++ - 柔性 : trying to generate a C++ lexer using Flex; "unrecognized rule" error

转载 作者:太空狗 更新时间:2023-10-29 20:52:37 27 4
gpt4 key购买 nike

我正在尝试使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com