[antlr-interest] Insert tokens and Recovering from an error.

Joan Pujol joanpujol at gmail.com
Sat Mar 12 10:41:47 PST 2005


For my recovery strategy I need to insert some missen tokens to the
parser and then relaunch some rules. And I don't know how to do it.

Here is an example:


rulea: b:ruleb c:rulec d:ruled; 

exception[c] catch NoviableAlt[ex]
{
 My recovery strategy for rulec is:
   consumeUntil TOKEN1,TOKEN2,TOKEN3,TOKEN4

   if(LA(1)==TOKEN1 or LA(1)==TOKEN2)
	relaunch rulec
   if(LA(1)==TOKEN3)
	insert TOKEN1 to the parser and relaunch rulec
   if(LA(1)==TOKEN2)
	insert TOKEN4 to the parser and relaunch rulec
}

where rulec: TOKEN1 rulec1 //rulec1 can start with TOKEN3
	     |TOKEN2 rulec2; //rulec2 can start with TOKEN4

I use this recovery strategy because I create an AST. And the
objective is to always create a correct AST.


I know how to relaunch rulec anc create the AST. The code I use is:

AST previousc=#c;
rulec();
#c=returnAST;
ChangeNode(currentAST,previousc,#c); //Where ChangeNode is an utility
method I made that when found the previousc change it for the #c

Is this the best way to do it?


But when I need to insert a token before launch the rule I don't know
how to do it.
How can I do it? Or any workarround?

A lot of thanks in advance,

-- 
Joan Jesús Pujol Espinar


More information about the antlr-interest mailing list