forked from QSCTech/zju-icicles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-30.y
115 lines (103 loc) · 3.13 KB
/
5-30.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
%{
#include <stdio.h>
#include <ctype.h>
%}
%code requires {
typedef enum {
TYPE_INTEGER,
TYPE_FLOAT
} value_type_t;
typedef struct {
int integer_value;
float float_value;
value_type_t value_type;
} type;
#define YYLTYPE type
#define YYSTYPE type
}
%token NUMBER
%%
command : exp {
if ($1.value_type == TYPE_INTEGER) {
printf("%d\n", $1.integer_value);
} else {
printf("%f\n", $1.float_value);
}
}
; /* allows printing of the result */
exp : exp '+' term {
if ($1.value_type == TYPE_INTEGER && $3.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_INTEGER;
$$.integer_value = $1.integer_value + $3.integer_value;
} else if ($1.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.integer_value + $3.float_value;
} else if ($3.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.float_value + $3.integer_value;
} else {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.float_value + $3.float_value;
}
}
| exp '-' term {
if ($1.value_type == TYPE_INTEGER && $3.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_INTEGER;
$$.integer_value = $1.integer_value - $3.integer_value;
} else if ($1.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.integer_value - $3.float_value;
} else if ($3.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.float_value - $3.integer_value;
} else {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.float_value - $3.float_value;
}
}
| term { $$ = $1; }
;
term : term '*' factor {
if ($1.value_type == TYPE_INTEGER && $3.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_INTEGER;
$$.integer_value = $1.integer_value * $3.integer_value;
} else if ($1.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.integer_value * $3.float_value;
} else if ($3.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.float_value * $3.integer_value;
} else {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.float_value * $3.float_value;
}
}
| term '/' factor {
if ($1.value_type == TYPE_INTEGER && $3.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_INTEGER;
$$.integer_value = $1.integer_value / $3.integer_value;
} else if ($1.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.integer_value / $3.float_value;
} else if ($3.value_type == TYPE_INTEGER) {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.float_value / $3.integer_value;
} else {
$$.value_type = TYPE_FLOAT;
$$.float_value = $1.float_value / $3.float_value;
}
}
| factor { $$ = $1; }
;
factor : NUMBER { $$ = $1; }
| '(' exp ')' { $$ = $2; }
;
%%
int main() {
return yyparse();
}
/* allows for printing of an error message */
int yyerror(char *s) {
fprintf(stderr, "%s\n", s);
return 0;
}