匹配此文本,但只有在文件末尾出现
Flex用户手册的第78页说:
没有办法编写“匹配此文本的规则”,但仅当 它出现在文件的末尾“。但是,如果您可以伪造它 碰巧有一个角色,您不允许您 输入。然后,您可以重新定义yy_input以调用自己的例行程序, 如果看到一个EOF,请首先返回魔术角色(并记得 下次返回真正的EOF。
我正在尝试实施这种方法。实际上,我设法使它起作用(见下文)。对于此输入:
Hello, world
How are you?@
我得到了此(正确的)输出:
Here's some text Hello, world
Saw this string at EOF How are you?
但是我必须在实施中做两件事才能使它起作用;我不必做的两件事:
我不得不调用yyterminate()
。如果我不调用yyternate()
,那么输出就是这样:
Here's some text Hello, world
Saw this string at EOF How are you?
Saw this string at EOF
我不应该得到最后一行。为什么我要获得最后一行?
我不明白为什么要这样做:tmp [yyleng-1] ='\ 0';
(减去1)。我应该能够做到这一点:tmp [yyleng] ='\ 0';
(不减少1)为什么我需要减去1?
%option noyywrap
%{
int sawEOF = 0;
#define YY_INPUT(buf,result,max_size) \
{ \
if (sawEOF == 1) \
result = YY_NULL; \
else { \
int c = fgetc(yyin); \
if (c == EOF) { \
sawEOF = 1; \
buf[0] = '@'; \
result = 1; \
} \
else { \
buf[0] = c; \
result = 1; \
} \
} \
}
%}
EOF_CHAR @
%%
[^\n@]*{EOF_CHAR} { char *tmp = strdup(yytext);
tmp[yyleng-1] = '\0';
printf("Saw this string at EOF %s\n", tmp);
yyterminate();
}
[^\n@]+ { printf("Here's some text %s\n", yytext); }
\n { }
%%
int main(int argc, char *argv[])
{
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
return 0;
}
Page 78 of the Flex user's manual says:
There is no way to write a rule which is "match this text, but only if
it comes at the end of the file". You can fake it, though, if you
happen to have a character lying around that you don't allow in your
input. Then you can redefine YY_INPUT to call your own routine which,
if it sees an EOF, returns the magic character first (and remembers to
return a real EOF next time it's called.
I am trying to implement that approach. In fact I managed to get it working (see below). For this input:
Hello, world
How are you?@
I get this (correct) output:
Here's some text Hello, world
Saw this string at EOF How are you?
But I had to do two things in my implementation to get it to work; two things that I shouldn't have to do:
I had to call yyterminate()
. If I don't call yyterminate()
then the output is this:
Here's some text Hello, world
Saw this string at EOF How are you?
Saw this string at EOF
I shouldn't be getting that last line. Why am I getting that last line?
I don't understand why I had to do this: tmp[yyleng-1] = '\0';
(subtract 1). I should be able to do this: tmp[yyleng] = '\0';
(not subtract 1) Why do I need to subtract 1?
%option noyywrap
%{
int sawEOF = 0;
#define YY_INPUT(buf,result,max_size) \
{ \
if (sawEOF == 1) \
result = YY_NULL; \
else { \
int c = fgetc(yyin); \
if (c == EOF) { \
sawEOF = 1; \
buf[0] = '@'; \
result = 1; \
} \
else { \
buf[0] = c; \
result = 1; \
} \
} \
}
%}
EOF_CHAR @
%%
[^\n@]*{EOF_CHAR} { char *tmp = strdup(yytext);
tmp[yyleng-1] = '\0';
printf("Saw this string at EOF %s\n", tmp);
yyterminate();
}
[^\n@]+ { printf("Here's some text %s\n", yytext); }
\n { }
%%
int main(int argc, char *argv[])
{
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论