-
Notifications
You must be signed in to change notification settings - Fork 2
/
Po_semantics.cpp
359 lines (321 loc) · 17.1 KB
/
Po_semantics.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*******************************************************************/
/*** FILE : Po_semantics.c ***/
/*** AUTHOR: Sekhar Muddana ***/
/*** PUBLIC ROUTINES: ***/
/*** void Reduce_semantics() ***/
/*** void Store_semantics() ***/
/*** void Print_sem_stack() ***/
/*** PRIVATE ROUTINES: ***/
/*** void Push_tnode_ptr() ***/
/*** void Pop_tnode_ptr() ***/
/*** MODULE DESCRIPTION: ***/
/*** This module contains routines for doing Semantics ***/
/*** while parsing the polynomial string. ***/
/*** These routines are called from Po_parse_poly.c ***/
/*******************************************************************/
#include <stdio.h>
#include "Po_parse_poly.h"
#include "Po_syn_stack.h"
#include "Po_semantics.h"
#include "Memory_routines.h"
static void Push_tnode_ptr(struct unexp_tnode *Sem_stack[], int *Sem_sp_ptr, struct unexp_tnode *Temp_tnode_ptr);
static struct unexp_tnode *Pop_tnode_ptr(struct unexp_tnode *Sem_stack[], int *Sem_sp_ptr);
#if 0
static void Print_Sem_stack(struct unexp_tnode *Sem_stack[], int Sem_sp);
#endif
/*******************************************************************/
/* MODIFIES: */
/* Sem_stack -- Semantic stack, stack of pointers to tnodes. */
/* *Sem_sp_ptr -- Semantic stack pointer. */
/* REQUIRES: */
/* Reduction_num -- Reduction number. */
/* RETURNS: None. */
/* FUNCTION: */
/* 1.Semantic stack manipulation: */
/* Manipulates the semantic stack by pushing and/or popping */
/* tnodes, depending on Reduction number. */
/* 2.Generation of partial tree: */
/* NOTE: */
/* There is a 1-1 correspondence between syntax and semantic */
/* stack. */
/*******************************************************************/
void Reduce_semantics(int Reduction_num, struct unexp_tnode *Sem_stack[], int *Sem_sp_ptr)
{
/*int i;*/
struct unexp_tnode *Temp_tnode_ptr;
struct unexp_tnode *temp_tnode_ptr1;
struct unexp_tnode *temp_tnode_ptr2;
/*struct unexp_tnode *trash;*/
switch (Reduction_num) {
case 1 : /* polynomial -> term */
break;
case 2 : /* polynomial -> - term */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = UNARY_MINUS;
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 3 : /* polynomial -> + term */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = UNARY_PLUS;
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 4 : /* polynomial -> polynomial + term */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = ADDITION;
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 5 : /* polynomial -> polynomial - term */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = SUBTRACTION;
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 6 : /* term -> product */
break;
case 7 : /* term -> int product */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = SCALAR_MULT;
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 8 : /* product -> atom_or_dbl_atom */
break;
case 9 : /* product -> atom_or_dbl_atom ^ int */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = EXPONENTIATION;
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 10 : /* atom_or_dbl_atom -> atom_or_dbl_atom * atom_or_dbl_atom */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = JORDAN_PROD;
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 11 : /* atom_or_dbl_atom -> ratom */
break;
case 12 : /* atom_or_dbl_atom -> double_atom */
break;
case 13 : /* double_atom -> atom ratom */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = JUXT_PROD;
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 14 : /* ratom -> atom */
case 15 : /* atom -> small_letter */
case 16 : /* atom -> commutator */
case 17 : /* atom -> associator */
case 18 : /* atom -> jacobi */
case 19 : /* atom -> jordan_associator */
case 20 : /* atom -> operator_product */
case 21 : /* atom -> artificial_word */
break;
case 22 : /* atom -> ( polynomial- ) */
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 23 : /* commutator -> [ polynomial-, polynomial- ] */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = COMMUTATION;
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 24 : /* associator -> ( polynomial-, polynomial-, polynomial- ) */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = ASSOCIATION;
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand3 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 25 : /* jacobi -> J(polynomial-,polynomial-,polynomial-) */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = JACOBI;
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand3 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 26 : /* jordan_associator -> <polynomial-,polynomial-,polynomial-> */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = JORDAN_ASSOC;
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand3 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 27 : /* polynomial- -> polynomial */
break;
case 28 : /* operator_product -> { atom operator_list- } */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = OPERATOR_PROD;
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 29 : /* operator_list- -> operator_list */
case 30 : /* operator_list -> operator */
break;
case 31 : /* operator_list -> operator_list operator */
temp_tnode_ptr1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
temp_tnode_ptr2 = Temp_tnode_ptr;
while (temp_tnode_ptr2->next != NULL)
temp_tnode_ptr2 = temp_tnode_ptr2->next;
temp_tnode_ptr2->next = temp_tnode_ptr1;
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 32 : /* operator -> atom ' */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = LEFT_MULT;
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 33 : /* operator -> atom ` */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = RIGHT_MULT;
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 34 : /* artificial_word -> artificial_word- } */
break;
case 35 : /* artificial_word- -> artificial_word- : atom */
temp_tnode_ptr1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
temp_tnode_ptr2 = Temp_tnode_ptr->operand2;
while (temp_tnode_ptr2->next != NULL)
temp_tnode_ptr2 = temp_tnode_ptr2->next;
temp_tnode_ptr2->next = temp_tnode_ptr1;
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
case 36 : /* artificial_word -> W { int : atom */
Temp_tnode_ptr = Unexp_tnode_alloc();
Temp_tnode_ptr->op = ARTIFICIAL_WORD;
Temp_tnode_ptr->operand2 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Temp_tnode_ptr->operand1 = Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
/*trash =*/ Pop_tnode_ptr(Sem_stack,Sem_sp_ptr);
Push_tnode_ptr(Sem_stack,Sem_sp_ptr,Temp_tnode_ptr);
break;
default :
printf("Error : Inavlid type\n");
}
}
/*******************************************************************/
/* MODIFIES: */
/* Sem_stack -- Semantic stack, stack of pointers to tnodes. */
/* REQUIRES: */
/* Sem_sp -- Semantic stack pointer. */
/* Token -- Token number. */
/* Current_letter -- letter to be stored in the stack. */
/* Current_int -- number to be stored in the stack. */
/* RETURNS: None. */
/* FUNCTION: */
/* Get a new tnode. Change its fields to represent a letter or */
/* an integer. */
/* NOTE: */
/*******************************************************************/
void Store_semantics(struct unexp_tnode *Sem_stack[], int Sem_sp, int Token, char Current_letter, int Current_int)
{
if (Token == LETTER) {
Sem_stack[Sem_sp] = Unexp_tnode_alloc();
Sem_stack[Sem_sp]->op = SMALL_LETTER;
Sem_stack[Sem_sp]->s_letter = Current_letter;
}
else if (Token == INT) {
Sem_stack[Sem_sp] = Unexp_tnode_alloc();
Sem_stack[Sem_sp]->op = SCALAR;
Sem_stack[Sem_sp]->scalar_num = Current_int;
}
}
/*******************************************************************/
/* MODIFIES: */
/* Sem_stack -- Semantic stack, stack of pointers to tnodes. */
/* *Sem_sp_ptr -- Semantic stack pointer. */
/* REQUIRES: */
/* Temp_tnode_ptr -- Pointer to tnode which is to be pushed. */
/* RETURNS: None. */
/*******************************************************************/
void Push_tnode_ptr(struct unexp_tnode *Sem_stack[], int *Sem_sp_ptr, struct unexp_tnode *Temp_tnode_ptr)
{
if (*Sem_sp_ptr < STACK_SIZE - 1)
Sem_stack[++(*Sem_sp_ptr)] = Temp_tnode_ptr;
else
printf("sematic stack overflow\n");
}
/*******************************************************************/
/* MODIFIES: */
/* Sem_stack -- Semantic stack, stack of pointers to tnodes. */
/* *Sem_sp_ptr -- Semantic stack pointer. */
/* REQUIRES: None. */
/* RETURNS: */
/* Pointer to the tnode stored at the top of the stack. */
/*******************************************************************/
struct unexp_tnode *Pop_tnode_ptr(struct unexp_tnode *Sem_stack[], int *Sem_sp_ptr)
{
if ((*Sem_sp_ptr > 0) && (*Sem_sp_ptr < STACK_SIZE))
return(Sem_stack[(*Sem_sp_ptr)--]);
printf("sematic stack underflow\n");
return NULL;
}
#if 0
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: */
/* Sem_stack -- Semantic stack, stack of pointers to tnodes. */
/* Sem_sp -- Semantic stack pointer. */
/* RETURNS: None. */
/* NOTE: */
/* Called only when DEBUG_PARSE flag is on. */
/*******************************************************************/
void Print_Sem_stack(struct unexp_tnode *Sem_stack[], int Sem_sp)
{
int i;
for (i=0;i<=Sem_sp;i++)
if (Sem_stack[i] != NULL) {
printf("Sem_stack[%d] = %c\n",i,Sem_stack[i]->s_letter);
printf("Sem_stack[%d] = %d\n",i,Sem_stack[i]->scalar_num);
}
}
#endif