问题
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