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

David Ferrao david.ferrao at gmail.com
Fri Sep 26 04:36:05 PDT 2008


I looked at the RecognitionException state in reportError and could not find
much that would help me with expected tokens (that I could use as auto
complete suggestions). "e.token" and "e.getUnExpectedType()" point to the
error token. Only e.grammarDecisionDescription (which requires parsing)
contains expected tokens. parser.getTokenNames() returns all the token names
in the grammer, not just the expected ones.

Just to re-iterate, even RecognitionException.grammarDecisionDescription is
not suitable for my purposes in some cases (as explained in the original
thread).

I started on this approach based on the suggestions at
http://www.antlr.org/pipermail/antlr-interest/2007-April/020291.html.
Are there any better ways to implement this functionality? I admit, I didn't
quite understand the "rule actions" approach mentioned at the link above.

Thanks
David

On Thu, Sep 25, 2008 at 8:07 PM, Loring Craymer <lgcraymer at yahoo.com> wrote:

> 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/20080926/b9a969aa/attachment.html 


More information about the antlr-interest mailing list