[antlr-interest] Problem with error handling.

georgehernando georgehernando at hotmail.com
Tue Sep 21 08:58:40 PDT 2004


I'm trying to write a lexer/parser that will read in a command line 
with switch options.

Something like:
cmdx -sw1 cbn kjy -sw2 mm -sw3 

In the lexer, I identify the switches (starting with hyphens) as 
tokens:
  SW_1, SW_2, SW_3
and the commands are identified as tokens too:
  CMD1, CMD2, ...
and I identify the non-switches as a parameter PARAM.

In the parser, I do something like:

protected sw1_switch   : SW_1 PARAM PARAM;
protected sw2_switch   : SW_2 PARAM; 

mycmd : CMD1 sw1_switch sw2_switch EOL;

Basically this is working, but I have problems with
validation.  Some entries can get confused.
For instance, if I make a mistake and type:

cmd1 -sw1 aaa -sw2

Switch sw1 is only supposed to take two parameters.
An error is thrown (good), but the code generated by
ANTLR to recover looks like:

protected final void sw1_switch() throws RecognitionException, 
TokenStreamException {		
    try {     
        match(SW_U);
	match(PARAM);
	match(PARAM);
	}
    catch (RecognitionException ex) {
        reportError(ex);
	consume();
	consumeUntil(_tokenSet_3);
    }
}

The problem is that _tokenSet_3 refers to the -sw2 switch.
The first consume() clears the -sw2 token and then the consumeUntil()
is looking for a -sw2 token.
The consumeUntil() reads past the EOL token and into the next line of 
input.
I want the command to exit with an error and clear all tokens through 
the EOL token.
I don't want to modify code autogenerated by ANTLR.

Is my parser definition bad?  
Is there something I can do to overcome this problem?




 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list