home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / tools / calc.src < prev    next >
Encoding:
Text File  |  1988-05-03  |  25.4 KB  |  941 lines

  1. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2. --auxlex.src
  3. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  4.   package AUXLEX_PKG is
  5.  
  6.    function UPPERCASE ( C: CHARACTER ) return CHARACTER;
  7.    function SPACEP ( C : CHARACTER ) return BOOLEAN; 
  8.    function DIGITP ( C : CHARACTER ) return BOOLEAN; 
  9.    function LETTERP ( C : CHARACTER ) return BOOLEAN; 
  10.  
  11.   end AUXLEX_PKG;
  12.  
  13.   package body AUXLEX_PKG is
  14.  
  15. -- The following are auxilliary procedures used by LRLEX
  16.  
  17.    function UPPERCASE ( C: CHARACTER ) return CHARACTER is
  18.  
  19.    -- convert input 'a..z' to 'A..Z'.  All other input returned as is.
  20.  
  21.    CHAR_POS    : INTEGER;
  22.    CASE_OFFSET : CONSTANT INTEGER := 32;  
  23.    -- assuming ASCII character set
  24.  
  25.    begin
  26.   
  27.      CHAR_POS := CHARACTER'POS ( C );
  28.  
  29.      if ( C >= 'a' ) and ( C <= 'z' ) then 
  30.        return CHARACTER'VAL ( CHAR_POS - CASE_OFFSET );
  31.      else
  32.        return C;
  33.      end if;
  34.  
  35.    end UPPERCASE;
  36.  
  37.  
  38. --   procedure GETC ( INPTR : in out INTEGER; C : out CHARACTER ) is
  39.  
  40.    -- This procedure gets an input character if neither the end_of_line 
  41.    -- nor the end_of_file has been reached.
  42.  
  43. --   MAXLINLEN : INTEGER := 80;
  44. --   EOLC       : CHARACTER := CHARACTER'VAL ( 1 );
  45. --   EOFC       : CHARACTER := CHARACTER'VAL ( 2 );
  46. --   INBUF     : array ( 1..Maxlinlen ) of CHARACTER;
  47.  
  48.  
  49. --   begin
  50.  
  51. --   end GETC;
  52.  
  53.  
  54.    function SPACEP ( C : CHARACTER ) return BOOLEAN is
  55.  
  56.       RESULT : BOOLEAN;
  57.  
  58.    begin
  59.  
  60.      RESULT := C = ' ';  
  61.  
  62.      return RESULT;
  63.  
  64.    end SPACEP;
  65.  
  66.  
  67.  
  68.    function DIGITP ( C : CHARACTER ) return BOOLEAN is
  69.  
  70.     RESULT : BOOLEAN;
  71.  
  72.    BEGIN
  73.  
  74.      RESULT := ( C >= '0' and C <= '9'  );
  75.  
  76.      return RESULT;
  77.  
  78.    end DIGITP;
  79.  
  80.  
  81.  
  82.    function LETTERP ( C : CHARACTER ) return BOOLEAN is
  83.  
  84.      RESULT : BOOLEAN;
  85.  
  86.    begin
  87.  
  88.      RESULT := ( C >= 'a' and C <= 'z' ) or ( C >= 'A' and C <= 'Z' );
  89.  
  90.      return RESULT;
  91.  
  92.    end LETTERP;
  93.  
  94.   begin -- package body
  95.  
  96.   --  null program
  97.   null;
  98.   end AUXLEX_PKG;
  99.  
  100.  
  101.  
  102. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  103. --tok.src
  104. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  105. package TOK_PKG is
  106. type TOK is ( 
  107. DIGIT                           , 
  108. DIV_OP                          , 
  109. ENDT                            , 
  110. EOL                             , 
  111. EQUALS                          , 
  112. ERROR                           , 
  113. LEFT_PAREN                      , 
  114. LETTER                          , 
  115. MINUS_OP                        , 
  116. PLUS_OP                         , 
  117. RIGHT_PAREN                     , 
  118. TIMES_OP                        
  119.  );
  120. end TOK_PKG;
  121. package body TOK_PKG is
  122.   begin
  123.     null;
  124. end TOK_PKG;
  125.  
  126.  
  127.  
  128. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  129. --lex.src
  130. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  131. with AUXLEX_PKG; use AUXLEX_PKG;
  132. with TOK_PKG; use TOK_PKG;
  133. with TEXT_IO; use TEXT_IO;
  134.  
  135. package LEX_PKG is
  136.   procedure LRLEX ( CURTOK : out TOK ; LEXVAL : out INTEGER );
  137.   function LRERR  return INTEGER; 
  138. end LEX_PKG;
  139.  
  140.  
  141.   -- This package was developed specifically for the CALC_PROG program.
  142.   -- The syntactic parser (PARSER_PKG) was generated by the LR parser generator
  143.   -- program (written in FORTRAN) on the VAX.  LR also generates a list of
  144.   -- tokens the parser looks for; these are assembled in a type called TOK and
  145.   -- placed in an Ada package called TOK_PKG.  The lexical scanner/analyzer for
  146.   -- any given grammar can be hand-coded on the Wicat (in Ada) for that
  147.   -- particular language.  LRLEX uses another package, AUXLEX_PKG, that 
  148.   -- contains a number of low-level auxiliary routines that are useful in
  149.   -- any lexical scanning task.  We expect to add additional functions and
  150.   -- procedures to AUXLEX_PKG as time goes on.  Note that this package also 
  151.   -- uses TOK_PKG, since it is LRLEX that identifies legal tokens in the
  152.   -- input stream.  LRLEX also uses the package, TOK_PKG, generated by LR, 
  153.   -- since the token number is the means of communication between LRLEX and
  154.   -- LRPARSE.
  155.   
  156.   package body LEX_PKG is
  157.  
  158. procedure LRLEX ( CURTOK : out TOK ; LEXVAL : out INTEGER ) is
  159.  
  160. -- NOTE... LRLEX IS NOW A PROCEDURE, NOT A FUNCTION, SINCE IT
  161. -- HAS OUTPUT PARAMETERS.
  162.  
  163.  
  164. C   : CHARACTER;
  165.  
  166. EOLC : CHARACTER := ';'; -- delimiter for the end of an input line.
  167. EOFC : CHARACTER := CHARACTER'VAL ( 2 );  -- Ada version of a CTRL B.
  168.                                           -- This is the end-of-file
  169.                                           -- character that terminates
  170.                                           -- the program. 
  171. INPTR : INTEGER := 0;  -- input buffer pointer
  172.  
  173.    procedure GETC ( INPTR : in out INTEGER; C : out CHARACTER ) is
  174.  
  175.    -- This procedure gets an input character if the end_of_line 
  176.    -- has not been reached.
  177.  
  178.    MAXLINLEN : INTEGER := 80;
  179.    EOLC       : CHARACTER := ';';
  180.    INBUF     : array ( 1..MAXLINLEN ) of CHARACTER;
  181.  
  182.  
  183.    begin
  184.  
  185.      if (INPTR > MAXLINLEN) then
  186.        C := EOLC;
  187.        INPTR := 0;
  188.      else
  189.           GET ( C );
  190.        INPTR := INPTR + 1;
  191.      end if;
  192.  
  193.    end GETC;
  194.  
  195.  -- *********************** NOW THE MAIN BODY OF LRLEX **********************
  196.  
  197.  
  198.  begin   -- LRLEX
  199.  
  200.    
  201.  
  202.      GETC (INPTR, C);
  203.  
  204.      while (SPACEP (C)) loop
  205.         GETC (INPTR, C);
  206.      end LOOP;
  207.  
  208.         LEXVAL := 0;
  209.  
  210.      if (DIGITP (C)) then
  211.        LEXVAL := CHARACTER'POS ( C ) - 48;
  212.        CURTOK := DIGIT;
  213.      elsif (LETTERP (C)) then
  214.        LEXVAL := CHARACTER'POS ( UPPERCASE ( C ) ) - 64 ;  
  215.        CURTOK := LETTER;
  216.      elsif (C = '+') then
  217.        CURTOK := PLUS_OP;
  218.      elsif (C = '-') then
  219.        CURTOK := MINUS_OP;
  220.      elsif (C = '*') then
  221.        CURTOK := TIMES_OP;
  222.         elsif (C = '/') then
  223.        CURTOK := DIV_OP;
  224.         elsif (C = '=') then
  225.        CURTOK := EQUALS;
  226.      elsif (C = '(') then
  227.        CURTOK := LEFT_PAREN;
  228.      elsif (C = ')') then
  229.        CURTOK := RIGHT_PAREN;
  230.      elsif (C = EOLC) then
  231.        CURTOK := EOL;
  232.      elsif (C = EOFC) then
  233.        CURTOK := ENDT;
  234.      else
  235.        CURTOK := ERROR;
  236.      end if;
  237.  
  238.  end LRLEX;
  239.   
  240.  
  241.  function LRERR return INTEGER is
  242.  
  243.   begin
  244.  
  245.   -- user can put custom error processing here, such as printing out
  246.   -- the offending input line, etc.
  247.  
  248.   return 0;
  249.  
  250.   end LRERR;
  251.  
  252. begin -- body of LEX_PKG
  253.   null;
  254. end LEX_PKG;
  255.  
  256. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  257. --decl.src
  258. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  259.   package DECL_PKG is
  260.   
  261.     REG : array (1..26) of INTEGER := (1..REG'LAST => 0);
  262.     
  263.       -- This array is used in the semantic routines of the CALC program
  264.       -- parser to store values in single-letter-named variables that the
  265.       -- user defines.
  266.       
  267.     TEMP : INTEGER;  -- Temporary variable used in semantic routines.
  268.     
  269.   end DECL_PKG;
  270.   
  271.   package body DECL_PKG is
  272.   
  273.     begin
  274.   
  275.     null;  -- this package exists only to provide variable declarations used
  276.            -- by the semantic routines in package PARSER_PKG.
  277.            
  278.     end DECL_PKG;
  279.       
  280.  
  281.  
  282. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  283. --parser.src
  284. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  285.  with DECL_PKG; use DECL_PKG;
  286.  with TEXT_IO;  use TEXT_IO;
  287.  with LEX_PKG;  use LEX_PKG;
  288.  with TOK_PKG;  use TOK_PKG;
  289.  package PARSER_PKG is
  290.     function LRPARSE return integer;
  291.  end PARSER_PKG;
  292.  package body PARSER_PKG is
  293.    function LRPARSE return integer is
  294.  
  295.          --  Start of %INCLUDE lrparse.dim                             
  296.  
  297.      type LRENT1 is array( 1 ..   31) of integer;
  298.      type LRFRED1 is array( 1 ..   32) of integer;
  299.      type LRFTRN1 is array( 1 ..   32) of integer;
  300.      type LRTRAN1 is array( 1 ..   78) of integer;
  301.      type LRNSET1 is array( 1 ..   20) of integer;
  302.      type LRPROD1 is array( 1 ..   20) of integer;
  303.      type LRLSET1 is array( 1 ..    9) of integer;
  304.      type LRLS1 is array( 1 ..   36) of integer;
  305.      type LRLEN1 is array( 1 ..   19) of integer;
  306.      type LRLHS1 is array( 1 ..   19) of integer;
  307.  
  308.          --  End of %INCLUDE from lrparse.dim                             
  309.  
  310.  
  311.          --  Start of %INCLUDE lrparse.dat                             
  312.  
  313.      LRENT : LRENT1 := (
  314.                     3,
  315.                     3,
  316.                    19,
  317.                     1,
  318.                     3,
  319.                     6,
  320.                     7,
  321.                     8,
  322.                     9,
  323.                    15,
  324.                    16,
  325.                    17,
  326.                    18,
  327.                    20,
  328.                     8,
  329.                    15,
  330.                     5,
  331.                    16,
  332.                     9,
  333.                    10,
  334.                     1,
  335.                    14,
  336.                     2,
  337.                    12,
  338.                    11,
  339.                    15,
  340.                    20,
  341.                    20,
  342.                     4,
  343.                    16,
  344.                    16 );
  345.      LRFRED : LRFRED1 := (
  346.                     1,
  347.                     1,
  348.                     2,
  349.                     2,
  350.                     3,
  351.                     4,
  352.                     5,
  353.                     5,
  354.                     6,
  355.                     6,
  356.                     7,
  357.                     8,
  358.                     9,
  359.                    10,
  360.                    11,
  361.                    12,
  362.                    12,
  363.                    12,
  364.                    13,
  365.                    13,
  366.                    13,
  367.                    14,
  368.                    14,
  369.                    14,
  370.                    14,
  371.                    15,
  372.                    16,
  373.                    17,
  374.                    18,
  375.                    19,
  376.                    20,
  377.                    21 );
  378.      LRFTRN : LRFTRN1 := (
  379.                     1,
  380.                     2,
  381.                     3,
  382.                    14,
  383.                    14,
  384.                    14,
  385.                    14,
  386.                    22,
  387.                    23,
  388.                    29,
  389.                    31,
  390.                    31,
  391.                    32,
  392.                    33,
  393.                    35,
  394.                    35,
  395.                    38,
  396.                    46,
  397.                    46,
  398.                    53,
  399.                    60,
  400.                    60,
  401.                    61,
  402.                    67,
  403.                    73,
  404.                    73,
  405.                    75,
  406.                    77,
  407.                    79,
  408.                    79,
  409.                    79,
  410.                    79 );
  411.      LRTRAN : LRTRAN1 := (
  412.                     2,
  413.                     3,
  414.                     4,
  415.                     5,
  416.                     6,
  417.                     7,
  418.                     8,
  419.                     9,
  420.                    10,
  421.                    11,
  422.                    12,
  423.                    13,
  424.                    14,
  425.                     4,
  426.                     7,
  427.                    15,
  428.                     9,
  429.                    16,
  430.                    11,
  431.                    12,
  432.                    14,
  433.                    17,
  434.                     4,
  435.                     7,
  436.                    15,
  437.                     9,
  438.                    18,
  439.                    12,
  440.                    19,
  441.                    20,
  442.                    21,
  443.                    22,
  444.                    23,
  445.                    24,
  446.                    19,
  447.                    20,
  448.                    25,
  449.                     4,
  450.                     7,
  451.                    15,
  452.                     9,
  453.                    26,
  454.                    11,
  455.                    12,
  456.                    14,
  457.                     4,
  458.                     7,
  459.                    15,
  460.                     9,
  461.                    11,
  462.                    12,
  463.                    27,
  464.                     4,
  465.                     7,
  466.                    15,
  467.                     9,
  468.                    11,
  469.                    12,
  470.                    28,
  471.                    29,
  472.                     4,
  473.                     7,
  474.                    15,
  475.                     9,
  476.                    30,
  477.                    12,
  478.                     4,
  479.                     7,
  480.                    15,
  481.                     9,
  482.                    31,
  483.                    12,
  484.                    19,
  485.                    20,
  486.                    23,
  487.                    24,
  488.                    23,
  489.                    24 );
  490.      LRNSET : LRNSET1 := (
  491.                     3,
  492.                     1,
  493.                     6,
  494.                     8,
  495.                     5,
  496.                     8,
  497.                     4,
  498.                     4,
  499.                     8,
  500.                     7,
  501.                     4,
  502.                     4,
  503.                     1,
  504.                     4,
  505.                     8,
  506.                     7,
  507.                     7,
  508.                     3,
  509.                     4,
  510.                     4 );
  511.      LRPROD : LRPROD1 := (
  512.                     2,
  513.                    18,
  514.                     1,
  515.                     7,
  516.                    15,
  517.                     5,
  518.                    11,
  519.                    14,
  520.                     4,
  521.                     8,
  522.                    15,
  523.                    17,
  524.                    19,
  525.                    16,
  526.                     6,
  527.                    10,
  528.                     9,
  529.                     3,
  530.                    13,
  531.                    12 );
  532.      LRLS : LRLS1 := (
  533.                     1,
  534.                     2,
  535.                     4,
  536.                     9,
  537.                    10,
  538.                    11,
  539.                    12,
  540.                     1,
  541.                     2,
  542.                     4,
  543.                     9,
  544.                    10,
  545.                    12,
  546.                     1,
  547.                     3,
  548.                     6,
  549.                     7,
  550.                     8,
  551.                     9,
  552.                     2,
  553.                     4,
  554.                     9,
  555.                    10,
  556.                    11,
  557.                    12,
  558.                     2,
  559.                     4,
  560.                     9,
  561.                    10,
  562.                    12,
  563.                     3,
  564.                     4,
  565.                     9,
  566.                    10,
  567.                    11,
  568.                     4 );
  569.      LRLSET : LRLSET1 := (
  570.                     1,
  571.                     8,
  572.                    14,
  573.                    20,
  574.                    26,
  575.                    31,
  576.                    32,
  577.                    36,
  578.                    37 );
  579.      LRLEN : LRLEN1 := (
  580.                     3,
  581.                     0,
  582.                     4,
  583.                     0,
  584.                     1,
  585.                     3,
  586.                     1,
  587.                     1,
  588.                     3,
  589.                     3,
  590.                     1,
  591.                     3,
  592.                     3,
  593.                     1,
  594.                     1,
  595.                     3,
  596.                     2,
  597.                     1,
  598.                     2 );
  599.      LRLHS : LRLHS1 := (
  600.                    13,
  601.                    19,
  602.                    19,
  603.                    14,
  604.                    18,
  605.                    18,
  606.                    18,
  607.                    15,
  608.                    15,
  609.                    15,
  610.                    20,
  611.                    20,
  612.                    20,
  613.                    16,
  614.                    16,
  615.                    16,
  616.                    16,
  617.                    17,
  618.                    17 );
  619.      LRIFINAL : integer :=   5;
  620.      LRIENDTK : integer :=   3;
  621.      LRIERRTK : integer :=   6;
  622.  
  623.          --  End of %INCLUDE from lrparse.dat                             
  624.  
  625.  
  626.          --  Start of %INCLUDE sia0:[tools.lrada]other.ada             
  627.  
  628.      LRSTASTK : array (1..100) of integer;
  629.      LRTOKSTK : array (1..100) of integer;
  630.      LRNOWSTA : integer;
  631.      LRCURTOK : integer;
  632.      LRLEXVAL : integer;
  633.      LRMAXSTK : integer;
  634.      LRERRFLG : integer;
  635.      LRS      : array (1..101) of integer;
  636.  
  637.      LRP      : integer;                           --  ###########?????
  638.      
  639.  
  640.          --  End of %INCLUDE from sia0:[tools.lrada]other.ada             
  641.  
  642.      procedure LRACTN_A( LRPROD : in integer ) is
  643.        begin
  644.          case LRPROD is
  645.          when     1 =>  null;
  646.          when     2 =>  null;
  647.          when     3 =>  null;
  648.                         NEW_LINE;
  649.                         PUT( "*" );   -- prompt for inPUT
  650.          when     4 =>  null;
  651.                         NEW_LINE;
  652.                         PUT( "Result is " );
  653.                         TEMP := LRS( LRP + 1);
  654.                         INTEGER_IO.PUT( TEMP );
  655.          when     5 =>  null;
  656.                         REG( LRS( LRP + 1) ):= LRS( LRP + 3);
  657.          when     6 =>  null;
  658.                         NEW_LINE;
  659.                         PUT( "? illegal statement" );
  660.          when     7 =>  null;
  661.                         LRS( LRP + 1) := LRS( LRP + 1);
  662.          when     8 =>  null;
  663.                         LRS( LRP + 1) := LRS( LRP + 1) + LRS( LRP + 3);
  664.          when     9 =>  null;
  665.                         LRS( LRP + 1) := LRS( LRP + 1) - LRS( LRP + 3);
  666.          when    10 =>  null;
  667.                         LRS( LRP + 1) := LRS( LRP + 1);
  668.          when OTHERS => NEW_LINE;
  669.                         PUT( "Bad production number in LRACTN_A" );
  670.                         INTEGER_IO.PUT( LRPROD );
  671.        end case;
  672.    end LRACTN_A;
  673.    procedure LRACTN_B( LRPROD : in integer ) is
  674.      begin
  675.        case LRPROD is
  676.          when    11 =>  null;
  677.                         LRS( LRP + 1) := LRS( LRP + 1) * LRS( LRP + 3);
  678.          when    12 =>  null;
  679.                         LRS( LRP + 1) := LRS( LRP + 1) / LRS( LRP + 3);
  680.          when    13 =>  null;
  681.                         LRS( LRP + 1) := LRS( LRP + 1);
  682.          when    14 =>  null;
  683.                         LRS( LRP + 1) := REG( LRS( LRP + 1) );
  684.          when    15 =>  null;
  685.                         LRS( LRP + 1) := LRS( LRP + 2);
  686.          when    16 =>  null;
  687.                         LRS( LRP + 1) := -LRS( LRP + 2);
  688.          when    17 =>  null;
  689.                         LRS( LRP + 1) := LRS( LRP + 1);
  690.          when    18 =>  null;
  691.                         LRS( LRP + 1) := 10 * LRS( LRP + 1) + LRS( LRP + 2);
  692.          when OTHERS => NEW_LINE;
  693.                         PUT( "Bad production number in LRACTN_B" );
  694.                         INTEGER_IO.PUT( LRPROD );
  695.          end case;
  696.      end LRACTN_B;
  697.      procedure LRACTN( LRPROD : in integer ) is
  698.        begin
  699.          case LRPROD is
  700.            when    1..   10 =>  LRACTN_A( LRPROD );
  701.            when   11..   18 =>  LRACTN_B( LRPROD );
  702.            when   OTHERS    =>  NEW_LINE;
  703.                                 PUT( "Bad production number in LRACTN");
  704.                                 INTEGER_IO.PUT( LRPROD );
  705.          end case;
  706.      end LRACTN;
  707.  
  708.          --  Start of %INCLUDE sia0:[tools.lrada]lraux.ada             
  709.  
  710.     procedure LRINIT is
  711.       begin
  712.         LRERRFLG := 0;
  713.         LRMAXSTK := 100;
  714.         LRNOWSTA := 1;
  715.         LRSTASTK( 1 ) := 1;
  716.         LRP := 1;
  717.         LRCURTOK := LRIENDTK;
  718.         LRLEXVAL := 0;
  719.     end LRINIT;
  720.     function LRFINDR( ISTATE,
  721.                       ITOKEN : in integer ) return integer is
  722.       ISTART,
  723.       IEND,
  724.       JSTART,
  725.       JEND,
  726.       I,
  727.       J : integer;
  728.       begin
  729.         ISTART := LRFRED( ISTATE );
  730.         IEND := LRFRED( ISTATE + 1 ) - 1;
  731.         I := ISTART;
  732.         while I <= IEND loop
  733.           J := LRNSET( I );
  734.           JSTART := LRLSET( J );
  735.           JEND := LRLSET( J + 1 ) - 1;
  736.           J := JSTART;
  737.           while J <= JEND loop
  738.             if ( LRLS( J ) = ITOKEN ) then
  739.                return LRPROD( I );
  740.                I := IEND;
  741.                J := JEND;
  742.             end if;
  743.             J := J + 1;
  744.           end loop;
  745.           I := I + 1;
  746.         end loop;
  747.         return -1;
  748.     end LRFINDR;
  749.     function LRFINDT( ISS,
  750.                       IT : in integer ) return integer is
  751.       ISTART,
  752.       IEND,
  753.       I,
  754.       J : integer;
  755.       begin
  756.         ISTART := LRFTRN( ISS );
  757.         IEND := LRFTRN( ISS + 1 ) - 1;
  758.         I := ISTART;
  759.         while I <= IEND loop
  760.           J := LRTRAN( I );
  761.           if ( LRENT( J ) = IT ) then
  762.              return J;
  763.              I := IEND;
  764.           end if;
  765.           I := I + 1;
  766.         end loop;
  767.         return -1;
  768.     end LRFINDT;
  769.     procedure LRDOTR( ISTA : in integer ) is
  770.       begin
  771.         LRP := LRP + 1;
  772.         if ( LRP > LRMAXSTK ) then
  773.            LRERRFLG := 2;
  774.         else
  775.            LRTOKSTK( LRP ) := LRCURTOK;
  776.            LRSTASTK( LRP ) := ISTA;
  777.            LRS( LRP + 1 ) := LRLEXVAL;
  778.            LRNOWSTA := ISTA;
  779.         end if;
  780.     end LRDOTR;
  781.     procedure LRDORE( IPROD : in integer ) is
  782.       LEPTR : integer;
  783.       begin
  784.         LEPTR := LRP - LRLEN( IPROD ) + 1;
  785.         if ( LEPTR > LRMAXSTK ) then
  786.            LRERRFLG := 2;
  787.         else
  788.            LRTOKSTK( LEPTR ) := LRLHS( IPROD );
  789.            LRNOWSTA := LRFINDT( LRSTASTK( LEPTR - 1 ), LRLHS( IPROD ) );
  790.            LRSTASTK( LEPTR ) := LRNOWSTA;
  791.            LRP := LEPTR;
  792.            LRACTN( IPROD - 1 );
  793.         end if;
  794.     end LRDORE;
  795.     procedure LRERROR is
  796.       FOUND : boolean;
  797.       ISTA,
  798.       IPROD : integer;
  799.       TOK1  : TOK;
  800.       begin
  801.         if lrerr = 0 then
  802.            FOUND := false;
  803.             while ( not FOUND ) and ( LRP > 0 ) loop
  804.              ISTA := LRFINDT( LRSTASTK( LRP ), LRIERRTK );
  805.              if ISTA > 0 then
  806.                 LRDOTR( ISTA );
  807.                 FOUND := true;
  808.              else
  809.                 LRP := LRP - 1;
  810.              end if;
  811.              if not FOUND then
  812.                 LRERRFLG := 1;
  813.              else
  814.                 FOUND := false;
  815.                 while not FOUND loop
  816.                   IPROD := LRFINDR( LRNOWSTA, LRCURTOK );
  817.                   if IPROD > 0 then
  818.                      LRDORE( IPROD );
  819.                      FOUND := true;
  820.                   else
  821.                      ISTA := LRFINDT( LRNOWSTA, LRCURTOK );
  822.                      if ISTA > 0 then
  823.                         LRDOTR( ISTA );
  824.                         FOUND := true;
  825.                      elsif LRCURTOK = LRIENDTK then
  826.                            LRERRFLG := 3;
  827.                            FOUND := true;
  828.                      else
  829.                          -- NOTE THAT LRLEX IS NOW A PROCEDURE,
  830.                          -- NOT A FUNCTION, SINCE IT HAS AN OUTPUT PARAMETER.
  831.                          LRLEX( TOK1, LRLEXVAL );
  832.                          LRCURTOK := TOK'POS( TOK1 ) + 1;
  833.                      end if;
  834.                   end if;
  835.                 end loop;
  836.              end if;
  837.           end loop;
  838.        end if;
  839.     end LRERROR;
  840.     procedure LRINPRT is
  841.       DONE : boolean;
  842.       IPROD,
  843.       ISTA : integer;
  844.       TOK1 : TOK;
  845.       begin
  846.         DONE := false;
  847.          while ( LRERRFLG = 0 ) and ( not DONE ) loop
  848.           IPROD := LRFINDR( LRNOWSTA, LRCURTOK );
  849.           if ( IPROD > 0 ) then
  850.              LRDORE( IPROD );
  851.              if IPROD = 1 then
  852.                 DONE := true;
  853.              end if;
  854.           else
  855.              ISTA := LRFINDT( LRNOWSTA, LRCURTOK );
  856.              if ( ISTA > 0 ) then
  857.                 LRDOTR( ISTA );
  858.                 if ISTA = LRIFINAL then
  859.                    DONE := true;
  860.                 else
  861.                    -- NOTE THAT LRLEX IS NOW A PROCEDURE, NOT A FUNCTION,
  862.                    -- SINCE IT HAS AN OUTPUT PARAMETER.
  863.                    LRLEX( TOK1, LRLEXVAL );
  864.                    LRCURTOK := TOK'POS( TOK1 ) + 1;
  865.                 end if;
  866.              else
  867.                 LRERROR;
  868.              end if;
  869.           end if;
  870.         end loop;
  871.     end LRINPRT;
  872.  
  873.          --  End of %INCLUDE from sia0:[tools.lrada]lraux.ada             
  874.  
  875.      begin   -- main body of function LRPARSE
  876.        LRINIT;
  877.        LRINPRT;
  878.        return  LRERRFLG;
  879.      end LRPARSE;
  880.    begin
  881.      null;
  882.  end PARSER_PKG;
  883. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  884. --main.src
  885. --::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  886.  
  887. -- ******************************* MAIN PROCEDURE BODY *********************
  888.   with PARSER_PKG; use PARSER_PKG;
  889.   with TERMINAL_IO; use TERMINAL_IO;
  890.   with TEXT_IO; use TEXT_IO;
  891.   
  892.     -- This is the main routine for an on-line calculator program.  It is
  893.     -- set up to handle only integers at the present time.  One-letter 
  894.     -- variables may be defined and used.  The parser for expressions was
  895.     -- generated by LR on the VAX.  Lexical scanning routines were written
  896.     -- in Ada on the Wicat.
  897.   
  898.     -- Expressions are terminated by a semi-colon.  The program (normally)
  899.     -- terminates with a CTRL B.  Error handling is non-existent at this
  900.     -- time, so syntactic errors will also cause the program to exit.
  901.     
  902. procedure MAIN is
  903.  
  904. MY_STATUS : integer;
  905.   
  906.    begin-- body of the main procedure
  907.  
  908.  
  909.    --- prompt user
  910.  
  911.       NEW_LINE;
  912.       
  913.       PUT ( "* " );
  914.  
  915.  
  916.    --- call the parser
  917.  
  918.     MY_STATUS := LRPARSE;
  919.  
  920.  
  921.      case MY_STATUS is
  922.  
  923.        when 1 => PUT ("*** fatal syntactic error  *** ");
  924.  
  925.        when 2 => PUT ("*** parser stack overflow  *** ");
  926.  
  927.        when 3 => PUT ("*** premature end of input *** ");
  928.  
  929.        when 4 => PUT ("*** normal completion      *** ");
  930.  
  931.        when OTHERS => NULL;  -- unused values
  932.  
  933.     end case;
  934.     
  935.      NEW_LINE;
  936.  
  937.    end MAIN;
  938.  
  939.  
  940.  
  941.