[antlr-interest] Non-reserved keyword as identifier question
troy runkel
trunkel at gmail.com
Fri Aug 31 09:03:46 PDT 2007
I'm working on a MS-SQL grammar and I've run into a problem with
keywords/identifiers that I'm hoping somebody in the ANTLR collective
can help me resolve.
MS-SQL maintains a list of reserved keywords than can't be used as
identifiers unless they're delimited. In the following grammar 'NAME'
is a keyword, but not a reserved keyword, so it can be used as an
identifier. If I try to parse a statement like 'SELECT Name FROM
Customers' it will fail because the lexer will return 'Name' as a
distinct token, not the Identifier token.
I could create an identifier parser rule which includes the Identifier
token as well as every keyword in the grammar, but I think that would
be a long list of keywords. Is there another way to handle this
problem? Thanks.
grammar TSQL;
options
{
language = C;
backtrack = true;
}
// STATEMENTS
start :
( sql_statement (';')? )+ ;
sql_statement :
alter_role
| select_rule
;
alter_role :
'ALTER' 'ROLE' Identifier 'WITH' 'NAME' '=' Identifier
;
select_rule :
'SELECT' Identifier 'FROM' Identifier
;
// LEXER rules
Identifier :
('a'..'z'|'A'..'Z'|'_'|'$'|'#')
('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'$'|'#')* ;
More information about the antlr-interest
mailing list