[antlr-interest] Failure of parser on invalid input

Tomas Svarovsky svarovsky.tomas at gmail.com
Thu Oct 11 23:20:29 PDT 2012


Hi list

I am trying to create simple application and I decided I will try to
use ANTLR so I bought the book and I am working through it (thanks for
it) while trying to create something.

I am little confused with the behavior of the generated parser while I
feed it with input that does not conform to the grammar. I would
expect it to fail loud with an exception or something like that but
instead it seems to recover and sort of finish parsing.

For example. This is my grammar

grammar Gooddata;

@header {
package org.fluke.tools;
}

query : 'SELECT';

The app looks roughly like this It is not Java but Clojure hopefully
it is not influencing things.

(let [input (new ANTLRFileStream "test.txt")
        lexer (new GooddataLexer input)
        tokens (new CommonTokenStream lexer)
        parser (new GooddataParser tokens)]
    (. (.  parser query) toStringTree parser)))

If I feed it SELECT it prints "(query SELECT)" and that is expected.
If I feed it SLCT It prints "query" but it does not fail.

Is there something I need to do to be able to tell that it did not
parse correctly and be able to present present some reasonable error
message? Is there a place in the book where this is discuss ed? I am
using antlr-4.0b1

Thanks in advance

Tomas


More information about the antlr-interest mailing list