[antlr-interest] Failure of parser on invalid input

Jim Idle jimi at temporal-wave.com
Thu Oct 11 23:59:04 PDT 2012

Please make use of http://antlr.markmail.org as you will find it is a good
search engine for the email list.

Unless you tell ANTLR to keep parsing while there is any input left, it
will just stop when there is input that will not match any parser rule. So
you want;

query: 'SELECT' EOF ;

Also, I would not try to write a SQL grammar as your first project as it
is far too complicated to learn while trying to do that. Start with
expression trees and simple languages until you have the basics down.


-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Tomas Svarovsky
Sent: Friday, October 12, 2012 2:20 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Failure of parser on invalid input

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


List: http://www.antlr.org/mailman/listinfo/antlr-interest

More information about the antlr-interest mailing list