如何在 lex 中使用后行断言?

发布于 2024-10-31 21:34:28 字数 804 浏览 9 评论 0原文

我需要 lex (flex 2.5.35) 中的积极后向断言。在研究了文档之后,我没有看到直接的方法来做到这一点。它有类似于前瞻断言(r/s 语法)的东西,但不是后瞻断言。达到相同效果的最佳方法是什么?

这是一个例子:假设我的扫描仪规范文件中有以下规则:

a         printf("matched a ");
b         printf("matched b ");
c         printf("matched c ");
d         printf("matched d ");

我如何匹配“b”后面的“d”以及“b”本身,以便在输入“abd”时我会得到:

matched a matched b matched d following b

但对于字符串 'acd'

matched a matched c matched d

规则:

bd        printf("matched d following b ");

显然不起作用,因为它消耗了 b;对于“abd”,它输出:

matched a matched d following b

如果我有 pcre Lookbehinds,我可以写:

(?<=b)d   printf("matched d following b ");

一切都会好起来,但 lex 不支持这一点。

I require positive lookbehind assertions in lex (flex 2.5.35). After investigating the documentation, I don't see a direct way to do this. It has something similar to a lookahead assertion (the r/s syntax), but not lookbehind. What's the best way to achieve the same effect?

Here's an example: Say I have the folling rules in my scanner spec file:

a         printf("matched a ");
b         printf("matched b ");
c         printf("matched c ");
d         printf("matched d ");

How would I match a 'd' following a 'b', and the 'b' itself, so that on an input of 'abd' I would get:

matched a matched b matched d following b

But for a string 'acd'

matched a matched c matched d

The rule:

bd        printf("matched d following b ");

obviously doesn't work since it consumes the b; for 'abd' it outputs:

matched a matched d following b

If I had pcre lookbehinds I could write:

(?<=b)d   printf("matched d following b ");

and all would be well, but lex doesn't support this.

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

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

发布评论

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

评论(3

淑女气质 2024-11-07 21:34:28

您可能可以通过启动条件实现您想要的效果,但代价是更复杂的扫描仪。开始条件允许您根据之前匹配的内容有条件地启用规则。这是一个例子:

%x matched_b
%%
<INITIAL,matched_b>{
    a       { printf("matched a\n"); BEGIN(INITIAL); }
    b       { printf("matched b\n"); BEGIN(matched_b); }
    c       { printf("matched c\n"); BEGIN(INITIAL); }
}

d       printf("matched d\n");
<matched_b>d    { printf("matched d following b\n"); BEGIN(INITIAL); }

使用这个扫描仪我得到:

$ echo abcd | ./5615080
matched a
matched b
matched c
matched d

$ echo abdd | ./5615080
matched a
matched b
matched d following b
matched d

You can probably achieve what you want with start conditions, at the cost of a more complicated scanner. Start conditions allow you to conditionally enable rules based on what has matched previously. Here is an example:

%x matched_b
%%
<INITIAL,matched_b>{
    a       { printf("matched a\n"); BEGIN(INITIAL); }
    b       { printf("matched b\n"); BEGIN(matched_b); }
    c       { printf("matched c\n"); BEGIN(INITIAL); }
}

d       printf("matched d\n");
<matched_b>d    { printf("matched d following b\n"); BEGIN(INITIAL); }

With this scanner I get:

$ echo abcd | ./5615080
matched a
matched b
matched c
matched d

$ echo abdd | ./5615080
matched a
matched b
matched d following b
matched d
狼亦尘 2024-11-07 21:34:28

如果你只有一个前瞻断言,我可以想到两种可能性。

  1. 反转您的字符串,并搜索您也反转的模式。通常在您的模式之前的内容现在位于它的前面。

  2. 我认为对于某些正则表达式风格,可以从右向左解析,也许这可以帮助您进行前瞻。

否则,您应该发布一些示例字符串以及您期望的结果,也许可以在不使用后向断言的情况下实现。

I can think of two possibilities, if you have only a lookahead assertion.

  1. Invert your string, and search for your also inverted pattern. What normally was before your pattern is now ahead of it.

  2. I think for some regex flavors it is possible to parse from right to the left, maybe this can help you together with your lookahead.

Otherwise you should post some example strings and what you expect as result, perhaps it can be achieved without using look behind assertions.

递刀给你 2024-11-07 21:34:28

对于 PCRE,后视语法如下:

(?<= ... ) 表示正
(? 负

且 PCRE 需要固定长度的后视(正或负)。

如果 lex 有它们,它可能是这种形式。

For pcre the look behind syntax is this:

(?<= ... ) for positive
(?<! ... ) negative

And pcre requires fixed length look behinds (positive or negative).

If lex has them, it is probably of this form.

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