dcsimg
Avoiding the "dreaded" Recognition error?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Peter_DePasquale
Posted On:   Monday, June 10, 2002 05:49 PM

Given the following fragment of my grammar file... constant returns [AbstractType type] { type = null; } : INTEGER_CONSTANT { type = new IntLiteralType(); } | FLOATING_CONSTANT { type = new DoubleLiteralType(); } | CHAR_CONSTANT { type = new CharLiteralType(); } | STRING_LITERAL { type = new StringLiteralType(); } | ( TRUE | FALSE ) { type = new BoolLiteralType(); } ; I'm wondering how to best to prevent a Recognition error from being thrown when I call this method in my semantic checker. For example, sometimes I may need to pass it what *seems* to be a variable identifier (let's call    More>>

Given the following fragment of my grammar file...

			
constant returns [AbstractType type]
{
type = null;
}
: INTEGER_CONSTANT
{ type = new IntLiteralType(); }
| FLOATING_CONSTANT
{ type = new DoubleLiteralType(); }
| CHAR_CONSTANT
{ type = new CharLiteralType(); }
| STRING_LITERAL
{ type = new StringLiteralType(); }
| ( TRUE | FALSE )
{ type = new BoolLiteralType(); }
;


I'm wondering how to best to prevent a Recognition error from being thrown when I call this method in my semantic checker.

For example, sometimes I may need to pass it what *seems* to be a variable identifier (let's call it foo). If foo is an undeclared variable and the code gets to the constant call, this method (constant) will throw a recognition error.

If I can return a null value from the constant method, rather than throwing a R.E., then I can deal with this properly in the calling method.

Thoughts, ideas and solutions welcomed!

Thanks in advance.

   <<Less

Re: Avoiding the "dreaded" Recognition error?

Posted By:   Monty_Zukowski  
Posted On:   Wednesday, June 12, 2002 06:48 AM

This is a common problem, and is why antlr has syntactic and semantic predicates. Posting your calling rule will help to figure out what you are trying to do, you may not need a predicate at all. In a nutshell syntactic predicates let you try out a grammar fragment and choose an alternative based on success or failure. This is sometimes referred to as "infinite lookahead" because you can go way beyond "k" to make the decision. Semantic predicates let you check some sort of state, for instance whether a variable is in a symbol table, and based on that test you can choose a different alternative.
About | Sitemap | Contact