home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / cpm68k / utils.lbr / PARSE.CQ / PARSE.C
Encoding:
Text File  |  1986-05-22  |  9.5 KB  |  406 lines

  1. #
  2. /*
  3.  
  4.               Copyright (C) 1976
  5.                 by the
  6.               Board of Trustees
  7.                 of the
  8.             University of Illinois
  9.  
  10.              All rights reserved
  11.  
  12.  
  13. FILE NAME:
  14.     parse.c
  15.  
  16. PURPOSE:
  17.     Contains the routines which keep track of the parse stack.
  18.  
  19. GLOBALS:
  20.     p_stack =    The parse stack, set by both routines
  21.     il =        Stack of indentation levels, set by parse
  22.     cstk =        Stack of case statement indentation levels, set by parse
  23.     tos =        Pointer to top of stack, set by both routines.
  24.  
  25. FUNCTIONS:
  26.     parse
  27.     reduce
  28. */
  29. /*
  30.  
  31.               Copyright (C) 1976
  32.                 by the
  33.               Board of Trustees
  34.                 of the
  35.             University of Illinois
  36.  
  37.              All rights reserved
  38.  
  39.  
  40. NAME:
  41.     parse
  42.  
  43. FUNCTION:
  44.     Parse is given one input which is a "maxi token" just scanned from
  45.     input.  Maxi tokens are signifigant constructs such as else, {, do,
  46.     if (...), etc.  Parse works with reduce to maintain a parse stack
  47.     of these constructs.  Parse is responsible for the "shift" portion
  48.     of the parse algorithm, and reduce handles the "reduce" portion.
  49.  
  50. ALGORITHM:
  51.     1) If there is "ifstmt" on the stack and input is anything other than
  52.        an else, then change the top of stack (TOS) to <stmt>.  Do a reduce.
  53.     2) Use a switch statement to implement the following shift operations:
  54.  
  55.        TOS___        Input_____        Stack_____        Note____
  56.     decl        decl        nothing
  57.     anything else    decl        decl
  58.     "dostmt"    while (..)            Change TOS to <stmt>
  59.     anything else    while (..)    while
  60.     "ifstmt"    else                Change TOS to "ifelse"
  61.     { <stmtl>    }                Change { <stmtl> 
  62.                                 to <stmtl>
  63.             switch (..)    switch
  64.             do        do
  65.             for(..)        for
  66.             ;        <stmt>
  67.             {        { <stmt>
  68.  
  69. PARAMETERS:
  70.     tk    An integer code for the maxi token scanned
  71.  
  72. RETURNS:
  73.     Nothing
  74.  
  75. GLOBALS:
  76.     break_comma =    Set to true when in a declaration but not initialization
  77.     btype_2
  78.     case_ind =
  79.     cstk =
  80.     i_l_follow =
  81.     il =        Stack of indentation levels
  82.     ind_level =
  83.     p_stack =    Stack of token codes
  84.     search_brace =    Set to true if we must look for possibility of moving a
  85.             brace
  86.     tos =        Pointer to top of p_stack, il, and cstk
  87.  
  88. CALLS:
  89.     printf (lib)
  90.     reduce
  91.  
  92. CALLED BY:
  93.     main
  94.  
  95. HISTORY:
  96.     initial coding     November 1976    D A Willcox of CAC
  97.  
  98. */
  99.  
  100. #include "indntglo.h";
  101. #include "indntcod.h";
  102.  
  103.  
  104. int     p_stack[50] = stmt;
  105.  /* this is the parser's stack */
  106. int     il[50];    /* this stack stores indentation levels */
  107. int     cstk[50];  /* used to store case stmt indentation levels */
  108. int     tos = 0;   /* pointer to top of stack */
  109.  
  110.  
  111. parse (tk)
  112. int     tk;       /* the code for the construct scanned */
  113. {
  114.     int     i;
  115.  
  116. #ifdef debug
  117.     printf ("%2d - %s\n", tk, token);
  118. #endif
  119.     while (p_stack[tos] == ifhead && tk != elselit) {
  120.     /* true if we have an if without an else */
  121.     p_stack[tos] = stmt;   /* apply the if(..) stmt ::= stmt reduction */
  122.     reduce ();           /* see if this allows any reduction */
  123.     }
  124.  
  125.  
  126.     switch (tk) {           /* go on and figure out what to do with the
  127.                       input */
  128.  
  129.     case decl:            /* scanned a declaration word */
  130.         search_brace = btype_2;
  131.     /* indicate that following brace should be on same line */
  132.         if (p_stack[tos] != decl) {
  133.         /* only put one declaration onto stack */
  134.         break_comma = true;
  135.         /* while in declaration, newline should be forced after comma */
  136.         p_stack[++tos] = decl;
  137.         il[tos] = i_l_follow;
  138.  
  139.         if (ljust_decl) {
  140.         /* only do if we want left justified declarations */
  141.             ind_level = 0;
  142.             for (i = tos - 1; i > 0; --i)
  143.             if (p_stack[i] == decl)
  144.                 ++ind_level;
  145.         /* indentation is number of declaration levels deep we are */
  146.             i_l_follow = ind_level;
  147.         }
  148.         }
  149.         break;
  150.  
  151.     case ifstmt:            /* scanned if (...) */
  152.     case dolit:            /* 'do' */
  153.     case forstmt:            /* for (...) */
  154.         p_stack[++tos] = tk;
  155.         il[tos] = ind_level = i_l_follow;
  156.         ++i_l_follow;      /* subsequent statements should be indented 1 */
  157.         search_brace = btype_2;
  158.         break;
  159.  
  160.     case lbrace:            /* scanned { */
  161.         break_comma = false;
  162.     /* don't break comma in an initial list */
  163.         if (p_stack[tos] == stmt || p_stack[tos] == decl
  164.                      || p_stack[tos] == stmtl)
  165.         ++i_l_follow;  /* it is a random, isolated stmt group or a
  166.                       declaration */
  167.         else {
  168.         if (s_code == e_code) {
  169.         /* only do this if there is nothing on the line */
  170.             --ind_level;
  171.         /* it is a group as part of a while, for, etc. */
  172.             if (p_stack[tos] == swstmt)
  173.             --ind_level;
  174.         /* for a switch, brace should be two levels out from the code 
  175.         */
  176.         }
  177.         }
  178.  
  179.         p_stack[++tos] = lbrace;
  180.         il[tos] = ind_level;
  181.         p_stack[++tos] = stmt;
  182.     /* allow null stmt between braces */
  183.         il[tos] = i_l_follow;
  184.         break;
  185.  
  186.     case whilestmt:        /* scanned while (...) */
  187.         if (p_stack[tos] == dohead) {
  188.         /* it is matched with do stmt */
  189.         ind_level = i_l_follow = il[tos];
  190.         p_stack[++tos] = whilestmt;
  191.         il[tos] = ind_level = i_l_follow;
  192.         }
  193.         else {           /* it is a while loop */
  194.         p_stack[++tos] = whilestmt;
  195.         il[tos] = i_l_follow;
  196.         ++i_l_follow;
  197.         search_brace = btype_2;
  198.         }
  199.  
  200.         break;
  201.  
  202.     case elselit:            /* scanned an else */
  203.  
  204.         if (p_stack[tos] != ifhead) {
  205.         printf ("%d: Unmatched else\n", line_no);
  206.         }
  207.         else {
  208.         ind_level = il[tos];
  209.         /* indentation for else should be same as for if */
  210.         i_l_follow = ind_level + 1;
  211.         /* everything following should be in 1 level */
  212.         p_stack[tos] = elsehead;
  213.         /* remember if with else */
  214.         search_brace = btype_2;
  215.         }
  216.  
  217.         break;
  218.  
  219.     case rbrace:            /* scanned a } */
  220.     /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
  221.         if (p_stack[tos - 1] == lbrace) {
  222.         ind_level = i_l_follow = il[--tos];
  223.         p_stack[tos] = stmt;
  224.         }
  225.         else {
  226.         printf ("%d: Stmt nesting error\n", line_no);
  227.         }
  228.  
  229.         break;
  230.  
  231.     case swstmt:            /* had switch (...) */
  232.         p_stack[++tos] = swstmt;
  233.         cstk[tos] = case_ind;
  234.     /* save current case indent level */
  235.         il[tos] = i_l_follow;
  236.         case_ind = i_l_follow + 1;
  237.     /* cases should be one level down from switch */
  238.         i_l_follow += 2;  /* statements should be two levels in */
  239.         search_brace = btype_2;
  240.         break;
  241.  
  242.     case semicolon:        /* this indicates a simple stmt */
  243.         break_comma = false;
  244.     /* turn off flag to break after commas in a declaration */
  245.         p_stack[++tos] = stmt;
  246.         il[tos] = ind_level;
  247.         break;
  248.  
  249.     default:            /* this is an error */
  250.         printf ("%d: Unknown code to parser - %d\n", line_no, tk);
  251.         return;
  252.  
  253.  
  254.     }                   /* end of switch */
  255.  
  256.     reduce ();               /* see if any reduction can be done */
  257. #ifdef debug
  258.     for (i = 1; i <= tos; ++i)
  259.     printf ("(%d %d)", p_stack[i], il[i]);
  260.     printf ("\n");
  261. #endif
  262.     return;
  263. }
  264. /*
  265.  
  266.               Copyright (C) 1976
  267.                 by the
  268.               Board of Trustees
  269.                 of the
  270.             University of Illinois
  271.  
  272.              All rights reserved
  273.  
  274.  
  275. NAME:
  276.     reduce
  277.  
  278. FUNCTION:
  279.     Implements the reduce part of the parsing algorithm
  280.  
  281. ALGORITHM:
  282.     The following reductions are done.  Reductions are repeated until no
  283.     more are possible.
  284.  
  285.     Old___ TOS___        New___ TOS___
  286.     <stmt> <stmt>    <stmtl>
  287.     <stmtl> <stmt>    <stmtl>
  288.     do <stmt>    "dostmt"
  289.     if <stmt>    "ifstmt"
  290.     switch <stmt>    <stmt>
  291.     decl <stmt>    <stmt>
  292.     "ifelse" <stmt>    <stmt>
  293.     for <stmt>    <stmt>
  294.     while <stmt>    <stmt>
  295.     "dostmt" while    <stmt>
  296.  
  297.     On each reduction, i_l_follow (the indentation for the following line)
  298.     is set to the indentation level associated with the old TOS.
  299.  
  300. PARAMETERS:
  301.     None
  302.  
  303. RETURNS:
  304.     Nothing
  305.  
  306. GLOBALS:
  307.     cstk
  308.     i_l_follow =
  309.     il
  310.     p_stack =
  311.     tos =
  312.  
  313. CALLS:
  314.     None
  315.  
  316. CALLED BY:
  317.     parse
  318. 
  319. HISTORY:
  320.     initial coding     November 1976    D A Willcox of CAC
  321.  
  322. */
  323. /*----------------------------------------------*\
  324. |   REDUCTION PHASE
  325. \*----------------------------------------------*/
  326. reduce () {
  327.  
  328.     register int    i;
  329.  /* local looping variable */
  330.  
  331.     for (;;) {               /* keep looping until there is nothing left to
  332.                       reduce */
  333.  
  334.     switch (p_stack[tos]) {
  335.  
  336.         case stmt: 
  337.         switch (p_stack[tos - 1]) {
  338.  
  339.             case stmt: 
  340.             case stmtl: 
  341.             /* stmtl stmt or stmt stmt */
  342.             p_stack[--tos] = stmtl;
  343.             break;
  344.  
  345.             case dolit: 
  346.             /* <do> <stmt> */
  347.             p_stack[--tos] = dohead;
  348.             i_l_follow = il[tos];
  349.             break;
  350.  
  351.             case ifstmt: 
  352.             /* <if> <stmt> */
  353.             p_stack[--tos] = ifhead;
  354.             for (i = tos - 1;
  355.                 (
  356.                     p_stack[i] != stmt
  357.                     &&
  358.                     p_stack[i] != stmtl
  359.                     &&
  360.                     p_stack[i] != lbrace
  361.                 );
  362.                 --i);
  363.             i_l_follow = il[i];
  364.             /* for the time being, we will assume that there is no else
  365.                on this if, and set the indentation level accordingly.
  366.                If an else is scanned, it will be fixed up later */
  367.             break;
  368.  
  369.             case swstmt: 
  370.             /* <switch> <stmt> */
  371.             case_ind = cstk[tos - 1];
  372.  
  373.             case decl: /* finish of a declaration */
  374.             case elsehead: 
  375.             /* <<if> <stmt> else> <stmt> */
  376.             case forstmt: 
  377.             /* <for> <stmt> */
  378.             case whilestmt: 
  379.             /* <while> <stmt> */
  380.             p_stack[--tos] = stmt;
  381.             i_l_follow = il[tos];
  382.             break;
  383.  
  384.             default:   /* <anything else> <stmt> */
  385.             return;
  386.  
  387.         }           /* end of section for <stmt> on top of stack */
  388.         break;
  389.  
  390.         case whilestmt:    /* while (...) on top */
  391.         if (p_stack[tos - 1] == dohead) {
  392.         /* it is termination of a do while */
  393.             p_stack[--tos] = stmt;
  394.             break;
  395.         }
  396.         else
  397.             return;
  398.  
  399.         default:            /* anything else on top */
  400.         return;
  401.  
  402.     }               /* end of big switch */
  403.  
  404.     }                   /* end of reduction phase for (;;) */
  405. }
  406.