[antlr-interest] ANTLR v3: enum as identifier and as a keyword
Terence Parr
parrt at cs.usfca.edu
Thu Jun 30 11:15:59 PDT 2005
On Jun 30, 2005, at 10:15 AM, David Ewing wrote:
> On Jun 29, 2005, at 1:10 PM, Jörg Rech wrote:
>> Hi,
>> maybe I got something wrong here but isn't the lexer called
>> before the recognizer for the methods? So if I enter a method
>> changes to enableEnum have no effect on the lexer?
>>
>> Or is the lexer just registered and called by the recognizer/
>> parser on demand?
>>
>
> It's called on demand. Another possibility would be to change
> 'IDENT' to '(IDENT | "enum")' in a few key places elsewhere in the
> grammar (e.g. variable declarators, expressions). That might be a
> tad more compatible, but it also might add ambiguities to the
> grammar. In any case, I haven't tried either, but I'd be interested
> to hear if they work.
Now that hoisted semantic predicates are back in ANTLR, you can do
this very easily. I just built a new example (and pushed it to the
site--examples-v3.tar.gz has been updated).
/** Demonstrates how semantic predicates get hoisted out of the rule in
* which they are found and used in other decisions. This grammar
illustrates
* how predicates can be used to distinguish between enum as a
keyword and
* an ID *dynamically*. :)
* Run "java org.antlr.Tool -dfa t.g" to generate DOT (graphviz)
files. See
* the T_dec-1.dot file to see the predicates in action.
*/
grammar T;
{
/** With this true, enum is seen as a keyword. False, it's an
identifier */
boolean enableEnum = false;
}
stat: identifier {System.out.println("enum is an ID");}
| enumAsKeyword {System.out.println("enum is a keyword");}
;
identifier
: ID
| enumAsID
;
enumAsKeyword : {enableEnum}? "enum" ;
enumAsID : {!enableEnum}? "enum" ;
...
The rule stat decision sees ID or ID syntactically, but with
predicates, the decision looks like this:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pastedGraphic.pdf
Type: application/pdf
Size: 14150 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20050630/c86da645/pastedGraphic-0001.pdf
-------------- next part --------------
The =>alt notation indicates which alt will be predicted. The
generated decision looks like:
int LA1_0 = input.LA(1);
if ( LA1_0==ID ) {
alt1=1;
}
else if ( LA1_0==7 ) {
int LA1_2 = input.LA(2);
if ( evalPredicate(enableEnum,"enableEnum") ) {
alt1=2;
}
else if ( evalPredicate(!enableEnum,"!enableEnum") ) {
alt1=1;
}
else {
...error...
}
}
else
...error...
Pretty slick, eh? Hooray!
Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
More information about the antlr-interest
mailing list