[antlr-interest] Grammars that uses Keyword as Identifiers
Alexandre Porcelli
porcelli at uol.com.br
Fri Jun 8 10:09:49 PDT 2007
Hi All...
I'm constructing a T-SQL grammar and this grammar accepts keywords
as identifiers. So I started to implement as Terr suggested on his
book (page 287 - Keyword as Variables):
address_key
: {input.LT(1).getText().equalsIgnoreCase("address")}? IDENTIFIER
;
But... the generated code for the parser has a problem in
specialStateTransition method.. it has a semantic predicate use.. :
if ( ((input.LT(1).getText().equalsIgnoreCase("constraint")||input.LT(1).getText().equalsIgnoreCase("identity")||input.LT(1).getText().equalsIgnoreCase("collate")))
) {s = 3;}
else if ( ((input.LT(1).getText().equalsIgnoreCase("create")||input.LT(1).getText().equalsIgnoreCase("primary")||input.LT(1).getText().equalsIgnoreCase("alter")||input.LT(1).getText().equalsIgnoreCase("constraint")))
) {s = 2;}
I could solve this problem... just externalizing with a function:
@parser::members {
private boolean validateIdentifierKey(String text){
return input.LT(1).getText().equalsIgnoreCase(text);
}
}
address_key
: {validateIdentifierKey("address")}? IDENTIFIER
;
Now it woks... but now the parser behavior looses most of its LL(*)
advantages (as Terr advices in the book when he talks about the DFA
evaluation)...
I'd like to ask/suggest/propose if it would be possible to create a
way to identify a special rule with some specific option should be
treated as an identifier but codes related to LL(*) still, something
like:
address_key
options {keywordText = "address";}
: IDENTIFIER
;
or something like fragment syntax:
keyword
address_key
: 'address'
;
There are several languages that needs this kind of behavior (I can
remember for now: COBOL, Natual/Adabas, JCL, T-SQL, PL-SQL).
Best Regards,
Alexandre Porcelli
More information about the antlr-interest
mailing list