[antlr-interest] BUG? lookahed and exception handler do not mix well

pganelin ganelin at mail.com
Fri Jun 17 08:16:47 PDT 2005


Summary:  

I was trying to wrap my own exception from semantic action into  
RecognitionException and I found the following problem. I do not 
understand how to resolve it properly. An example of
grammar file showing the issue is attached. For me it looks like a 
design bug for lookahead exception handling.

Description:

ANTLT generates the following exception handler for lookahead and the 
resulting code does not compile.


        catch ( IOException ex ) {
            if (inputState.guessing==0) {
                throw new RecognitionException ("IO exception");
            } else {
                throw ex ;
            }
        }

Without lookahead it would be just

        catch ( IOException ex ) {
                throw new RecognitionException ("IO exception");
        }
and it is exactly what I need.

I understand that idea of lookahead it to propagate exception and deal 
with them later, but it should propagate only ANTLR exception. While 
ANTLR is doing lookahead it does not call actions and so it cannot 
generate user defined exception. As a result catch clause is not 
required to propagate it because the exception can not be raised in the 
first place.

The ugly  solution I could see is attached as the second file. I do not 
like it because it circumvent  out of ANTLR provided mechanism. In case 
you have ten semantic actions in the rule you will need to write 10 
catch clause instead on single one.

BTW. I would propose in ANTLR 3.0 to add cause field to ANTLR exception  
constructors  to allow chained exception. (May be it is laready done. I 
only heard about 3.0 and never saw one)

PS. This is not an actual rule. I understand that I do not need 
lookahead here :-). It is used just for illustration.


================================example=================
header{
package com.equ;
import java.io.IOException;
}

class EquParser extends Parser;
options {
}


program
:
 |
    (IDENT) => IDENT { throw new IOException ("no file") ;}

;
exception
catch [ IOException ex ] { throw new RecognitionException ("IO exception");}

//----------------------------------------------------------------------------
class EquLexer extends Lexer;
//----------------------------------------------------------------------------

IDENT            :    ('a' .. 'z' )+        ;

 

===how to solve the problem =================
header{
package com.equ;
import java.io.IOException;
}

class EquParser extends Parser;
options {
}


program
:
 |
    (IDENT) => IDENT

{ try{throw new IOException ("no file") ;{ throw new 
RecognitionException ("IO exception");} }

;
 
//----------------------------------------------------------------------------
class EquLexer extends Lexer;
//----------------------------------------------------------------------------

IDENT            :    ('a' .. 'z' )+        ;

 



More information about the antlr-interest mailing list