1 提出问题
例如, 有这样一段字符串:
VAR ics142: INTEGER; // variable declaration
假设我们想把它变成:
VAR ID(ics142) COLON ID(INTEGER) SEMICOLON
我们可以使用gnu flex.
2 准备试验环境
Fedora 30
$ sudo dnf install flex
$ sudo dnf install flex-devel
3 设置规则
// An example of using the flex C++ scanner class.
%option C++ noyywrap
%{
%}
alpha [A-Za-z]
dig [0-9]
colon :
semicolon ;
variable VAR
comment \/\/({alpha}|{ws})*
ws [ \t]+
name ({alpha}|{dig}|\$)({alpha}|{dig}|\_|\.|\-|\/|\$)*
%%
{variable} std::cout << YYText() << " ";
{ws} /* skip blanks and tabs */
{comment} /* skip blanks and tabs */
{name} std::cout << "ID(" << YYText() << ") ";
{colon} std::cout << "COLON ";
{semicolon} std::cout << "SEMICOLON ";
%%
int main( int /* argc */, char** /* argv */ )
{
FlexLexer* lexer = new yyFlexLexer;
lexer->yylex();
return 0;
}
4 编译
g++ lex.yy.cc -lfl
5 执行
echo "VAR ics142: INTEGER; // variable declaration" | ./a.out
6 总结
参考:
http://alumni.cs.ucr.edu/~lgao/teaching/flex.html
https://github.com/westes/flex/blob/master/examples/testxxLexer.l