[antlr-interest] Non-reserved keyword as identifier question
Andy Tripp
antlr at jazillian.com
Fri Aug 31 12:21:17 PDT 2007
See the "Keywords as Variables" section on the ANTLR book.
For starters, I'd at least like to write a preprocessor that "does the
keyword as variable dance"
for every keyword in the grammar.
There's gotta be a better way, though.
Andy
troy runkel wrote:
> 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