[antlr-interest] Non-reserved keyword as identifier question

Peggy Fieland madcapmaggie at yahoo.com
Fri Aug 31 09:53:07 PDT 2007


I wrote a program to solve this. It works off a list of "real literals" and goes through the *.hpp file grabbing literals. When it finds one that's a "real literal," it outputs it to a file of keywords. When it finds one that isn't, it outputs it to a file of "non-keywords." I then scarf them up and put them into my grammar file. Identifiers can be either an identifier, a delimited identifier, or a non-keyword.

I'm using antlr-2.7.

troy runkel <trunkel at gmail.com> 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'|'_'|'$'|'#')* ;



Margaret Fieland
    www.margaretfieland.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070831/cc262ee6/attachment.html 


More information about the antlr-interest mailing list