-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfcalc.yacc.y
53 lines (45 loc) · 1.3 KB
/
mfcalc.yacc.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
%{
#include <math.h>
#include <stdio.h>
#include "calc.h"
int yylex (void);
void yyerror (char const *);
%}
%union {
double val; /* For returning numbers. */
symrec *tptr; /* For returning symbol-table pointers. */
}
%token <val> NUM /* Simple double precision number. */
%token <tptr> VAR FNCT /* Variable and function. */
%type <val> exp
%right '='
%left '-' '+'
%left '*' '/'
%left NEG /* negation--unary minus */
%right '^' /* exponentiation */
%%
/* The grammar follows. */
input:
/* empty */
| input line
;
line:
'\n'
| exp '\n' { printf ("%.10g\n", $1); }
| error '\n' { yyerrok; }
;
exp:
NUM { $$ = $1; }
| VAR { $$ = $1->value.var; }
| VAR '=' exp { $$ = $3; $1->value.var = $3; }
| FNCT '(' exp ')' { $$ = (*($1->value.fnctptr))($3); }
| exp '+' exp { $$ = $1 + $3; }
| exp '-' exp { $$ = $1 - $3; }
| exp '*' exp { $$ = $1 * $3; }
| exp '/' exp { $$ = $1 / $3; }
| '-' exp %prec NEG { $$ = -$2; }
| exp '^' exp { $$ = pow ($1, $3); }
| '(' exp ')' { $$ = $2; }
;
/* End of grammar. */
%%