-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4.y
57 lines (46 loc) · 901 Bytes
/
lab4.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
53
54
55
56
57
%{
#define YYSTYPE double
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int yylex(void);
void yyerror(char*);
%}
%token DOUBLE
%%
expres:
expres expre '\n' {printf("%lf\n",$2);}
|expres single_neg '\n' {printf("%lf\n",$2);}
|
;
single_neg:
'-' DOUBLE {$$= -$2; };
expre:
expr {$$ = $1; }
|expre '+' expr {$$ = $1 + $3; }
| expre '-' expr {$$ = $1 - $3; }
;
expr:
expr '*' exp {$$ = $1 * $3; }
| expr '/' exp {$$ = $1 / $3; }
| exp {$$ = $1; }
;
exp:
ex '^' ex {double x=0.,y=0.,z=0.;x=$1,y=$3,z=pow(x,y);$$=z; }
| ex {$$ = $1; }
;
ex:
DOUBLE {$$ = $1; }
| '(' expre ')' {$$ = $2; }
| '(' '-' DOUBLE ')' {$$ = -$3; }
;
%%
void yyerror(char*s)
{
fprintf(stderr,"%s\n",s);
}
int main(void)
{
yyparse();
return 0;
}