-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfixExpressions.c
223 lines (203 loc) · 5.62 KB
/
infixExpressions.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* prefixExp.c, Gerard Renardel, 29 January 2014
*
* In this file functions are defined for the construction of expression trees
* from prefix expressions generated by the following BNF grammar:
*
* <prefexp> ::= <number> | <identifier> | '+' <prefexp> <prefexp>
* | '-' <prefexp> <prefexp> | '*' <prefexp> <prefexp> | '/' <prefexp> <prefexp>
*
* <number> ::= <digit> { <digit> }
*
* <identifier> ::= <letter> { <letter> | <digit> }
*
* Starting pount is the token list obtained from the scanner (in scanner.c).
*/
#include <stdio.h> /* printf */
#include <stdlib.h> /* malloc, free */
#include <assert.h> /* assert */
#include "scanner.h"
#include "recognizeExp.h"
#include "evalExp.h"
#include "prefixExp.h"
/* The function newExpTreeNode creates a new node for an expression tree.
*/
ExpTree newExpTreeNode(TokenType tt, Token t, ExpTree tL, ExpTree tR) {
ExpTree new = malloc(sizeof(ExpTreeNode));
assert (new!=NULL);
new->tt = tt;
new->t = t;
new->left = tL;
new->right = tR;
return new;
}
/* The function valueIdentifier recognizes an identifier in a token list and
* makes the second parameter point to it.
*/
int valueIdentifier(List *lp, char **sp) {
if (*lp != NULL && (*lp)->tt == Identifier ) {
*sp = ((*lp)->t).identifier;
*lp = (*lp)->next;
return 1;
}
return 0;
}
/* The function valueOperator recognizes an arithmetic operator in a token list
* and makes the second parameter point to it.
* Here the auxiliary function isOperator is used.
*/
int isOperator(char c) {
return ( c == '+' || c == '-' || c == '*' || c == '/');
}
int valueOperator(List *lp, char *cp) {
if (*lp != NULL && (*lp)->tt == Symbol && isOperator(((*lp)->t).symbol) ) {
*cp = ((*lp)->t).symbol;
*lp = (*lp)->next;
return 1;
}
return 0;
}
/* De functie freeExpTree frees the memory of the nodes in the expression tree.
* Observe that here, unlike in freeList, the strings in indentifier nodes
* are not freed. The reason is that the function newExpTree does not allocate
* memory for strings in nodes, but only a pointer to a string in a node
* in the token list.
*/
void freeExpTree(ExpTree tr) {
if (tr==NULL) {
return;
}
freeExpTree(tr->left);
freeExpTree(tr->right);
free(tr);
}
/* The function treeExpression tries to build a tree from the tokens in the token list
* (its first argument) and makes its second argument point to the tree.
* The return value indicates whether the action is successful.
* Observe that we use ordinary recursion, not mutual recursion.
*/
int treePrefixExpression(List *lp, ExpTree *tp) {
double w;
char *s;
char c;
Token t;
ExpTree tL, tR;
if ( valueNumber(lp,&w) ) {
t.number = (int)w;
*tp = newExpTreeNode(Number, t, NULL, NULL);
return 1;
}
if ( valueIdentifier(lp,&s) ) {
t.identifier = s;
*tp = newExpTreeNode(Identifier, t, NULL, NULL);
return 1;
}
if ( valueOperator(lp,&c) && treePrefixExpression(lp,&tL) ) {
if ( treePrefixExpression(lp,&tR) ) {
t.symbol = c;
*tp = newExpTreeNode(Symbol, t, tL, tR);
return 1;
} else { /* withuot 'else' the program works fine, but there is a memory leak */
freeExpTree(tL);
return 0;
}
}
return 0;
}
/* The function printExpTreeInfix does what its name suggests.
*/
void printExpTreeInfix(ExpTree tr) {
if (tr == NULL) {
return;
}
switch (tr->tt) {
case Number:
printf("%d",(tr->t).number);
break;
case Identifier:
printf("%s",(tr->t).identifier);
break;
case Symbol:
printf("(");
printExpTreeInfix(tr->left);
printf(" %c ",(tr->t).symbol);
printExpTreeInfix(tr->right);
printf(")");
break;
}
}
/* The function isNumerical checks for an expression tree whether it represents
* a numerical expression, i.e. without identifiers.
*/
int isNumerical(ExpTree tr) {
assert(tr!=NULL);
if (tr->tt==Number) {
return 1;
}
if (tr->tt==Identifier) {
return 0;
}
return (isNumerical(tr->left) && isNumerical(tr->right));
}
/* The function valueExpTree computes the value of an expression tree that represents a
* numerical expression.
*/
double valueExpTree(ExpTree tr) { /* precondition: isNumerical(tr)) */
double lval, rval;
assert(tr!=NULL);
if (tr->tt==Number) {
return (tr->t).number;
}
lval = valueExpTree(tr->left);
rval = valueExpTree(tr->right);
switch ((tr->t).symbol) {
case '+':
return (lval + rval);
case '-':
return (lval - rval);
case '*':
return (lval * rval);
case '/':
assert( rval!=0 );
return (lval / rval);
default:
abort();
}
}
/* the function prefExpressionExpTrees performs a dialogue with the user and tries
* to recognize the input as a prefix expression. When it is a numerical prefix
* expression, its value is computed and printed.
*/
void prefExpTrees() {
char *ar;
List tl, tl1;
ExpTree t = NULL;
printf("give a prefix expression: ");
ar = readInput();
while (ar[0] != '!') {
tl = tokenList(ar);
printf("the token list is ");
printList(tl);
tl1 = tl;
if ( treePrefixExpression(&tl1,&t) && tl1 == NULL ) {
/* there should be no tokens left */
printf("in infix notation: ");
printExpTreeInfix(t);
printf("\n");
if ( isNumerical(t) ) {
printf("the value is %g\n",valueExpTree(t));
} else {
printf("this is not a numerical prefix expression\n");
}
} else {
printf("this is not a prefix expression\n");
}
freeExpTree(t);
t = NULL;
freeTokenList(tl);
free(ar);
printf("\ngive a prefix expression: ");
ar = readInput();
}
free(ar);
printf("good bye\n");
}