[antlr-interest] Antlr 3 beta 5

Miguel Ping miguel.ping at gmail.com
Sun Jan 21 08:05:52 PST 2007


Hi all!

I am trying to use Antlr3 beta 5, and I am trying to develop a very 
primitive SQL parser. I am using antlr works to write up the grammar, 
and I can't even parse a simple query.

Here is my grammar:

///------------------------------------------------
grammar SQL;

//@parser::header {package etc;}
//@parseR::members {@SupressWarnings("all");}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/
 
 startRule
    :    sqlStatement (SEMI sqlStatement )* SEMI? EOF;

sqlStatement
    :    //sqlSchemaStatement
        sqlDataStatement ;
//    |    sqlTransactionStatement ;

sqlDataStatement
    :    SELECT ASTERISK FROM IDENT;
 
 
//@lexer::header{package etc;}

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/


IDENT        :    (LETTER | UNDERS) (LETTER | UNDERS | DIGIT)*;
fragment LETTER    :    'a'..'z' | 'A'..'Z';
fragment DIGIT    :    '0'..'9';
fragment UNDERS    :    '_';

LPAREN    :    '(';
RPAREN    :    ')';        
        
//characters        
SEMI    :    ';';
COMMA    :    ',';
DOT    :    '.';
ASTERISK:    '*';

//SQL COMMANDS
ORDER    :    'order';
GROUP    :    'group';
BY    :    'by';
HAVING    :    'having';
UNION    :    'union';
EXCEPT    :    'except';
INTERSECT:    'intersect';    
ALL    :    'all';
FOR    :    'for';
IN    :    'in';

AND    :    'and';
NOT    :    'not';
NULL    :    'null';

SELECT    :    'select';
FROM    :    'from';
WHERE    :    'where';
AS    :    'as';
JOIN    :    'join';

PIVOT    :    'pivot';

COUNT    :    'count';
AVG    :    'avg';
MIN    :    'min';
MAX    :    'max';
SUM    :    'sum';
STDEV    :    'stdev';
STDEVP    :    'stdevp';
VAR    :    'var';
VARP    :    'varp';

DISTINCT:    'distinct';
NATURAL    :    'natural';

LEFT    :    'left';
RIGHT    :    'right';
INNER    :    'inner';
OUTER    :    'outer';
FULL    :    'full';
ON    :    'on';
USING    :    'using';
LIMIT    :    'limit';
OFFSET    :    'offset';
DEFAULT    :    'default';

//common stuff
WS    :   (   ' '
        |   '\t'
        |   '\r' '\n'
        |   '\n'
        |   '\r'
        ) {$channel=HIDDEN;};
 //ignore this token
        
UINT    :    '0'..'9';//correct this later    

//comentarios
ML_COMMENT
    :   '/*' (options {greedy=false;} : .)* {$channel=HIDDEN;}'*/';    

///------------------------------------------------

If i try and interpret a simple

 select * from sailors

query, antlr works gives me a 'MismatchedTokenException (8!=5)', altough 
this grammar is as simple as it gets! token 8 and 5 are the 'select' and 
'ident' tokens, what am I doing wrong?

I started doing a big piece of the grammar at one shot, and antlr3 
parser generation process gave me this error:

error(10):  internal error: 
org.antlr.misc.OrderedHashSet.size(OrderedHashSet.java:87): 
OrderedHashSet: elements and set size differs; 2097151!=1020508

So I changed my approach and tried the supplied grammar, one step at a 
time. I have read and re-read the documentation, and I cannot find my 
problem.

Thanks in advance, Miguel Ping


More information about the antlr-interest mailing list