在ANTLv4中灵活显示"r/s" [英] Flex ‘r/s’ in ANTLv4

查看:74
本文介绍了在ANTLv4中灵活显示"r/s"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Flex: ‘r/s’

Flex: ‘r/s’

一个"r",但前提是其后跟一个"s".确定此规则是否为最长匹配项时,将包含用"s"匹配的文本,但在执行操作之前,该文本将返回到输入.因此,该操作只会看到与"r"匹配的文本.这种模式称为尾随上下文. (有一些'r/s'组合,flex不能正确匹配.有关危险的尾随上下文,请参见限制.)

an ‘r’ but only if it is followed by an ‘s’. The text matched by ‘s’ is included when determining whether this rule is the longest match, but is then returned to the input before the action is executed. So the action only sees the text matched by ‘r’. This type of pattern is called trailing context. (There are some combinations of ‘r/s’ that flex cannot match correctly. See Limitations, regarding dangerous trailing context.)

在ANTLRv4中如何实现?

How do this in ANTLRv4 ?

推荐答案

我完成此操作的主要方法有两种.

There are two primary ways I've accomplished this.

第一个涉及一个简单的语义谓词.

The first involves a simple semantic predicate.

'r' {_input.LA(1) == 's'}?

第二个要复杂得多,它涉及在匹配之后重置输入流的位置,在该匹配中实际将尾随上下文编码为规则的一部分.实际上,此行为已在ANTLR 4测试套件的一部分中显示.

The second is vastly more complicated, and involves resetting the input stream position following a match where the trailing context is actually encoded as part of the rule. This behavior is actually shown in part of the ANTLR 4 test suite.

https://github.com/antlr/antlr4/blob/master/tool/test/org/antlr/v4/test/PositionAdjustingLexer.g4

这篇关于在ANTLv4中灵活显示"r/s"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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