在Flex中匹配尾随上下文 [英] Matching trailing context in flex

查看:69
本文介绍了在Flex中匹配尾随上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

flex手册中,它提到了跟踪上下文"模式( r/s),表示r,但前提是紧随其后的是s.但是,以下代码无法编译(相反,它会给出无法识别的规则"错误.为什么?

In the flex manual it mentions a "trailing context" pattern (r/s), which means r, but only if followed by s. However the following code doesn't compile (instead it gives an error of "unrecognized rule". Why?

LITERAL a/b
%%
{LITERAL} { }

推荐答案

简单的答案是,除非使用-l选项(不推荐使用 ),否则不能将尾随的上下文放入名称定义.那是因为flex:

The simple answer is that unless you use the -l option, which is not recommended, you cannot put trailing context into a name definition. That's because flex:

  • 在括号内不允许尾随上下文;和

  • doesn't allow trailing context inside parentheses; and

自动将定义的扩展用括号括起来,除了在某些情况下(请参见下文).

automatically surrounds expansions of definitions with parentheses, except in a few situations (see below).

flex用括号括住扩展的原因是,否则会发生奇怪的事情.例如:

The reason flex surrounds expansions with parentheses is that otherwise weird things happen. For example:

prefix        milli|centi
%%
{prefix}pede  return BUG;

没有自动括号,该模式将扩展为:

Without the automatic parentheses, the pattern would expand to:

milli|centipede

,它与millipede不匹配. (各种后缀运算符也存在类似的问题.例如,考虑{prefix}?pede.)

which would not match millipede. (There's a similar problem with the various postfix operators. Consider {prefix}?pede, for example.)

Flex不允许在括号内使用尾随上下文,因为许多这样的表达式更难编译.实际上,您可以结束编写两个正则表达式的交集的模式. (例如,({base}/{a}){b}匹配{base},后跟{b},它是{a}的前缀或投影.)这些仍然是正则表达式,但Thomson算法并未考虑将它们用于将正则表达式转换成有限状态机.由于该功能很少使用,因此从未尝试实现.

Flex doesn't allow trailing context inside parentheses because many such expressions are harder to compile. In effect, you can end up writing patterns which are the intersection of two regular expressions. (For example, ({base}/{a}){b} matches {base} followed by a {b} which is either a prefix or a projection of an {a}.) These are still regular expressions, but they aren't contemplated by the Thomson algorithm for turning regular expressions into finite state machines. Since the feature is rarely if ever needed, no attempt was ever made to implement it.

不幸的是,在括号内禁止尾随上下文也禁止在包含尾随上下文的模式周围使用多余的括号,并且这包括定义扩展,因为定义使用可能多余的括号进行了扩展.

Unfortunately, banning trailing context inside parentheses also bans redundant parentheses around patterns which include trailing context, and this includes definition expansions because definitions are expanded with possibly redundant parentheses.

原始的AT& T lex没有添加括号,这就是为什么强制-l的lex-compatibility允许您的flex文件进行编译的原因.但是,如上所述,它可能会导致各种各样的其他问题,所以我不建议这样做.

The original AT&T lex did not add the parentheses, which is why forcing lex-compatibility with -l allows your flex file to compile. However, it may result in all sorts of other problems, as indicated above, so I wouldn't recommend it.

此外,尾随上下文"在这里表示形式为r/s或形式为r$的完整模式.将r/s放在括号内(无论是显式还是隐式)都会产生错误消息,但是将r$放在括号内只会使$匹配$字符,而不是强制模式在行尾匹配.在这种情况下,不会发出错误或警告.

Also, "trailing context" here means either a full pattern of the form r/s or of the form r$. Putting r/s inside parentheses (whether explicitly or implicitly) produces an error message, but putting r$ inside parentheses just makes the $ match a $ character, instead of forcing the pattern to match at the end of a line. No error or warning is emitted in this case.

这将使得不可能在名称定义中使用$(或^).但是,在版本2.3.53之前的某个时间点,如果定义以^开头或以$结尾,则插入了一个抑制括号的hack.而且,由于我不完全了解的原因,如果扩展出现在尾随上下文的末尾,它还会隐藏括号.这可能是一个错误,确实有一个与此相关的错误报告.

That would make it impossible to use $ (or ^) inside a name definition. However, at some point prior to version 2.3.53, a hack was inserted which suppresses the parentheses if the definition starts with ^ or ends with $. And, for reasons I don't fully understand, it also suppresses the parentheses if the expansion occurs at the end of trailing context. This might be a bug, and indeed there is a bug report relating to it.

这篇关于在Flex中匹配尾随上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆