home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / bison / Old_Bison / C / Lalr < prev    next >
Encoding:
Text File  |  1990-04-01  |  13.0 KB  |  717 lines

  1. /* Compute look-ahead criteria for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* Compute how to make the finite state machine deterministic;
  22.  find which rules need lookahead in each state, and which lookahead tokens they accept.
  23.  
  24. lalr(), the entry point, builds these data structures:
  25.  
  26. goto_map, from_state and to_state 
  27.  record each shift transition which accepts a variable (a nonterminal).
  28. ngotos is the number of such transitions.
  29. from_state[t] is the state number which a transition leads from
  30. and to_state[t] is the state number it leads to.
  31. All the transitions that accept a particular variable are grouped together and
  32. goto_map[i - ntokens] is the index in from_state and to_state of the first of them.
  33.  
  34. consistent[s] is nonzero if no lookahead is needed to decide what to do in state s.
  35.  
  36. LAruleno is a vector which records the rules that need lookahead in various states.
  37. The elements of LAruleno that apply to state s are those from
  38.  lookaheads[s] through lookaheads[s+1]-1.
  39. Each element of LAruleno is a rule number.
  40.  
  41. If lr is the length of LAruleno, then a number from 0 to lr-1 
  42. can specify both a rule and a state where the rule might be applied.
  43.  
  44. LA is a lr by ntokens matrix of bits.
  45. LA[l, i] is 1 if the rule LAruleno[l] is applicable in the appropriate state
  46.  when the next token is symbol i.
  47. If LA[l, i] and LA[l, j] are both 1 for i != j, it is a conflict.
  48. */
  49.  
  50. #include <stdio.h>
  51. #include "system.h"
  52. #include "machine.h"
  53. #include "types.h"
  54. #include "state.h"
  55. #include "new.h"
  56. #include "gram.h"
  57.  
  58.  
  59. extern short **derives;
  60. extern char *nullable;
  61.  
  62.  
  63. int tokensetsize;
  64. short *lookaheads;
  65. short *LAruleno;
  66. unsigned *LA;
  67. short *accessing_symbol;
  68. char *consistent;
  69. core **state_table;
  70. shifts **shift_table;
  71. reductions **reduction_table;
  72. short *goto_map;
  73. short *from_state;
  74. short *to_state;
  75.  
  76. static int infinity;
  77. static int maxrhs;
  78. static int ngotos;
  79. static unsigned *F;
  80. static short **includes;
  81. static shorts **lookback;
  82. static short **R;
  83. static short *INDEX;
  84. static short *VERTICES;
  85. static int top;
  86.  
  87.  
  88. void lalr(void)
  89. {
  90.   tokensetsize = WORDSIZE(ntokens);
  91.  
  92.   set_state_table();
  93.   set_accessing_symbol();
  94.   set_shift_table();
  95.   set_reduction_table();
  96.   set_maxrhs();
  97.   initialize_LA();
  98.   set_goto_map();
  99.   initialize_F();
  100.   build_relations();
  101.   compute_FOLLOWS();
  102.   compute_lookaheads();
  103. }
  104.  
  105.  
  106. void set_state_table(void)
  107. {
  108.   register core *sp;
  109.  
  110.   state_table = NEW2(nstates, core *);
  111.  
  112.   for (sp = first_state; sp; sp = sp->next)
  113.     state_table[sp->number] = sp;
  114. }
  115.  
  116.  
  117. void set_accessing_symbol(void)
  118. {
  119.   register core *sp;
  120.  
  121.   accessing_symbol = NEW2(nstates, short);
  122.  
  123.   for (sp = first_state; sp; sp = sp->next)
  124.     accessing_symbol[sp->number] = sp->accessing_symbol;
  125. }
  126.  
  127.  
  128. void set_shift_table(void)
  129. {
  130.   register shifts *sp;
  131.  
  132.   shift_table = NEW2(nstates, shifts *);
  133.  
  134.   for (sp = first_shift; sp; sp = sp->next)
  135.     shift_table[sp->number] = sp;
  136. }
  137.  
  138.  
  139. void set_reduction_table(void)
  140. {
  141.   register reductions *rp;
  142.  
  143.   reduction_table = NEW2(nstates, reductions *);
  144.  
  145.   for (rp = first_reduction; rp; rp = rp->next)
  146.     reduction_table[rp->number] = rp;
  147. }
  148.  
  149.  
  150. void set_maxrhs(void)
  151. {
  152.   register short *itemp;
  153.   register int length;
  154.   register int max;
  155.  
  156.   length = 0;
  157.   max = 0;
  158.   for (itemp = ritem; *itemp; itemp++)
  159.     {
  160.       if (*itemp > 0)
  161.     {
  162.       length++;
  163.     }
  164.       else
  165.     {
  166.       if (length > max) max = length;
  167.       length = 0;
  168.     }
  169.     }
  170.  
  171.   maxrhs = max;
  172. }
  173.  
  174.  
  175. void initialize_LA(void)
  176. {
  177.   register int i;
  178.   register int j;
  179.   register int count;
  180.   register reductions *rp;
  181.   register shifts *sp;
  182.   register short *np;
  183.  
  184.   consistent = NEW2(nstates, char);
  185.   lookaheads = NEW2(nstates + 1, short);
  186.  
  187.   count = 0;
  188.   for (i = 0; i < nstates; i++)
  189.     {
  190.       register int j;
  191.  
  192.       lookaheads[i] = count;
  193.  
  194.       rp = reduction_table[i];
  195.       sp = shift_table[i];
  196.       if (rp && (rp->nreds > 1
  197.           || (sp && ! ISVAR(accessing_symbol[sp->shifts[0]]))))
  198.     count += rp->nreds;
  199.       else
  200.     consistent[i] = 1;
  201.  
  202.       if (sp)
  203.     for (j = 0; j < sp->nshifts; j++)
  204.       {
  205.         if (accessing_symbol[sp->shifts[j]] == error_token_number)
  206.           {
  207.         consistent[i] = 0;
  208.         break;
  209.           }
  210.       }
  211.     }
  212.  
  213.   lookaheads[nstates] = count;
  214.  
  215.   LA = NEW2(count * tokensetsize, unsigned);
  216.   LAruleno = NEW2(count, short);
  217.   lookback = NEW2(count, shorts *);
  218.  
  219.   np = LAruleno;
  220.   for (i = 0; i < nstates; i++)
  221.     {
  222.       if (!consistent[i])
  223.     {
  224.       if ((rp = reduction_table[i]) != 0)
  225.         for (j = 0; j < rp->nreds; j++)
  226.           *np++ = rp->rules[j];
  227.     }
  228.     }
  229. }
  230.  
  231.  
  232. void set_goto_map(void)
  233. {
  234.   register shifts *sp;
  235.   register int i;
  236.   register int symbol;
  237.   register int k;
  238.   register short *temp_map;
  239.   register int state2;
  240.   register int state1;
  241.  
  242.   goto_map = NEW2(nvars + 1, short) - ntokens;
  243.   temp_map = NEW2(nvars + 1, short) - ntokens;
  244.  
  245.   ngotos = 0;
  246.   for (sp = first_shift; sp; sp = sp->next)
  247.     {
  248.       for (i = sp->nshifts - 1; i >= 0; i--)
  249.     {
  250.       symbol = accessing_symbol[sp->shifts[i]];
  251.  
  252.       if (ISTOKEN(symbol)) break;
  253.  
  254.       if (ngotos == MAXSHORT)
  255.         toomany("gotos");
  256.  
  257.       ngotos++;
  258.       goto_map[symbol]++;
  259.         }
  260.     }
  261.  
  262.   k = 0;
  263.   for (i = ntokens; i < nsyms; i++)
  264.     {
  265.       temp_map[i] = k;
  266.       k += goto_map[i];
  267.     }
  268.  
  269.   for (i = ntokens; i < nsyms; i++)
  270.     goto_map[i] = temp_map[i];
  271.  
  272.   goto_map[nsyms] = ngotos;
  273.   temp_map[nsyms] = ngotos;
  274.  
  275.   from_state = NEW2(ngotos, short);
  276.   to_state = NEW2(ngotos, short);
  277.  
  278.   for (sp = first_shift; sp; sp = sp->next)
  279.     {
  280.       state1 = sp->number;
  281.       for (i = sp->nshifts - 1; i >= 0; i--)
  282.     {
  283.       state2 = sp->shifts[i];
  284.       symbol = accessing_symbol[state2];
  285.  
  286.       if (ISTOKEN(symbol)) break;
  287.  
  288.       k = temp_map[symbol]++;
  289.       from_state[k] = state1;
  290.       to_state[k] = state2;
  291.     }
  292.     }
  293.  
  294.   FREE(temp_map + ntokens);
  295. }
  296.  
  297.  
  298.  
  299. /*  Map_goto maps a state/symbol pair into its numeric representation.    */
  300.  
  301. int map_goto(int state, int symbol)
  302. {
  303.   register int high;
  304.   register int low;
  305.   register int middle;
  306.   register int s;
  307.  
  308.   low = goto_map[symbol];
  309.   high = goto_map[symbol + 1] - 1;
  310.  
  311.   while (low <= high)
  312.     {
  313.       middle = (low + high) / 2;
  314.       s = from_state[middle];
  315.       if (s == state)
  316.     return (middle);
  317.       else if (s < state)
  318.     low = middle + 1;
  319.       else
  320.     high = middle - 1;
  321.     }
  322.  
  323.   berror("map_goto");
  324. /* NOTREACHED */
  325.   return 0;
  326. }
  327.  
  328.  
  329. void initialize_F(void)
  330. {
  331.   register int i;
  332.   register int j;
  333.   register int k;
  334.   register shifts *sp;
  335.   register short *edge;
  336.   register unsigned *rowp;
  337.   register short *rp;
  338.   register short **reads;
  339.   register int nedges;
  340.   register int stateno;
  341.   register int symbol;
  342.   register int nwords;
  343.  
  344.   nwords = ngotos * tokensetsize;
  345.   F = NEW2(nwords, unsigned);
  346.  
  347.   reads = NEW2(ngotos, short *);
  348.   edge = NEW2(ngotos + 1, short);
  349.   nedges = 0;
  350.  
  351.   rowp = F;
  352.   for (i = 0; i < ngotos; i++)
  353.     {
  354.       stateno = to_state[i];
  355.       sp = shift_table[stateno];
  356.  
  357.       if (sp)
  358.     {
  359.       k = sp->nshifts;
  360.  
  361.       for (j = 0; j < k; j++)
  362.         {
  363.           symbol = accessing_symbol[sp->shifts[j]];
  364.           if (ISVAR(symbol))
  365.         break;
  366.           SETBIT(rowp, symbol);
  367.         }
  368.  
  369.       for (; j < k; j++)
  370.         {
  371.           symbol = accessing_symbol[sp->shifts[j]];
  372.           if (nullable[symbol])
  373.         edge[nedges++] = map_goto(stateno, symbol);
  374.         }
  375.     
  376.       if (nedges)
  377.         {
  378.           reads[i] = rp = NEW2(nedges + 1, short);
  379.  
  380.           for (j = 0; j < nedges; j++)
  381.         rp[j] = edge[j];
  382.  
  383.           rp[nedges] = -1;
  384.           nedges = 0;
  385.         }
  386.     }
  387.  
  388.       rowp += tokensetsize;
  389.     }
  390.  
  391.   digraph(reads);
  392.  
  393.   for (i = 0; i < ngotos; i++)
  394.     {
  395.       if (reads[i])
  396.     FREE(reads[i]);
  397.     }
  398.  
  399.   FREE(reads);
  400.   FREE(edge);
  401. }
  402.  
  403.  
  404. void build_relations(void)
  405. {
  406.   register int i;
  407.   register int j;
  408.   register int k;
  409.   register short *rulep;
  410.   register short *rp;
  411.   register shifts *sp;
  412.   register int length;
  413.   register int nedges;
  414.   register int done;
  415.   register int state1;
  416.   register int stateno;
  417.   register int symbol1;
  418.   register int symbol2;
  419.   register short *shortp;
  420.   register short *edge;
  421.   register short *states;
  422.   register short **new_includes;
  423.  
  424.   includes = NEW2(ngotos, short *);
  425.   edge = NEW2(ngotos + 1, short);
  426.   states = NEW2(maxrhs + 1, short);
  427.  
  428.   for (i = 0; i < ngotos; i++)
  429.     {
  430.       nedges = 0;
  431.       state1 = from_state[i];
  432.       symbol1 = accessing_symbol[to_state[i]];
  433.  
  434.       for (rulep = derives[symbol1]; *rulep > 0; rulep++)
  435.     {
  436.       length = 1;
  437.       states[0] = state1;
  438.       stateno = state1;
  439.  
  440.       for (rp = ritem + rrhs[*rulep]; *rp > 0; rp++)
  441.         {
  442.           symbol2 = *rp;
  443.           sp = shift_table[stateno];
  444.           k = sp->nshifts;
  445.  
  446.           for (j = 0; j < k; j++)
  447.         {
  448.           stateno = sp->shifts[j];
  449.           if (accessing_symbol[stateno] == symbol2) break;
  450.         }
  451.  
  452.           states[length++] = stateno;
  453.         }
  454.  
  455.       if (!consistent[stateno])
  456.         add_lookback_edge(stateno, *rulep, i);
  457.  
  458.       length--;
  459.       done = 0;
  460.       while (!done)
  461.         {
  462.           done = 1;
  463.           rp--;
  464.             /* JF added rp>=ritem &&   I hope to god its right! */
  465.           if (rp>=ritem && ISVAR(*rp))
  466.         {
  467.           stateno = states[--length];
  468.           edge[nedges++] = map_goto(stateno, *rp);
  469.           if (nullable[*rp]) done = 0;
  470.         }
  471.         }
  472.     }
  473.  
  474.       if (nedges)
  475.     {
  476.       includes[i] = shortp = NEW2(nedges + 1, short);
  477.       for (j = 0; j < nedges; j++)
  478.         shortp[j] = edge[j];
  479.       shortp[nedges] = -1;
  480.     }
  481.     }
  482.  
  483.   new_includes = transpose(includes, ngotos);
  484.  
  485.   for (i = 0; i < ngotos; i++)
  486.     if (includes[i])
  487.       FREE(includes[i]);
  488.  
  489.   FREE(includes);
  490.  
  491.   includes = new_includes;
  492.  
  493.   FREE(edge);
  494.   FREE(states);
  495. }
  496.  
  497.  
  498. void add_lookback_edge(int stateno, int ruleno, int gotono)
  499. {
  500.   register int i;
  501.   register int k;
  502.   register int found;
  503.   register shorts *sp;
  504.  
  505.   i = lookaheads[stateno];
  506.   k = lookaheads[stateno + 1];
  507.   found = 0;
  508.   while (!found && i < k)
  509.     {
  510.       if (LAruleno[i] == ruleno)
  511.     found = 1;
  512.       else
  513.     i++;
  514.     }
  515.  
  516.   if (found == 0)
  517.     berror("add_lookback_edge");
  518.  
  519.   sp = NEW(shorts);
  520.   sp->next = lookback[i];
  521.   sp->value = gotono;
  522.   lookback[i] = sp;
  523. }
  524.  
  525.  
  526.  
  527. short **transpose(short **R, int n)
  528. {
  529.   register short **new_R;
  530.   register short **temp_R;
  531.   register short *nedges;
  532.   register short *sp;
  533.   register int i;
  534.   register int k;
  535.  
  536.   nedges = NEW2(n, short);
  537.  
  538.   for (i = 0; i < n; i++)
  539.     {
  540.       sp = R[i];
  541.       if (sp)
  542.     {
  543.       while (*sp >= 0)
  544.         nedges[*sp++]++;
  545.     }
  546.     }
  547.  
  548.   new_R = NEW2(n, short *);
  549.   temp_R = NEW2(n, short *);
  550.  
  551.   for (i = 0; i < n; i++)
  552.     {
  553.       k = nedges[i];
  554.       if (k > 0)
  555.     {
  556.       sp = NEW2(k + 1, short);
  557.       new_R[i] = sp;
  558.       temp_R[i] = sp;
  559.       sp[k] = -1;
  560.     }
  561.     }
  562.  
  563.   FREE(nedges);
  564.  
  565.   for (i = 0; i < n; i++)
  566.     {
  567.       sp = R[i];
  568.       if (sp)
  569.     {
  570.       while (*sp >= 0)
  571.         *temp_R[*sp++]++ = i;
  572.     }
  573.     }
  574.  
  575.   FREE(temp_R);
  576.  
  577.   return (new_R);
  578. }
  579.  
  580.  
  581. void compute_FOLLOWS(void)
  582. {
  583.   register int i;
  584.  
  585.   digraph(includes);
  586.  
  587.   for (i = 0; i < ngotos; i++)
  588.     {
  589.       if (includes[i]) FREE(includes[i]);
  590.     }
  591.  
  592.   FREE(includes);
  593. }
  594.  
  595.  
  596. void compute_lookaheads(void)
  597. {
  598.   register int i;
  599.   register int n;
  600.   register unsigned *fp1;
  601.   register unsigned *fp2;
  602.   register unsigned *fp3;
  603.   register shorts *sp;
  604.   register unsigned *rowp;
  605. /*  register short *rulep; JF unused */
  606. /*  register int count; JF unused */
  607.   register shorts *sptmp; /* JF */
  608.  
  609.   rowp = LA;
  610.   n = lookaheads[nstates];
  611.   for (i = 0; i < n; i++)
  612.     {
  613.       fp3 = rowp + tokensetsize;
  614.       for (sp = lookback[i]; sp; sp = sp->next)
  615.     {
  616.       fp1 = rowp;
  617.       fp2 = F + tokensetsize * sp->value;
  618.       while (fp1 < fp3)
  619.         *fp1++ |= *fp2++;
  620.     }
  621.  
  622.       rowp = fp3;
  623.     }
  624.  
  625.   for (i = 0; i < n; i++)
  626.     {/* JF removed ref to freed storage */
  627.       for (sp = lookback[i]; sp; sp = sptmp) {
  628.     sptmp=sp->next;
  629.     FREE(sp);
  630.       }
  631.     }
  632.  
  633.   FREE(lookback);
  634.   FREE(F);
  635. }
  636.  
  637.  
  638. void digraph(short **relation)
  639. {
  640.   register int i;
  641.  
  642.   infinity = ngotos + 2;
  643.   INDEX = NEW2(ngotos + 1, short);
  644.   VERTICES = NEW2(ngotos + 1, short);
  645.   top = 0;
  646.  
  647.   R = relation;
  648.  
  649.   for (i = 0; i < ngotos; i++)
  650.     INDEX[i] = 0;
  651.  
  652.   for (i = 0; i < ngotos; i++)
  653.     {
  654.       if (INDEX[i] == 0 && R[i])
  655.     traverse(i);
  656.     }
  657.  
  658.   FREE(INDEX);
  659.   FREE(VERTICES);
  660. }
  661.  
  662.  
  663. void traverse(int i)
  664. {
  665.   register unsigned *fp1;
  666.   register unsigned *fp2;
  667.   register unsigned *fp3;
  668.   register int j;
  669.   register short *rp;
  670.  
  671.   int height;
  672.   unsigned *base;
  673.  
  674.   VERTICES[++top] = i;
  675.   INDEX[i] = height = top;
  676.  
  677.   base = F + i * tokensetsize;
  678.   fp3 = base + tokensetsize;
  679.  
  680.   rp = R[i];
  681.   if (rp)
  682.     {
  683.       while ((j = *rp++) >= 0)
  684.     {
  685.       if (INDEX[j] == 0)
  686.         traverse(j);
  687.  
  688.       if (INDEX[i] > INDEX[j])
  689.         INDEX[i] = INDEX[j];
  690.  
  691.       fp1 = base;
  692.       fp2 = F + j * tokensetsize;
  693.  
  694.       while (fp1 < fp3)
  695.         *fp1++ |= *fp2++;
  696.     }
  697.     }
  698.  
  699.   if (INDEX[i] == height)
  700.     {
  701.       for (;;)
  702.     {
  703.       j = VERTICES[top--];
  704.       INDEX[j] = infinity;
  705.  
  706.       if (i == j)
  707.         break;
  708.  
  709.       fp1 = base;
  710.       fp2 = F + j * tokensetsize;
  711.  
  712.       while (fp1 < fp3)
  713.         *fp2++ = *fp1++;
  714.     }
  715.     }
  716. }
  717.