first finished implementation of Quantity parser

This commit is contained in:
jriegel
2013-09-22 18:03:28 +02:00
parent 3c5fb838ad
commit 7fe834ffbe
13 changed files with 1988 additions and 1184 deletions

View File

@@ -11,6 +11,7 @@
/* Bison declarations. */
%token UNIT NUM
%token ACOS ASIN ATAN ATAN2 COS EXP ABS MOD LOG LOG10 POW SIN SINH TAN TANH SQRT;
%left '-' '+'
%left '*' '/'
%left NEG /* negation--unary minus */
@@ -22,19 +23,43 @@
%%
input: exp { QuantResult = $1 ; }
;
exp: NUM { $$ = $1; }
| UNIT { $$ = $1; }
| NUM UNIT { $$ = $1*$2; }
| exp '+' exp { $$ = $1 + $3; }
| exp '-' exp { $$ = $1 - $3; }
| exp '*' exp { $$ = $1 * $3; }
| exp '/' exp { $$ = $1 / $3; }
| '-' exp %prec NEG { $$ = -$2; }
| exp '^' NUM { $$ = $1.pow($3); }
| '(' exp ')' { $$ = $2; }
input: num { QuantResult = $1 ; }
| unit { QuantResult = $1 ; }
| quantity { QuantResult = $1 ; }
| quantity quantity { QuantResult = $1 + $2; }
;
num: NUM { $$ = $1; }
| num '+' num { $$ = $1.getValue() + $3.getValue(); }
| num '-' num { $$ = $1.getValue() - $3.getValue(); }
| num '*' num { $$ = $1.getValue() * $3.getValue(); }
| num '/' num { $$ = $1.getValue() / $3.getValue(); }
| '-' num %prec NEG { $$ = -$2.getValue(); }
| num '^' num { $$ = pow ($1.getValue(), $3.getValue());}
| '(' num ')' { $$ = $2; }
| ACOS '(' num ')' { $$ = acos($3.getValue()); }
| ASIN '(' num ')' { $$ = asin($3.getValue()); }
| ATAN '(' num ')' { $$ = atan($3.getValue()); }
| ATAN2 '(' num ',' num ')' { $$ = atan2($3.getValue(),$5.getValue());}
| ABS '(' num ')' { $$ = fabs($3.getValue()); }
| EXP '(' num ')' { $$ = exp($3.getValue()); }
| MOD '(' num ',' num ')' { $$ = fmod($3.getValue(),$5.getValue()); }
| LOG '(' num ')' { $$ = log($3.getValue()); }
| LOG10 '(' num ')' { $$ = log10($3.getValue()); }
| POW '(' num ',' num ')' { $$ = pow($3.getValue(),$5.getValue()); }
| SIN '(' num ')' { $$ = sin($3.getValue()); }
| SINH '(' num ')' { $$ = sinh($3.getValue()); }
| TAN '(' num ')' { $$ = tan($3.getValue()); }
| TANH '(' num ')' { $$ = tanh($3.getValue()); }
| SQRT '(' num ')' { $$ = tanh($3.getValue()); }
| COS '(' num ')' { $$ = cos($3.getValue()); }
;
unit: UNIT { $$ = $1; }
| unit '*' unit { $$ = $1 * $3; }
| unit '/' unit { $$ = $1 / $3; }
| unit '^' num { $$ = $1.pow ($3); }
;
quantity: num unit { $$ = $1*$2; }
;