dyl*_*unn 4 c++ lex lexer flex-lexer
我正在尝试使用生成词法分析器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();
}
Run Code Online (Sandbox Code Playgroud)
但是,调用后:
flex lexer.l
Run Code Online (Sandbox Code Playgroud)
我得到:
lexer.l:18: unrecognized rule
lexer.l:19: unrecognized rule
lexer.l:20: unrecognized rule
Run Code Online (Sandbox Code Playgroud)
我的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"
[...]
Run Code Online (Sandbox Code Playgroud)
它写在flex的手册中。
PATTERNS
在输入的模式是使用扩展的组正则表达式的写入。这些是:
[...]
r | s r或s
| 归档时间: |
|
| 查看次数: |
218 次 |
| 最近记录: |