[antlr-interest] Keywords as identifiers in ANTLR 3.0

Ayende Rahien ayende at ayende.com
Tue Aug 28 14:51:49 PDT 2007


I know that the question was raised before, and I checked to wiki for the
explanation on it, but I can't seem to follow the solutions there and can't
get it to work.
Here is my sample grammar:

grammar Test;
options{
    language=CSharp;
    output=AST;
    backtrack=true;
}

program: stat+;

stat:
    ( identifierClause | orderByClause) ';';

identifierClause
    : IDENTIFIER AS? IDENTIFIER ;

orderByClause
    :
    ORDER_BY IDENTIFIER
    ;

// Multi word keywords
ORDER_BY    :    ORDER WS+ BY;
GROUP_BY    :    GROUP WS+ BY;

IDENTIFIER
    : ID_LETTER+
    ;

fragment
AS :    'as';
fragment
ORDER    : 'order';
fragment
GROUP    : 'group';
fragment
BY        : 'by';

// White space
WS : ( ' ' | '\t' | '\n'  | '\r' )+
     { $channel=HIDDEN; }
   ;

fragment
ID_LETTER     :
        'a'..'z' |   'A'..'Z' |    '_';


And my test inputs are:

   - "order by order"
   - "order order"
   - "order as order"
   - "order as o"
   - "order by o"

The test program is:

static void Main(string[] args)
{
    try
    {
        ANTLRStringStream stream = new ANTLRStringStream("order as o");
        TestLexer l = new TestLexer(stream);
        CommonTokenStream cts = new CommonTokenStream(l);
        TestParser p = new TestParser(cts);
        TestParser.program_return program = p.program();
        Console.WriteLine(program.Tree.ToString());
    }
    catch (Exception e)
    {
        System.Console.WriteLine(e);
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070828/8e81e7c5/attachment.html 


More information about the antlr-interest mailing list