-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.c,v
463 lines (384 loc) · 10.5 KB
/
parse.c,v
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
head 1.3;
access ;
symbols ;
locks abs:1.3;
comment @ * @;
1.3
date 89.11.17.21.29.09; author abs; state Exp;
branches ;
next 1.2;
1.2
date 88.05.10.18.20.13; author root; state Exp;
branches ;
next 1.1;
1.1
date 88.05.10.17.36.34; author root; state Exp;
branches ;
next ;
desc
@
@
1.3
log
@checked in XENIX
@
text
@/*
**
** Эта штука добавляется в исходный файл перед
** запуском rcs Давидов 04.28.88
**
** $Header: parse.c,v 1.2 88/05/10 18:20:13 root Exp $
** $Log: parse.c,v $
* Revision 1.2 88/05/10 18:20:13 root
*
*
* Revision 1.1 88/05/10 17:36:34 root
* Initial revision
*
*/
# ifndef lint
/* NOXSTR */
static char Rcs_id[] = "$Header: parse.c,v 1.2 88/05/10 18:20:13 root Exp $";
/* YESXSTR */
# endif lint
/*
FILE NAME:
parse.c
PURPOSE:
Contains the routines which keep track of the parse stack.
GLOBALS:
p_stack = The parse stack, set by both routines
il = Stack of indentation levels, set by parse
cstk = Stack of case statement indentation levels, set by parse
tos = Pointer to top of stack, set by both routines.
FUNCTIONS:
parse
reduce
*/
/*
NAME:
parse
FUNCTION:
Parse is given one input which is a "maxi token" just scanned from
input. Maxi tokens are signifigant constructs such as else, {, do,
if (...), etc. Parse works with reduce to maintain a parse stack
of these constructs. Parse is responsible for the "shift" portion
of the parse algorithm, and reduce handles the "reduce" portion.
ALGORITHM:
1) If there is "ifstmt" on the stack and input is anything other than
an else, then change the top of stack (TOS) to <stmt>. Do a reduce.
2) Use a switch statement to implement the following shift operations:
TOS___ Input_____ Stack_____ Note____
decl decl nothing
anything else decl decl
"dostmt" while (..) Change TOS to <stmt>
anything else while (..) while
"ifstmt" else Change TOS to "ifelse"
{ <stmtl> } Change { <stmtl>
to <stmtl>
switch (..) switch
do do
for(..) for
; <stmt>
{ { <stmt>
PARAMETERS:
tk An integer code for the maxi token scanned
RETURNS:
Nothing
GLOBALS:
break_comma = Set to true when in a declaration but not initialization
btype_2
case_ind =
cstk =
i_l_follow =
il = Stack of indentation levels
ind_level =
p_stack = Stack of token codes
search_brace = Set to true if we must look for possibility of moving a
brace
tos = Pointer to top of p_stack, il, and cstk
CALLS:
printf (lib)
reduce
CALLED BY:
main
HISTORY:
initial coding November 1976 D A Willcox of CAC
*/
#include "./in_globs.h";
#include "./in_codes.h";
int p_stack[50] = { stmt };
/* this is the parser's stack */
int il[50]; /* this stack stores indentation levels */
int cstk[50]; /* used to store case stmt indentation levels */
int tos = 0; /* pointer to top of stack */
parse (tk)
int tk; /* the code for the construct scanned */
{
int i;
#ifdef debug
printf ("%2d - %s\n", tk, token);
#endif
while (p_stack[tos] == ifhead && tk != elselit) {
/* true if we have an if without an else */
p_stack[tos] = stmt; /* apply the if(..) stmt ::= stmt reduction */
reduce (); /* see if this allows any reduction */
}
switch (tk) { /* go on and figure out what to do with the input */
case decl: /* scanned a declaration word */
search_brace = btype_2;
/* indicate that following brace should be on same line */
if (p_stack[tos] != decl) {
/* only put one declaration onto stack */
break_comma = true;
/* while in declaration, newline should be forced after comma */
p_stack[++tos] = decl;
il[tos] = i_l_follow;
if (ljust_decl) {
/* only do if we want left justified declarations */
ind_level = 0;
for (i = tos - 1; i > 0; --i)
if (p_stack[i] == decl)
++ind_level;
/* indentation is number of declaration levels deep we are */
i_l_follow = ind_level;
}
}
break;
case ifstmt: /* scanned if (...) */
case dolit: /* 'do' */
case forstmt: /* for (...) */
p_stack[++tos] = tk;
il[tos] = ind_level = i_l_follow;
++i_l_follow; /* subsequent statements should be indented 1 */
search_brace = btype_2;
break;
case lbrace: /* scanned { */
break_comma = false;
/* don't break comma in an initial list */
if (p_stack[tos] == stmt || p_stack[tos] == decl
|| p_stack[tos] == stmtl)
++i_l_follow; /* it is a random, isolated stmt group or a declaration */
else {
if (s_code == e_code) {
/* only do this if there is nothing on the line */
--ind_level;
/* it is a group as part of a while, for, etc. */
if (p_stack[tos] == swstmt)
--ind_level;
/* for a switch, brace should be two levels out from the code */
}
}
p_stack[++tos] = lbrace;
il[tos] = ind_level;
p_stack[++tos] = stmt;
/* allow null stmt between braces */
il[tos] = i_l_follow;
break;
case whilestmt: /* scanned while (...) */
if (p_stack[tos] == dohead) {
/* it is matched with do stmt */
ind_level = i_l_follow = il[tos];
p_stack[++tos] = whilestmt;
il[tos] = ind_level = i_l_follow;
}
else { /* it is a while loop */
p_stack[++tos] = whilestmt;
il[tos] = i_l_follow;
++i_l_follow;
search_brace = btype_2;
}
break;
case elselit: /* scanned an else */
if (p_stack[tos] != ifhead) {
printf ("%d: Unmatched else\n", line_no);
}
else {
ind_level = il[tos];
/* indentation for else should be same as for if */
i_l_follow = ind_level + 1;
/* everything following should be in 1 level */
p_stack[tos] = elsehead;
/* remember if with else */
search_brace = btype_2;
}
break;
case rbrace: /* scanned a } */
/* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
if (p_stack[tos - 1] == lbrace) {
ind_level = i_l_follow = il[--tos];
p_stack[tos] = stmt;
}
else {
printf ("%d: Stmt nesting error\n", line_no);
}
break;
case swstmt: /* had switch (...) */
p_stack[++tos] = swstmt;
cstk[tos] = case_ind;
/* save current case indent level */
il[tos] = i_l_follow;
case_ind = i_l_follow + 1;
/* cases should be one level down from switch */
i_l_follow += 2; /* statements should be two levels in */
search_brace = btype_2;
break;
case semicolon: /* this indicates a simple stmt */
break_comma = false;
/* turn off flag to break after commas in a declaration */
p_stack[++tos] = stmt;
il[tos] = ind_level;
break;
default: /* this is an error */
printf ("%d: Unknown code to parser - %d\n", line_no, tk);
return;
} /* end of switch */
reduce (); /* see if any reduction can be done */
#ifdef debug
for (i = 1; i <= tos; ++i)
printf ("(%d %d)", p_stack[i], il[i]);
printf ("\n");
#endif
return;
}
/*
NAME:
reduce
FUNCTION:
Implements the reduce part of the parsing algorithm
ALGORITHM:
The following reductions are done. Reductions are repeated until no
more are possible.
Old___ TOS___ New___ TOS___
<stmt> <stmt> <stmtl>
<stmtl> <stmt> <stmtl>
do <stmt> "dostmt"
if <stmt> "ifstmt"
switch <stmt> <stmt>
decl <stmt> <stmt>
"ifelse" <stmt> <stmt>
for <stmt> <stmt>
while <stmt> <stmt>
"dostmt" while <stmt>
On each reduction, i_l_follow (the indentation for the following line)
is set to the indentation level associated with the old TOS.
PARAMETERS:
None
RETURNS:
Nothing
GLOBALS:
cstk
i_l_follow =
il
p_stack =
tos =
CALLS:
None
CALLED BY:
parse
HISTORY:
initial coding November 1976 D A Willcox of CAC
*/
/*----------------------------------------------*\
| REDUCTION PHASE
\*----------------------------------------------*/
reduce () {
register int i;
/* local looping variable */
for (;;) { /* keep looping until there is nothing left to reduce */
switch (p_stack[tos]) {
case stmt:
switch (p_stack[tos - 1]) {
case stmt:
case stmtl:
/* stmtl stmt or stmt stmt */
p_stack[--tos] = stmtl;
break;
case dolit:
/* <do> <stmt> */
p_stack[--tos] = dohead;
i_l_follow = il[tos];
break;
case ifstmt:
/* <if> <stmt> */
p_stack[--tos] = ifhead;
for (i = tos - 1;
(
p_stack[i] != stmt
&&
p_stack[i] != stmtl
&&
p_stack[i] != lbrace
);
--i);
i_l_follow = il[i];
/* for the time being, we will assume that there is no else on this if, and set the indentation level accordingly. If an else is scanned, it will be fixed up later */
break;
case swstmt:
/* <switch> <stmt> */
case_ind = cstk[tos - 1];
case decl: /* finish of a declaration */
case elsehead:
/* <<if> <stmt> else> <stmt> */
case forstmt:
/* <for> <stmt> */
case whilestmt:
/* <while> <stmt> */
p_stack[--tos] = stmt;
i_l_follow = il[tos];
break;
default: /* <anything else> <stmt> */
return;
} /* end of section for <stmt> on top of stack */
break;
case whilestmt: /* while (...) on top */
if (p_stack[tos - 1] == dohead) {
/* it is termination of a do while */
p_stack[--tos] = stmt;
break;
}
else
return;
default: /* anything else on top */
return;
} /* end of big switch */
} /* end of reduction phase for (;;) */
}
@
1.2
log
@
@
text
@d6 5
a10 2
** $Header: parse.c,v 1.1 88/05/10 17:36:34 root Exp $
** $Log: parse.c,v $
d18 1
a18 1
static char Rcs_id[] = "$Header: parse.c,v 1.1 88/05/10 17:36:34 root Exp $";
d104 1
a104 1
int p_stack[50] = stmt;
d235 1
a235 1
i_l_follow + = 2; /* statements should be two levels in */
@
1.1
log
@Initial revision
@
text
@d6 5
a10 2
** $Header$
** $Log$
d15 1
a15 1
static char Rcs_id[] = "$Header$";
d97 2
a98 2
#include "./indent_globs.h";
#include "./indent_codes.h";
@