[antlr-interest] Tokens as keywords

Jim Idle jimi at temporal-wave.com
Sat May 3 17:59:32 PDT 2008


statement :  FIND tableName whereClause? orderByClause?;

tableName
   : Identifier
   | (keywords)=>keywords
   ;

keywords
   : FIND
   | WHERE
   | etc
   ;

If writing a tree you can also do:

  | (keywords)=>k=keywords
              ->Identifier[$k]

Then look for real ambiguities in the grammar where you might need more elaborate predicates. For instance if table is optional and followed by some clause, then you won't know if the keyword is the table name or the next clause without some machinations. If an Identifier can always be a keyword, then use the rulename id where I called it tableName.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Steve O'Hara
> Sent: Saturday, May 03, 2008 5:47 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Tokens as keywords
> 
> I've written a grammar to unravel BASIS Find commands - these look very
> similar to SQL Selects e.g.
> 
>     Find table where field='gdfdfg' order by field/ascend
> 
> All is well except when I try and parse a command that contains a token
> as the table name.  For example;
> 
>     Find ascend where field='gdfdfg' order by field/ascend
> 
> This is a valid command - tables can be called practically anything.
> Below is a snippet from the grammar.
> Where do I look to go about fixing this?
> 
> statement :  FIND Identifier whereClause? orderByClause?;
> 
> ASC : ('asc' | 'ascend') ;
> FIND : 'find' ;
> Identifier : ('a'..'z' | 'A'..'Z' | '_' | '\$') ('.' |  'a'..'z' |
> 'A'..'Z' | '_' | '0'..'9' | '$')* ;
> 
> 
> Thanks,
> Steve






More information about the antlr-interest mailing list