121971Sdist /* 235500Sbostic * Copyright (c) 1985 Sun Microsystems, Inc. 335500Sbostic * Copyright (c) 1980 The Regents of the University of California. 433767Sbostic * Copyright (c) 1976 Board of Trustees of the University of Illinois. 533767Sbostic * All rights reserved. 633767Sbostic * 733767Sbostic * Redistribution and use in source and binary forms are permitted 834885Sbostic * provided that the above copyright notice and this paragraph are 934885Sbostic * duplicated in all such forms and that any documentation, 1034885Sbostic * advertising materials, and other materials related to such 1134885Sbostic * distribution and use acknowledge that the software was developed 1235500Sbostic * by the University of California, Berkeley, the University of Illinois, 1335500Sbostic * Urbana, and Sun Microsystems, Inc. The name of either University 1435500Sbostic * or Sun Microsystems may not be used to endorse or promote products 1535500Sbostic * derived from this software without specific prior written permission. 1634885Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1734885Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1834885Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1921971Sdist */ 208805Smckusick 2121971Sdist #ifndef lint 22*36969Sbostic static char sccsid[] = "@(#)parse.c 5.10 (Berkeley) 03/05/89"; 2333767Sbostic #endif /* not lint */ 2421971Sdist 25*36969Sbostic #include "indent_globs.h" 26*36969Sbostic #include "indent_codes.h" 278805Smckusick 2824456Smckusick parse(tk) 2924456Smckusick int tk; /* the code for the construct scanned */ 308805Smckusick { 3124456Smckusick int i; 328805Smckusick 338805Smckusick #ifdef debug 3424456Smckusick printf("%2d - %s\n", tk, token); 358805Smckusick #endif 3635500Sbostic 3724456Smckusick while (ps.p_stack[ps.tos] == ifhead && tk != elselit) { 3824456Smckusick /* true if we have an if without an else */ 3924456Smckusick ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt 4035500Sbostic * reduction */ 4124456Smckusick reduce(); /* see if this allows any reduction */ 428805Smckusick } 438805Smckusick 448805Smckusick 4535500Sbostic switch (tk) { /* go on and figure out what to do with the 4635500Sbostic * input */ 478805Smckusick 4835500Sbostic case decl: /* scanned a declaration word */ 4935500Sbostic ps.search_brace = btype_2; 5035500Sbostic /* indicate that following brace should be on same line */ 5135500Sbostic if (ps.p_stack[ps.tos] != decl) { /* only put one declaration 5235500Sbostic * onto stack */ 5335500Sbostic break_comma = true; /* while in declaration, newline should be 5435500Sbostic * forced after comma */ 5535500Sbostic ps.p_stack[++ps.tos] = decl; 5635500Sbostic ps.il[ps.tos] = ps.i_l_follow; 578805Smckusick 5835500Sbostic if (ps.ljust_decl) {/* only do if we want left justified 5935500Sbostic * declarations */ 6035500Sbostic ps.ind_level = 0; 6135500Sbostic for (i = ps.tos - 1; i > 0; --i) 6235500Sbostic if (ps.p_stack[i] == decl) 6335500Sbostic ++ps.ind_level; /* indentation is number of 6435500Sbostic * declaration levels deep we are */ 6535500Sbostic ps.i_l_follow = ps.ind_level; 668805Smckusick } 6735500Sbostic } 6835500Sbostic break; 698805Smckusick 7035500Sbostic case ifstmt: /* scanned if (...) */ 7135500Sbostic if (ps.p_stack[ps.tos] == elsehead && ps.else_if) /* "else if ..." */ 7235500Sbostic ps.i_l_follow = ps.il[ps.tos]; 7335500Sbostic case dolit: /* 'do' */ 7435500Sbostic case forstmt: /* for (...) */ 7535500Sbostic ps.p_stack[++ps.tos] = tk; 7635500Sbostic ps.il[ps.tos] = ps.ind_level = ps.i_l_follow; 7735500Sbostic ++ps.i_l_follow; /* subsequent statements should be indented 1 */ 7835500Sbostic ps.search_brace = btype_2; 7935500Sbostic break; 808805Smckusick 8135500Sbostic case lbrace: /* scanned { */ 8235500Sbostic break_comma = false; /* don't break comma in an initial list */ 8335500Sbostic if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl 8435500Sbostic || ps.p_stack[ps.tos] == stmtl) 8535500Sbostic ++ps.i_l_follow; /* it is a random, isolated stmt group or a 8635500Sbostic * declaration */ 8735500Sbostic else { 8835500Sbostic if (s_code == e_code) { 8935500Sbostic /* 9035500Sbostic * only do this if there is nothing on the line 9135500Sbostic */ 9235500Sbostic --ps.ind_level; 9335500Sbostic /* 9435500Sbostic * it is a group as part of a while, for, etc. 9535500Sbostic */ 9635500Sbostic if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1) 9724456Smckusick --ps.ind_level; 9835500Sbostic /* 9935500Sbostic * for a switch, brace should be two levels out from the code 10035500Sbostic */ 1018805Smckusick } 10235500Sbostic } 1038805Smckusick 10435500Sbostic ps.p_stack[++ps.tos] = lbrace; 10535500Sbostic ps.il[ps.tos] = ps.ind_level; 10635500Sbostic ps.p_stack[++ps.tos] = stmt; 10735500Sbostic /* allow null stmt between braces */ 10835500Sbostic ps.il[ps.tos] = ps.i_l_follow; 10935500Sbostic break; 11035500Sbostic 11135500Sbostic case whilestmt: /* scanned while (...) */ 11235500Sbostic if (ps.p_stack[ps.tos] == dohead) { 11335500Sbostic /* it is matched with do stmt */ 11435500Sbostic ps.ind_level = ps.i_l_follow = ps.il[ps.tos]; 11535500Sbostic ps.p_stack[++ps.tos] = whilestmt; 11635500Sbostic ps.il[ps.tos] = ps.ind_level = ps.i_l_follow; 11735500Sbostic } 11835500Sbostic else { /* it is a while loop */ 11935500Sbostic ps.p_stack[++ps.tos] = whilestmt; 12024456Smckusick ps.il[ps.tos] = ps.i_l_follow; 12135500Sbostic ++ps.i_l_follow; 12235500Sbostic ps.search_brace = btype_2; 12335500Sbostic } 1248805Smckusick 12535500Sbostic break; 1268805Smckusick 12735500Sbostic case elselit: /* scanned an else */ 1288805Smckusick 12935500Sbostic if (ps.p_stack[ps.tos] != ifhead) 13035500Sbostic diag(1, "Unmatched 'else'"); 13135500Sbostic else { 13235500Sbostic ps.ind_level = ps.il[ps.tos]; /* indentation for else should 13335500Sbostic * be same as for if */ 13435500Sbostic ps.i_l_follow = ps.ind_level + 1; /* everything following should 13535500Sbostic * be in 1 level */ 13635500Sbostic ps.p_stack[ps.tos] = elsehead; 13735500Sbostic /* remember if with else */ 13835500Sbostic ps.search_brace = btype_2 | ps.else_if; 13935500Sbostic } 14035500Sbostic break; 1418805Smckusick 14235500Sbostic case rbrace: /* scanned a } */ 14335500Sbostic /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */ 14435500Sbostic if (ps.p_stack[ps.tos - 1] == lbrace) { 14535500Sbostic ps.ind_level = ps.i_l_follow = ps.il[--ps.tos]; 14635500Sbostic ps.p_stack[ps.tos] = stmt; 14735500Sbostic } 14835500Sbostic else 14935500Sbostic diag(1, "Stmt nesting error."); 15035500Sbostic break; 1518805Smckusick 15235500Sbostic case swstmt: /* had switch (...) */ 15335500Sbostic ps.p_stack[++ps.tos] = swstmt; 15435500Sbostic ps.cstk[ps.tos] = case_ind; 15535500Sbostic /* save current case indent level */ 15635500Sbostic ps.il[ps.tos] = ps.i_l_follow; 15735500Sbostic case_ind = ps.i_l_follow + ps.case_indent; /* cases should be one 15824456Smckusick * level down from 15924456Smckusick * switch */ 16035502Sbostic ps.i_l_follow += ps.case_indent + 1; /* statements should be two 16135500Sbostic * levels in */ 16235500Sbostic ps.search_brace = btype_2; 16335500Sbostic break; 1648805Smckusick 16535500Sbostic case semicolon: /* this indicates a simple stmt */ 16635500Sbostic break_comma = false; /* turn off flag to break after commas in a 16735500Sbostic * declaration */ 16835500Sbostic ps.p_stack[++ps.tos] = stmt; 16935500Sbostic ps.il[ps.tos] = ps.ind_level; 17035500Sbostic break; 1718805Smckusick 17235500Sbostic default: /* this is an error */ 17335500Sbostic diag(1, "Unknown code to parser"); 17435500Sbostic return; 1758805Smckusick 1768805Smckusick 17724456Smckusick } /* end of switch */ 1788805Smckusick 17924456Smckusick reduce(); /* see if any reduction can be done */ 18035500Sbostic 1818805Smckusick #ifdef debug 18224456Smckusick for (i = 1; i <= ps.tos; ++i) 18324456Smckusick printf("(%d %d)", ps.p_stack[i], ps.il[i]); 18424456Smckusick printf("\n"); 1858805Smckusick #endif 18635500Sbostic 1878805Smckusick return; 1888805Smckusick } 18935500Sbostic 19035500Sbostic /* 19135500Sbostic * NAME: reduce 19235500Sbostic * 19335500Sbostic * FUNCTION: Implements the reduce part of the parsing algorithm 19435500Sbostic * 19536555Sbostic * ALGORITHM: The following reductions are done. Reductions are repeated 19636555Sbostic * until no more are possible. 19735500Sbostic * 19836555Sbostic * Old TOS New TOS 19936555Sbostic * <stmt> <stmt> <stmtl> 20036555Sbostic * <stmtl> <stmt> <stmtl> 20136555Sbostic * do <stmt> "dostmt" 20236555Sbostic * if <stmt> "ifstmt" 20336555Sbostic * switch <stmt> <stmt> 20436555Sbostic * decl <stmt> <stmt> 20536555Sbostic * "ifelse" <stmt> <stmt> 20636555Sbostic * for <stmt> <stmt> 20736555Sbostic * while <stmt> <stmt> 20836555Sbostic * "dostmt" while <stmt> 20935500Sbostic * 21036555Sbostic * On each reduction, ps.i_l_follow (the indentation for the following line) 21136555Sbostic * is set to the indentation level associated with the old TOS. 21235500Sbostic * 21335500Sbostic * PARAMETERS: None 21435500Sbostic * 21535500Sbostic * RETURNS: Nothing 21635500Sbostic * 21735500Sbostic * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos = 21835500Sbostic * 21935500Sbostic * CALLS: None 22035500Sbostic * 22136555Sbostic * CALLED BY: parse 22235500Sbostic * 22335500Sbostic * HISTORY: initial coding November 1976 D A Willcox of CAC 22435500Sbostic * 22536555Sbostic */ 2268805Smckusick /*----------------------------------------------*\ 22735500Sbostic | REDUCTION PHASE | 2288805Smckusick \*----------------------------------------------*/ 22935500Sbostic reduce() 23035500Sbostic { 2318805Smckusick 23224456Smckusick register int i; 2338805Smckusick 23435500Sbostic for (;;) { /* keep looping until there is nothing left to 23535500Sbostic * reduce */ 2368805Smckusick 23724456Smckusick switch (ps.p_stack[ps.tos]) { 2388805Smckusick 23935500Sbostic case stmt: 24035500Sbostic switch (ps.p_stack[ps.tos - 1]) { 2418805Smckusick 24235500Sbostic case stmt: 24335500Sbostic case stmtl: 24435500Sbostic /* stmtl stmt or stmt stmt */ 24535500Sbostic ps.p_stack[--ps.tos] = stmtl; 24635500Sbostic break; 2478805Smckusick 24835500Sbostic case dolit: /* <do> <stmt> */ 24935500Sbostic ps.p_stack[--ps.tos] = dohead; 25035500Sbostic ps.i_l_follow = ps.il[ps.tos]; 25135500Sbostic break; 2528805Smckusick 25335500Sbostic case ifstmt: 25435500Sbostic /* <if> <stmt> */ 25535500Sbostic ps.p_stack[--ps.tos] = ifhead; 25635500Sbostic for (i = ps.tos - 1; 25735500Sbostic ( 25835500Sbostic ps.p_stack[i] != stmt 25935500Sbostic && 26035500Sbostic ps.p_stack[i] != stmtl 26135500Sbostic && 26235500Sbostic ps.p_stack[i] != lbrace 26335500Sbostic ); 26435500Sbostic --i); 26535500Sbostic ps.i_l_follow = ps.il[i]; 26635500Sbostic /* 26735500Sbostic * for the time being, we will assume that there is no else on 26835500Sbostic * this if, and set the indentation level accordingly. If an 26935500Sbostic * else is scanned, it will be fixed up later 27035500Sbostic */ 27135500Sbostic break; 2728805Smckusick 27335500Sbostic case swstmt: 27435500Sbostic /* <switch> <stmt> */ 27535500Sbostic case_ind = ps.cstk[ps.tos - 1]; 2768805Smckusick 27735500Sbostic case decl: /* finish of a declaration */ 27835500Sbostic case elsehead: 27935500Sbostic /* <<if> <stmt> else> <stmt> */ 28035500Sbostic case forstmt: 28135500Sbostic /* <for> <stmt> */ 28235500Sbostic case whilestmt: 28335500Sbostic /* <while> <stmt> */ 28435500Sbostic ps.p_stack[--ps.tos] = stmt; 28535500Sbostic ps.i_l_follow = ps.il[ps.tos]; 28635500Sbostic break; 2878805Smckusick 28835500Sbostic default: /* <anything else> <stmt> */ 28935500Sbostic return; 2908805Smckusick 29135500Sbostic } /* end of section for <stmt> on top of stack */ 29235500Sbostic break; 29335500Sbostic 29435500Sbostic case whilestmt: /* while (...) on top */ 29535500Sbostic if (ps.p_stack[ps.tos - 1] == dohead) { 29635500Sbostic /* it is termination of a do while */ 29735500Sbostic ps.p_stack[--ps.tos] = stmt; 2988805Smckusick break; 29935500Sbostic } 30035500Sbostic else 30135500Sbostic return; 3028805Smckusick 30335500Sbostic default: /* anything else on top */ 30435500Sbostic return; 3058805Smckusick 30624456Smckusick } 30724456Smckusick } 3088805Smckusick } 309