Flex ‘r/s’ in ANTLv4

左心房为你撑大大i 提交于 2019-12-13 06:57:06

问题


Flex: ‘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.)

How do this in ANTLRv4 ?


回答1:


There are two primary ways I've accomplished this.

The first involves a simple semantic predicate.

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

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



来源:https://stackoverflow.com/questions/25535089/flex-r-s-in-antlv4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!