antlr - Using floats in range and grammar mistakes? -
i trying convert lalr grammar ll using antlr , running few problems. far, think converting expressions top-down approach straight forwards me. problem when include range (1..10) , (1.0..10.0) floats.
i have tried utilize reply found here , somehow not running correctly code, allow lone solving range of float, i.e. (float..float). float literal , range parameter in antlr
attached sample of grammar focuses on issue.
grammar test; options { language = java; output = ast; } parse: 'in' rangeexpression ';' ; rangeexpression : look ('..' expression)? ; look : addingexpression (('=='|'!='|'<='|'<'|'>='|'>') addingexpression)* ; addingexpression : multiplyingexpression (('+'|'-') multiplyingexpression)* ; multiplyingexpression : unaryexpression (('*'|'/'|'div') unaryexpression)* ; unaryexpression: ('+'|'-')* primitiveelement; primitiveelement : literalexpression | id ('.' id)? | '(' look ')' ; literalexpression : number | boolean_literal | 'infinity' ; id : identifier ; // l e x c l r u l e s range : '..' ; number : (digits range) => digits {$type=digits;} | (floatliteral) => floatliteral {$type=floatliteral;} | digits {$type=digits;} ; // fragments fragment floatliteral : float; fragment float : digits ( options {greedy = true; } : '.' digit* exponent?) | '.' digits exponent? | digits exponent ; boolean_literal : 'false' | 'true' ; identifier : letter (letter | digit)*; ws : ( ' ' | '\t' | '\r' | '\n' ) {$channel=hidden;} ; fragment letter : ('a'..'z' | 'a'..'z' | '_') ; fragment digits: digit+; fragment digit : '0'..'9'; fragment exponent : ('e'|'e') ('+'|'-')? ('0'..'9')+ ;
any reason why not taking:
in 10;
or
in 10.0;
thanks in advance!
the next things not correct:
you're never matchingfloatliteral
in literalexpression
rule in every alternative of number
you're changing type of token, therefor number
token never created something should work both 11..22
, 1.1..2.2
:
... literalexpression : int | boolean_literal | float | 'infinity' ; id : identifier ; // l e x c l r u l e s range : '..' ; int : (digits range)=> digits | digits (('.' digits exponent? | exponent) {$type=float;})? ; boolean_literal : 'false' | 'true' ; identifier : letter (letter | digit)*; ws : ( ' ' | '\t' | '\r' | '\n' ) {$channel=hidden;} ; fragment letter : ('a'..'z' | 'a'..'z' | '_') ; fragment digits: digit+; fragment digit : '0'..'9'; fragment exponent : ('e'|'e') ('+'|'-')? ('0'..'9')+ ; fragment float : ;
antlr antlr3
No comments:
Post a Comment