[antlr-interest] syntactic predicates and exceptions

Terence Parr parrt at cs.usfca.edu
Tue Oct 18 11:17:32 PDT 2005


On Oct 18, 2005, at 9:47 AM, Prashant Deva wrote:

> cant u have something like this-
> match(int tokenType, int guessing)
> {
>     //do matching stuff
>     //on error, do this-
>     if (guessing>0)
>        return false;
>
>    else throw Excpetion;
> }


Yep, something like that, but it would require those changes over a  
lot of the code...perhaps it's worse in my mid than reality.  All  
generated code though would see

match vs if(match)

and then

int alt = predict(...);
switch (alt) {
   case 1 : ...
   ...
   default : // error
     if ( guessing>0 ) cleanup; return false;
     else throw NoViableAltException(...);
}

That is not so bad I guess...this is mroe complicated when I have to  
clean up code.  Imagine that the switch is nested 10 levels deep.  I  
have to figure out how to do all the clean up for all 10 code levels  
in that error clause. :(  I suppose try/finally is the answer.  What  
about C++?  Gotos?

Also using a return value is a drag as I have to deal with user  
return values.

perhaps a separate stack for error returns?  Can we get away with a  
single global instance var for errors like the old errno for old unix?

void match(int type) {
   if ( input.LA(1)!=type ) {
     if ( guessing>0 ) error=true; return;
     throw exception
   }
   ...
}

then in the call, you'd do

match(ID); // normal
if ( error ) return;

Maybe an error for each level:

int error[MAX_GUESSING_LEVEL];

then

if (error[guessing]) return;

???

Hmm...seeming more and more doable...

Ter


More information about the antlr-interest mailing list