[antlr-interest] How does one suppress 'no viable alternative at character' warning messages

Tim Halloran hallorant at gmail.com
Wed Sep 17 12:03:52 PDT 2008


Well, I'm not sure but I don't want extra output.

Riffing off Jim's idea I tried:

ANY
    : . { throw new RecognitionException(); }
    ;

but this doesn't work as the generated code (I'm using :

// $ANTLR 3.1

...

    // $ANTLR start "ANY"
    public final void mANY() throws RecognitionException {
        try {
            int _type = ANY;
            int _channel = DEFAULT_TOKEN_CHANNEL;
            //
C:\\Users\\Tim\\Source\\Work34\\ad-hoc-query-common\\src\\com\\surelogic\\common\\adhoc\\model\\parser\\ColumnAnnotation.g:94:2:
( . )
            //
C:\\Users\\Tim\\Source\\Work34\\ad-hoc-query-common\\src\\com\\surelogic\\common\\adhoc\\model\\parser\\ColumnAnnotation.g:94:4:
.
            {
            matchAny();
             throw new RecognitionException();

            }

            state.type = _type;
            state.channel = _channel;
        }
        finally {
        }
    }

Has an error -- the Java compiler is unhappy because the state.* assignments
are unreachable code (due to the throw).  Now I can work around this by
changing the grammar to:

ANY
    : . { if (true) throw new RecognitionException(); }
    ;

There is another problem with the use of "."  If I change to


ANY
    : c=. { if (true) {
              RecognitionException e =new RecognitionException();
              e.line = ?
              ...
              throw e;
            }
          }
    ;

"c" isn't a token it is an int (as described on page 126 of the book).  This
is bad as there is no way to "fill out" the RecognitionException information
(line, index, pos, etc.)  Is there some way around this problem?  I'm
guessing this is for performance, but lacking a token makes reporting
impossible as far as I can see.

Regards,
Tim

On Wed, Sep 17, 2008 at 2:39 PM, Jim Idle <jimi at temporal-wave.com> wrote:

>  On Wed, 2008-09-17 at 14:14 -0400, Tim Halloran wrote:
>
> My lexer is sending warnings to stderr of the form and I'd like to suppress
> these.
>
> line 1:7 no viable alternative at character 'H'
> line 1:8 no viable alternative at character 'I'
> line 1:9 no viable alternative at character 'D'
> line 1:10 no viable alternative at character 'E'
>
>
> Rather than do that, create a last rule in the lexer;
>
> ANY : . { System.out.println("Don't support this char" + $text); skip(); }
>
> Or perhaps you want a filtering lexer?
>
> Jim
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080917/37c03bc7/attachment.html 


More information about the antlr-interest mailing list