[antlr-interest] Problem with Backtrack

Alexander Gängel alexander at gaengel.de
Wed Dec 20 19:03:11 PST 2006


My Grammar uses the backtrack option.

but now I have a Problem, after I check a predicate it wont match my Input.

if I remove the Backtrack Option It works. (can't I use Backtrack and
Predicates tried it before an I worked I think)

I use AntlrWorks 1.0b8

could somebody tell me, if there is something wrong with my grammar?

Alexander Gängel

------------------------------------------------------------------------

the start rule is: oclExpression

the not working Input: validFrom.mod

hier is the Grammar:

grammar ocl;

options  {
    backtrack=true;
    k=2;
    output=AST;
    ASTLabelType=CommonTree;
    }
 
tokens {
    Var;
    Type;
    TupleType;
    Collection;
    IfExpression;
    VariableExp;
    CollectionRange;
}

@members {
    boolean op = false;
}

typeCS    :
    collectionTypeCS -> ^(Type collectionTypeCS)
    | Identifier -> ^(Type Identifier)
    |tupleTypes -> ^(Type  tupleTypes)
    ;
   
   
collectionTypeCS
    :
    collectionTypeIdentifier '(' typeCS ')' -> ^(Collection
collectionTypeIdentifier typeCS)
    ;
   
tupleTypes
    :
    'TupleType' '(' variableDeclaration ')' -> ^(TupleType
variableDeclaration)
    ;

oclExpression            
    :
    (
     oclExpression_direct
    (
        ( ocl_or_operation)* (standard_operation  oclExpression  )*)
    )
    |'('oclExpression')'
    ;
   
ocl_or_operation
    :
    ('.'{op=true;}(oclExpression|ocl_operations))
    ;

ocl_operations
    :
    {op}?
    ('mod'^|'div'^){op=false;}
    ;

oclExpression_direct
    :   
    (    (literalExp-> literalExp)
        |
        (variableExp-> variableExp)
        |
        (ifExpression-> ifExpression)
    )
    ;   

standard_operation
    :   
    OP_Plus   
    ;   
   

literalExp
    :
    collectionLiteralExp
    |tupleLiteralExp
    ;
   
tupleLiteralExp
    :   
    integerLiteral
    |RealLiteral
    |StringLiteral
    |booleanLiteral
    ;   
   
collectionLiteralExp
    :
    collectionTypeIdentifier '{'collectionLiteralParts '}' ->
^(collectionTypeIdentifier collectionLiteralParts)
    ;   
   
collectionLiteralParts
    :
    collectionLiteralPart (',' collectionLiteralPart)*   ->
^(collectionLiteralPart)+
    ;
   
collectionLiteralPart
    :
    oclExpression -> ^(oclExpression)
    |collectionRange -> ^(collectionRange)
    ;   
   
collectionRange
    :
    oclExpression '..' oclExpression -> ^(CollectionRange
oclExpression+)   
    ;   


variableExp
    :
    Identifier ('('oclExpression?')')? -> ^(Identifier oclExpression?)   
    ;
   
   
ifExpression
    :
    ifpart
    thenpart
    elsepart
    'endif'
    ;
   
   
ifpart    :
    'if' oclExpression -> ^('if' ^(oclExpression))
    ;
   
thenpart    :
    'then' oclExpression -> ^('then' ^(oclExpression))
    ;
   
   
elsepart    :
    'else' oclExpression -> ^('else' ^(oclExpression))
    ;           
       
variableDeclaration
    :
    Identifier (Colon typeCS)? ( '=' oclExpression)?
    ->  ^(Var Identifier typeCS? oclExpression?)
    ;
   
collectionTypeIdentifier
    :
    'Set'
    |'Bag'
    |'Sequence'
    |'OrderedSet'
    |'Collection'
    ;
       
// LEXER
Identifier
    :   ('a'..'z'|'A'..'Z'|'_') (Letter)*
        ;

fragment
Letter   
        :'a'..'z'|'A'..'Z'|'0'..'9'|'_'|'$';


WS    :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=99;}
    ;

COMMENT
    :   '/*' ( options {greedy=false;} : . )* '*/' {$channel=99;}
    ;

LINE_COMMENT
       : ('//'|'--') ~('\n'|'\r')* '\r'? '\n' {$channel=99;}
    ;
   
Colon   :                
    ':'
    ;
   
StringLiteral    :
     '\''  ~('\'')* '\''   {setText(getText().substring(1,
getText().length()-1));}
     ;
   
booleanLiteral    :   
    'true'| 'false'
    ;           
   
RealLiteral
    :   
        //'.' ('0'..'9')+ Exponent? FloatTypeSuffix?
      ('0'..'9')+
        (
            (Exponent FloatTypeSuffix?)|(FloatTypeSuffix)
                |('.' ('0'..'9')+) Exponent? FloatTypeSuffix?
        );

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

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

integerLiteral
    :   HexLiteral
    |   OctalLiteral
    |   DecimalLiteral
    ;

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') ;

OP_Plus    :    '+';

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20061221/370bb197/attachment.html 


More information about the antlr-interest mailing list