如何在flex中使用REJECT?

发布于 2024-12-05 07:50:51 字数 374 浏览 5 评论 0原文

让我们明确一下,我正在谈论词法分析工具。

对于以下输入:

aa bb cc dd

我需要解析器来匹配以下子字符串:

aa bb

bb cc

cc dd

我有这个正则表达式:

[a-zA-Z]+[ ]+[a-zA-Z]+

但这只匹配:

aa bb

<代码>抄送dd

我被告知可以使用 REJECT 命令来获取我需要的输出,但我不知道如何应用它。有人可以帮我吗?

Let's be clear I'm talking about the lexical analysis tool.

For the following input:

aa bb cc dd

I need the parser to match the following sub strings:

aa bb

bb cc

cc dd

I have this regex:

[a-zA-Z]+[ ]+[a-zA-Z]+

But this only matches:

aa bb

cc dd

I've been told the REJECT command can be used to get the output I need but I don't know how to apply it. Could someone give me a hand?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界和平 2024-12-12 07:50:51

REJECT 将重新开始扫描,然后您只需“吃掉”已经匹配的第一个字母字符串即可。可能这就是您想要的(10 年前:-):

%%
[a-zA-Z]+[ ]+[a-zA-Z]+    { printf("found %s\n", yytext); REJECT; }
[a-zA-Z]+[ ]*

给定输入字符串:

a b c d e

它将产生:

found a b
found b c
found c d
found d e

REJECT will start the scan over, then you just need to "eat" the first alphabetic string that you already matched. Possibly this is what you wanted (10 years ago :-):

%%
[a-zA-Z]+[ ]+[a-zA-Z]+    { printf("found %s\n", yytext); REJECT; }
[a-zA-Z]+[ ]*

Given the input string:

a b c d e

it will produce:

found a b
found b c
found c d
found d e
魔法唧唧 2024-12-12 07:50:51

您可以使用正则表达式枚举整个集合中的 n 和 n + 1 对。

查看具有“贪婪”向后(或向前)匹配的分组。

You can use a regex to enumerate n and n + 1 pairs through an entire set.

Look into a grouping with a "greedy" backwards (or forwards) match.

嗳卜坏 2024-12-12 07:50:51

这是 Flex 解决该问题的一种方法:

    %%
    "aa bb"         {printf( "matched aa bb\n");REJECT;}
    "bb cc"         {printf( "matched bb cc\n");REJECT;}
    "cc dd"         {printf( "matched cc dd\n");REJECT;}
    .               /*ignore anything else*/
    %%
    int main()
    {
    yylex();
    return 0;
    }


When I run the code on your input I get :
matched aa bb
matched bb cc
matched cc dd

This is one Flex solution to the problem :

    %%
    "aa bb"         {printf( "matched aa bb\n");REJECT;}
    "bb cc"         {printf( "matched bb cc\n");REJECT;}
    "cc dd"         {printf( "matched cc dd\n");REJECT;}
    .               /*ignore anything else*/
    %%
    int main()
    {
    yylex();
    return 0;
    }


When I run the code on your input I get :
matched aa bb
matched bb cc
matched cc dd
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文