[antlr-interest] Semantic Predicates and SemanticExceptions

johnj1984 johnj1984 at yahoo.com
Mon Aug 2 09:38:03 PDT 2004


Hello Folks, I have a question about the useage of Semantic 
Predicates and SemanticExceptions.

I have the following (simplified) source text:

 TClass1.someProcedureDeclaration {...};
 TClass1.someProcedureDeclaration {...};
 TClass2.someProcedureDeclaration {...};
 TClass2.someProcedureDeclaration {...};
 
 This gets parsed by a rule
   ruleClassDeclaration
   	: (ruleProcedureDeclarationPart) *
   	;
   	
   ruleProcedureDeclarationPart
   	:  { stillInClass(LT(3).getText()) }? 
   		SEMI! ruleProcedureOrFunctionDeclaration
		   
		exception 
			catch [RecognitionException ex] {
		   		reportError("non-class 
procedure/function exception:"+ex.toString());
		   	}
   	;
   	
   Where stillInClass(String className) is a custom function that 
checks if the procedure's class (TClass1, TClass2, etc) has changed.  
This function is used in the semantic predicate 
{ stillInClass(LT(3).getText()) }? 
   
   My problem is how do I correctly perform the semantic check & 
catch its exception: The semantic check throws a
   semantic exception when the class name switches from TClass1 to 
TClass2 -- But how do I handle the exception? If I catch the exception
   where it is thrown, in ruleProcedureDeclarationPart, then I enter 
an infinite loop when the semantic check fails, where the parser 
attempts to reevaluate the same failed text ad infinitum.
   On the other hand, if I catch the exception in the while loop as 
follows, I also get the same infinite loop:
   
   ruleClassDeclaration
   	: (ruleProcedureDeclarationPart
   		exception 
			catch [RecognitionException ex] {
		   		reportError("non-class 
procedure/function exception:"+ex.toString());
		   	}
   	   ) *
   	;
   	
   Finally, if I catch the exception outside the while loop in 
ruleClassDeclaration, then all the previously matched 
ruleProcedureDeclarationParts are thrown away, since essentially the 
whole class declaration throws an exception.
   
   So... How can I check the semantic validity of a rule and use that 
semantic check to break out of a loop, while still keepting the prior 
matches?
   Essentially I need to tell the parser that when my semantic check 
fails, it should backtrack from the failed evaluation only, 
successfully exit the rules its matched so far, and reparse the 
failed text from somewhere higher in its rule hierarchy.
   
   Does this make sense? Should I not be using semantic predicates 
but some kind of syntactic predicate instead? Any pointers welcome!
   
   Thanks in advance!
   
   
   
   
  



 
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