Catching errors in ANTLR and finding parent
- by Andreas
I have found out that I can catch errors during parsing by overwriting displayRecognitionError, but how do I find the parent "node" of this error?
ex. if I have the grammar:
prog: stat expr;
stat: STRING;
expr: INTEGER;
And give it the input "abc def".
Then I will get an error at "def" which should be an integer. At this point I then want to get the parent which is "expr" (since it fails inside the INTEGER part) and it's parent "prog". Kind of like printing stack trace in java.
I tried to look at the node from RecognitionException parsed to displayRecognitionError, but it is null, and using CommonErrorNode the parent is null.
Should I maybe take a completely different approach?