static char sccsid
[] = "@(#)parse.c 4.1 (Berkeley) %G%";
Contains the routines which keep track of the parse stack.
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.
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.
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\b\b\b___ Input\b\b\b\b\b_____ Stack\b\b\b\b\b_____ Note\b\b\b\b____
"dostmt" while (..) Change TOS to <stmt>
anything else while (..) while
"ifstmt" else Change TOS to "ifelse"
{ <stmtl> } Change { <stmtl>
tk An integer code for the maxi token scanned
break_comma = Set to true when in a declaration but not initialization
il = Stack of indentation levels
p_stack = Stack of token codes
search_brace = Set to true if we must look for possibility of moving a
tos = Pointer to top of p_stack, il, and cstk
initial coding November 1976 D A Willcox of CAC
#include "./indent_globs.h";
#include "./indent_codes.h";
/* 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 */
int tk
; /* the code for the construct scanned */
printf ("%2d - %s\n", tk
, token
);
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
case decl
: /* scanned a declaration word */
/* indicate that following brace should be on same line */
if (p_stack
[tos
] != decl
) {
/* only put one declaration onto stack */
/* while in declaration, newline should be forced after comma */
/* only do if we want left justified declarations */
for (i
= tos
- 1; i
> 0; --i
)
/* indentation is number of declaration levels deep we are */
case ifstmt
: /* scanned if (...) */
case forstmt
: /* for (...) */
il
[tos
] = ind_level
= i_l_follow
;
++i_l_follow
; /* subsequent statements should be indented 1 */
case lbrace
: /* scanned { */
/* 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
/* only do this if there is nothing on the line */
/* it is a group as part of a while, for, etc. */
if (p_stack
[tos
] == swstmt
)
/* for a switch, brace should be two levels out from the code
/* allow null stmt between braces */
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
;
case elselit
: /* scanned an else */
if (p_stack
[tos
] != ifhead
) {
printf ("%d: Unmatched else\n", line_no
);
/* indentation for else should be same as for if */
i_l_follow
= ind_level
+ 1;
/* everything following should be in 1 level */
/* remember if with else */
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
];
printf ("%d: Stmt nesting error\n", line_no
);
case swstmt
: /* had switch (...) */
/* save current case indent level */
case_ind
= i_l_follow
+ 1;
/* cases should be one level down from switch */
i_l_follow
+ = 2; /* statements should be two levels in */
case semicolon
: /* this indicates a simple stmt */
/* turn off flag to break after commas in a declaration */
default: /* this is an error */
printf ("%d: Unknown code to parser - %d\n", line_no
, tk
);
reduce (); /* see if any reduction can be done */
for (i
= 1; i
<= tos
; ++i
)
printf ("(%d %d)", p_stack
[i
], il
[i
]);
Implements the reduce part of the parsing algorithm
The following reductions are done. Reductions are repeated until no
Old\b\b\b___ TOS\b\b\b___ New\b\b\b___ TOS\b\b\b___
On each reduction, i_l_follow (the indentation for the following line)
is set to the indentation level associated with the old TOS.
initial coding November 1976 D A Willcox of CAC
/*----------------------------------------------*\
\*----------------------------------------------*/
/* local looping variable */
for (;;) { /* keep looping until there is nothing left to
switch (p_stack
[tos
- 1]) {
/* stmtl stmt or stmt stmt */
/* 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 */
case_ind
= cstk
[tos
- 1];
case decl
: /* finish of a declaration */
/* <<if> <stmt> else> <stmt> */
default: /* <anything else> <stmt> */
} /* end of section for <stmt> on top of stack */
case whilestmt
: /* while (...) on top */
if (p_stack
[tos
- 1] == dohead
) {
/* it is termination of a do while */
default: /* anything else on top */
} /* end of big switch */
} /* end of reduction phase for (;;) */