ANTLv4 中的 Flex 'r/s' [英] Flex ‘r/s’ in ANTLv4

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

问题描述

弹性:‘r/s’

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

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 中的 Flex 'r/s'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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