// additional functionality of power right associative
grammar Exp;
options{
k=2;
}
/* This will be the entry point of our parser. */
eval returns [double value]
: additionExp{$value=$additionExp.value; System.out.println($value);}
;
/* Addition and subtraction have the lowest precedence. */
additionExp returns [double value]
: m1=multiplyExp {$value=$m1.value;}
( '+' m2=multiplyExp {$value+=$m2.value;}
| '-' m2=multiplyExp {$value-=$m2.value;}
)*
;
/* Multiplication and division have a higher precedence. */
multiplyExp returns [double value]
: a1=restExp {$value=$a1.value;}
( '*' a2=restExp {$value*=$a2.value;}
| '/' a2=restExp {$value/=$a2.value;}
| '%' a2=restExp {$value=$value\%$a2.value;}
)*
;
/* Power have a higher precedence. */
restExp returns [double value]
: a1=incdecExp {$value=$a1.value;}
(
'^' a2=restExp {$value=Math.pow($value,$a2.value);}
)*
;
/* Power have a higher precedence. */
incdecExp returns [double value]
: a1=atomExp {$value=$a1.value;}
('++' {$value+=1;} )*
;
/* An expression atom is the smallest part of an expression: a number. Or
when we encounter parenthesis, we're making a recursive call back to the
rule 'additionExp'. As you can see, an 'atomExp' has the highest precedence. */
atomExp returns [double value]
: Number {$value=Double.parseDouble($Number.text);}
| '(' additionExp ')' {$value=$additionExp.value;}
;
/* A number: can be an integer value, or a decimal value */
Number
: ('0'..'9')+ ('.' ('0'..'9')+)?
;
/* We're going to ignore all white space characters */
WS
: (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;}
;
Sunday, September 2, 2012
[ANTLR]Simple Expression evalution using ANTLR
Subscribe to:
Post Comments (Atom)