[antlr-interest] Parser sneaks out of file with unknown tokens
shmuel siegel
antlr at shmuelhome.mine.nu
Wed Jul 12 03:03:54 PDT 2006
Ploett, Norbert wrote:
> Hi all,
>
> let me give you an example:
>
> Here is what my parser looks like, simplified of course:
> ==================================
> class SimpleParser extends Parser;
>
> options { k = 2; buildAST = true; defaultErrorHandler = false ; }
>
> edd
> options {defaultErrorHandler=true;}
> : (definition)+
> ;
>
> definition
> : VARIABLE IDENTIFIER SEMICOLON
> ;
> ==================================
>
> Now I give this input:
> ==================================
> VARIABLE A ;
> VARIABLE B ;
> C LIKE VARIABLE A ;
> ==================================
>
> The parser should (in my opinion) choke on the last line and report an
> error. But this is not the case. In the edd() method the parser is in an
> infinite loop, trying to evaluate "definition"s. When trying to evaluate
> another "definition" it notes that an IDENTIFIER token is not in the
> entry set to the "definition" rule, exits the loop and just quits
> without any error indication.
>
> What is the intended way to get an error message in this situation?
>
> Thanks for hints
>
>
> Norbert Ploett
>
>
>
Your grammar was obeyed by the input. You had two instances of
"definition". To get it to fail if there is any bad input, you need to
specify a termination condition, i.e. a token that must follow the
repeated pattern (definition). For this simple case that would be EOF.
So change your grammar to read
: (definition)+ EOF
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.9.10/384 - Release Date: 7/10/2006
More information about the antlr-interest
mailing list