-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.c
165 lines (153 loc) · 3.94 KB
/
calc.c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Implementation of a calculator in infix notation */
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "lexer.h"
#include "list.h"
/* look a-head */
static token current;
static int att; /* attribute of current */
/* prototype */
void parse_exp2(list l, int *res);
/* parsing of token */
void parse_token(token expected) {
if (current != expected) {
printf("ERROR: expected token ");
display(expected, -1);
printf(" but found ");
display(current, att);
printf("\n");
exit(1);
}
if (current != END) {
current = next(&att);
}
}
void parse_exp0(list l, int *res){
int n;
switch (current){
case INT:
*res = att;
parse_token(INT);
break;
case VAR:
*res = pick(l, att);
parse_token(VAR);
break;
case OPAR:
parse_token(OPAR);
parse_exp2(l, res);
parse_token(CPAR);
break;
case MINUS:
parse_token(MINUS);
parse_exp0(l, &n);
*res = -n;
break;
default:
unexpected(current, att, "parse_exp0");
break;
}
}
void _parse_exp1(list l, int n, int *res){
int n0;
switch (current){
case MULT:
parse_token(MULT);
parse_exp0(l, &n0);
_parse_exp1(l, n * n0, res);
break;
case DIV:
parse_token(DIV);
parse_exp0(l, &n0);
_parse_exp1(l, n / n0, res);
break;
case PLUS:case MINUS:case CPAR:case QUEST:
*res = n;
break;
default:
unexpected(current, att, "parse_exp1");
break;
}
}
void parse_exp1(list l, int *res){
int n;
switch (current){
case MINUS:case INT:case VAR: case OPAR:
parse_exp0(l, &n);
_parse_exp1(l, n, res);
break;
default:
unexpected(current, att, "parse_exp1");
break;
}
}
void _parse_exp2(list l, int n, int *res){
int n0;
switch (current){
case PLUS:
parse_token(PLUS);
parse_exp1(l, &n0);
_parse_exp2(l, n + n0, res);
break;
case MINUS:
parse_token(MINUS);
parse_exp1(l, &n0);
_parse_exp2(l, n - n0, res);
break;
case CPAR:case QUEST:
*res = n;
break;
default:
unexpected(current, att, "parse_exp2");
break;
}
}
void parse_exp2(list l, int *res){
int n;
switch (current){
case MINUS:case INT:case VAR: case OPAR:
parse_exp1(l, &n);
_parse_exp2(l, n, res);
break;
default:
unexpected(current, att, "parse_exp2");
break;
}
}
void parse_input(list l, list *res){
int n;
switch (current){
case MINUS:case INT:case VAR:case OPAR:
parse_exp2(l, &n);
append(l, n);
parse_token(QUEST);
parse_input(l, res);
break;
case END:
*res = l;
break;
default:
unexpected(current, att, "parse_input");
break;
}
}
list parse(){
list res;
list l = empty();
parse_input(l, &res);
return res;
}
int main() {
printf("// Mini-calculator.\n//\n") ;
printf("// Enter below a sequence of integer computations (using infix notation).\n");
printf("// Each computation must end with '?'.\n") ;
printf("// Type Ctrl-D to quit.\n") ;
/* A FAIRE */
current = next(&att); /* init de current */
list res=empty();
parse_input(empty(), &res);
/* Ligne suivante à garder, pour vérifier qu'on sort proprement */
printf("// End of Input: Bye !\n") ;
return 0;
}