How can I get the previous token in Antlr 3 parser?. Actually i can get it easily by using "input.LT(-1)".But I don't want to use the lookahead.So is there any alternative method to get the previous token in parser?
Asked
Active
Viewed 223 times
0
-
What is the reason for avoiding `LT(-1)`? As long as you want the previous token before the one currently matched, this is the standard way and is fully supported by ANTLR. Only case I know of where you cannot use it is if you want to know the previous token before some given arbitrary token (not the one actually matched). – Jiri Tousek May 23 '17 at 08:09
1 Answers
1
An alternative way would be to override emit(Token t)
and nextToken()
in your lexer and keep track of the last emitted token. For an example of how this could work, have a look at the PyEsque
grammar from this Q&A: ANTLR What is simpliest way to realize python like indent-depending grammar?
But I would just use input.LT(-1)
.

Bart Kiers
- 166,582
- 36
- 299
- 288