문제

I'm trying to process in flex a config file, which looks like this

[Tooldir]
BcLib=C:\APPS\BC\LIB
BcInclude=C:\APPS\BC\INCLUDE
[IDE]
DefaultDesktopDir=C:\APPS\BC\BIN
HelpDir=C:\APPS\BC\BIN
[Startup]
State=0
Left=21
Right=21
Width=946
Height=663
[Project]
Lastproj=c:\apps\bc\bin\proj0002.ide

So it would look like this

[Tooldir]
[IDE]
[Startup]
[Project]

I'm currently trying with states, but I just don't seem to understand how they work.

%{
#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(); }

The code above is printing everything except the "[" and "]"... Some help, please?

EDIT:

Working code

%{
#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(); }
도움이 되었습니까?

해결책

By default, anything that doesn't match any of the flex rules is printed. So your rules match the [whatever] lines and print whatever (removing the [ and ]), while the default rule matches everything else (printing it).

Add a rule like:

.|\n  { /* ignoring all other unmatched text */ }

to the end of your rules if you want to ignore everything else, rather than printing it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top