[antlr-interest] Problem in my Grammar

Alexander Gängel alexander at gaengel.de
Wed Dec 6 06:01:39 PST 2006


I have to distinguish "." from "..".

I have terms like Set{number,number,....}



number can be an int or an real (int.int) or an Range int/real .. int/real

the Problem is the Range because when Antlr finds one . always thinks it 
a real and than complains that another . is wrong

I thought that with option backtrack=true Antlr would try the other 
rules when an error occurred.

I hope you can help me,

Alexander

the whole grammar is:

grammar problem;
options {k=2;backtrack=true; }

start :expression;

expression
    :
    collectionLiteralExp|tupleLiteralExp
    ;

tupleLiteralExp
    :
    integerLiteral|RealLiteral
    ;   
   
collectionLiteralExp
    :
    'Set' '{'collectionLiteralParts '}'
    ;   
   
collectionLiteralParts
    :
    collectionLiteralPart (',' collectionLiteralPart)*  
    ;
   
collectionLiteralPart
    :
    expression | collectionRange
    ;   
   
collectionRange
    :
    expression '..' expression
    ;   

integerLiteral
    :   HexLiteral
    |   OctalLiteral
    |   DecimalLiteral
    ;
   
// LEXER
WS    :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=99;}
    ;

RealLiteral
    :   
    '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
    |
       ('0'..'9')+
        (
            (Exponent FloatTypeSuffix)|(FloatTypeSuffix)|(Exponent)
                |'.' ('0'..'9')* Exponent? FloatTypeSuffix?
        );

fragment
Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

fragment
FloatTypeSuffix : ('f'|'F'|'d'|'D') ;

HexLiteral : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix? ;

DecimalLiteral : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;

OctalLiteral : '0' ('0'..'7')+ IntegerTypeSuffix? ;

fragment
HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
IntegerTypeSuffix : ('l'|'L') ;







More information about the antlr-interest mailing list