[antlr-interest] better error messages in tree parsers
Oliver Zeigermann
oliver.zeigermann at gmail.com
Sat Jan 30 03:48:21 PST 2010
As input.LT seems to return null values in case we are at the very
start/end of the node stream, I added this check which does the job
for me
input.LT(-3) == null ? "" : ((Tree)input.LT(-3)).getText()+" "+
input.LT(-2) == null ? "" : ((Tree)input.LT(-2)).getText()+" "+
input.LT(-1) == null ? "" : ((Tree)input.LT(-1)).getText()+" >>>"+
input.LT(1) == null ? "" : ((Tree)input.LT(1)).getText()+"<<< "+
input.LT(2) == null ? "" : ((Tree)input.LT(2)).getText()+" "+
input.LT(3) == null ? "" : ((Tree)input.LT(3)).getText();
2010/1/26 Terence Parr <parrt at cs.usfca.edu>:
> Hi, a reminder that debugging tree grammars can be a bitch. I like to override standard messaging to spew lots of stuff. E.g., i like this kind of thing:
>
> ASTVerifier.g: node from after line 150:17 [grammarSpec, rules, rule, altListAsBlock, altList, alternative, elements, element, ebnf, block, altList, alternative] no viable alt; token=[@-1,0:0='ALT',<84>,0:-1] (decision=24 state 3) decision=<<>>
> context=...DOWN BLOCK DOWN >>>ALT<<< DOWN DOC_COMMENT...
>
> Here's my code:
>
> public String getErrorMessage(RecognitionException e,
> String[] tokenNames)
> {
> List stack = getRuleInvocationStack(e, this.getClass().getName());
> String msg = null;
> String inputContext =
> ((Tree)input.LT(-3)).getText()+" "+
> ((Tree)input.LT(-2)).getText()+" "+
> ((Tree)input.LT(-1)).getText()+" >>>"+
> ((Tree)input.LT(1)).getText()+"<<< "+
> ((Tree)input.LT(2)).getText()+" "+
> ((Tree)input.LT(3)).getText();
> if ( e instanceof NoViableAltException ) {
> NoViableAltException nvae = (NoViableAltException)e;
> msg = " no viable alt; token="+e.token+
> " (decision="+nvae.decisionNumber+
> " state "+nvae.stateNumber+")"+
> " decision=<<"+nvae.grammarDecisionDescription+">>";
> }
> else {
> msg = super.getErrorMessage(e, tokenNames);
> }
> return stack+" "+msg+" context=..."+inputContext+"...";
> }
> public String getTokenErrorDisplay(Token t) {
> return t.toString();
> }
>
> Ter
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
More information about the antlr-interest
mailing list