[antlr-interest] Error reporting
tbrandonau at gmail.com
Tue Aug 14 02:44:33 PDT 2007
On 8/14/07, Matthias Schnelte <mschnelte at gmx.de> wrote:
> No ideas? anybody?
> I have tried the same grammar with javacc and it reports all the three
> possibilities as error message.
> Any idea how to implement this in ANTLR?
> ----- Ursprüngliche Nachricht -----
> Von: mschnelte at gmx.de
> Gesendet: 13.08.07 13:38 Uhr
> An: antlr-interest at antlr.org
> Betreff: [antlr-interest] Error reporting
> Hi everyone,
> I have the following grammar:
> sp2: WENN condition DANN;
> condition : simpleCondition (('und' ^|'oder' ^) simpleCondition)*;
> simpleCondition : ie iv 'ist';
> ie : QUOTE! .+ QUOTE!;
> iv : QUOTE! .+ QUOTE!;
> If the parser reads the following string: "wenn "bla" "blub" ist" it reports
> as error:
> "mismatched input '<EOF>' expecting DANN"
> Ok, "DANN" would produce a correct statement but "und" or "oder" can follow
> a simpleCondition as well.
> How can I get the parser to print out all possible inputs on a decision
> I want to implement a code completion and such thing would of course be very
I think the issue might be that ANTLR has already decided that
condition has ended when it reports the error. Not sure, you should
confirm by checking the generated source. But I think that at the end
of the closure in condition ANTLR is checking the LA to see if it is
'und' or 'oder' to decide whether to run round the loop again. Upon
seeing EOF it exits closure and then the condition rule. Then in sp2
it checks the lookahead, finds it isn't 'DANN' so gives an error. At
this point another simpleCondition isn't a valid alternative as it's
already decided condition has ended, so it doesn't list that as an
option. It looks like JavaCC either does it's lookahead checks
differently or keeps the follow sets for exited closures\rules around.
Not sure how difficult it would be to have ANTLR provide this
information. ANTLR has follow sets for each available token invocation
(or maybe just a subset, not sure there) and uses them to dynamically
maintain a current follow set. You could maintain your own follow set
that gave the information you wanted. Not sure how complicated the
algorithm would be. You could do this by creating a custom target to
avoid repetitive actions. Or you might be able to create a modified
target that pre-calculated this information (or generated methods to
do so at runtime).
Comparing JavaCCs implementation with ANTLR's should show how to get
the same level of information it provides.
More information about the antlr-interest