[antlr-interest] "Coarse grained" errors in antlr

Loring Craymer lgcraymer at yahoo.com
Thu Sep 25 07:37:21 PDT 2008


This is doable, but it may be more painful than you would like--lots of grammar surgery.  You should not be parsing error messages; you should override reportError() in your parser (much easier and more informative than parsing error messages) and get the token information from the input stream.  Without some form of resynchronization during error recovery, you are also dependent on having a correct grammar except at the point where the user is typing; that seems undesirable, but maybe you have some way around that.

--Loring



----- Original Message ----
From: David Ferrao <david.ferrao at gmail.com>
To: antlr-interest at antlr.org
Sent: Thursday, September 25, 2008 3:03:33 AM
Subject: [antlr-interest] "Coarse grained" errors in antlr


Hi and apologies for the newbie question.

I am trying to build an autocomplete mechanism using antlr error messages, but am facing a problem where the parser seems to bail out one rule earlier than I  would like.
To illustrate, consider the following grammar snippet.

--------Antlr 3 snippet-------
create  : 'CREATE'  stream;
stream    : 'STREAM' qname 'TYPE' stream_types;
stream_types : map_schema| java_bean|xml_schema;
map_schema    :'MAP_SCHEMA' '{' (value_list)? '}';
java_bean    :'JAVA_BEAN' '(' class_name ')' '{' '}';
xml_schema    :'XML_SCHEMA' '(' string_literal ')' '{' '}';

....contd...

---------end Grammar------

Given the string "CREATE STREAM system:TEST TYPE MAP_SCHEMA" as input, the parser generates the following NoViableAltException

"36:1: stream_types : ( map_schema | java_bean | xml_schema );"

This is a completely logical error considering that the parser cannot match either "map_schema" or  "java_bean" or "xml_schema". 
However, it is not very helpful for my auto complete scenario which would be better served by an error message stating that a '{' was expected.
I was wondering if there is any option /technique to coax the parser to start executing and consequently throw a NoViableAltException in the "map_schema" rule, considering that the input DOES contain the 'MAP_SCHEMA' token.

Would deeply appreciate any inputs, I have skimmed through the 'Definitive ANTLR reference', but still couldnt come up with a solution :( 

Thanks
David



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080925/96fbdf48/attachment.html 


More information about the antlr-interest mailing list