我正在尝试在flex中处理一个配置文件,如下所示
[Tooldir]
BcLib=C:APPSBCLIB
BcInclude=C:APPSBCINCLUDE
[IDE]
DefaultDesktopDir=C:APPSBCBIN
HelpDir=C:APPSBCBIN
[Startup]
State=0
Left=21
Right=21
Width=946
Height=663
[Project]
Lastproj=c:appsbcbinproj0002.ide
所以它看起来像这样
[Tooldir]
[IDE]
[Startup]
[Project]
我目前正在尝试使用状态,但我似乎不了解它们是如何工作的。
%{
#include <stdio.h>
int yywrap(void);
int yylex(void);
%}
%s section
%%
/* == rules == */
<INITIAL>"[" BEGIN section;
<section>. printf("%s",yytext);
<section>"]n" BEGIN INITIAL;
%%
int yywrap(void) { return 1; }
int main() { return yylex(); }
上面的代码正在打印除"["和"]"之外的所有内容...请帮忙吗?
编辑:
工作代码
%{
#include <stdio.h>
int yywrap(void);
int yylex(void);
%}
%s section
%%
/* == rules == */
<INITIAL>"[" BEGIN section; printf("[");
<section>. printf("%s",yytext);
<section>"]n" BEGIN INITIAL; printf("]n");
.|n {;}
%%
int yywrap(void) { return 1; }
int main() { return yylex(); }
默认情况下,将打印与任何 flex 规则不匹配的任何内容。 因此,您的规则与[whatever]
行和打印whatever
匹配(删除[
和]
),而默认规则与其他所有规则(打印)匹配。
添加如下规则:
.|n { /* ignoring all other unmatched text */ }
到规则的末尾,如果你想忽略其他所有内容,而不是打印它。