I’m working on a parser for the language D and I ran in to trouble when I tried to add the “slice” operator rule. You can find the ANTLR grammar for it here.
Basically the problem is that if the lexer encounters a string like this: “1..2” it gets completely lost, and it ends up being as a single float value and therefore the postfixExpression rule for a string like “a[10..11]” ends up being a ExpArrIndex object with a ExpLiteralReal argument. Can somebody explain what is exactly wrong with the numeric literals? (as far as I understand it fails somewhere around these tokens)
I’m working on a parser for the language D and I ran in to
Share
You can do that by emitting two tokens (an
IntandRangetoken) when you encounter a".."inside a float rule. You need to override two methods in your lexer to accomplish this.A demo with a small part of your
Deegrammar:Test the grammar with the class:
And when you run
Main, the following output is produced:EDIT
Yeah, as you indicated in the comments, a lexer rule can only emit 1 single token. But, as you yourself already tried, semantic predicates can indeed be used to force the lexer to look ahead in the char-stream to ensure there is actually a
".."after anIntegerLiteraltoken before trying to match aFloatLiteral.The following grammar would produce the same tokens as the first demo.