/[bison]/bison/src/output.c
ViewVC logotype

Diff of /bison/src/output.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.185 by akim, Wed Jul 3 12:51:30 2002 UTC revision 1.186 by akim, Thu Jul 25 17:30:44 2002 UTC
# Line 76  Line 76 
76     in a roundabout way, the bounds of the portion you are trying to     in a roundabout way, the bounds of the portion you are trying to
77     examine.     examine.
78    
79     Suppose that the portion of yytable starts at index P and the index     Suppose that the portion of YYTABLE starts at index P and the index
80     to be examined within the portion is I.  Then if YYCHECK[P+I] != I,     to be examined within the portion is I.  Then if YYCHECK[P+I] != I,
81     I is outside the bounds of what is actually allocated, and the     I is outside the bounds of what is actually allocated, and the
82     default (from YYDEFACT or YYDEFGOTO) should be used.  Otherwise,     default (from YYDEFACT or YYDEFGOTO) should be used.  Otherwise,
# Line 104  Line 104 
104  /* From src/scan-skel.l. */  /* From src/scan-skel.l. */
105  void m4_invoke PARAMS ((const char *definitions));  void m4_invoke PARAMS ((const char *definitions));
106    
107    
108    /* Several tables will be indexed both by state and nonterminal
109       numbers.  We call `vector' such a thing (= either a state or a
110       symbol number.
111    
112       Of course vector_number_t ought to be wide enough to contain
113       state_number_t and symbol_number_t.  */
114    typedef short vector_number_t;
115    #define VECTOR_NUMBER_MAX ((vector_number_t) SHRT_MAX)
116    #define VECTOR_NUMBER_MIN ((vector_number_t) SHRT_MIN)
117    #define state_number_to_vector_number(State) \
118       ((vector_number_t) State)
119    #define symbol_number_to_vector_number(Symbol) \
120       ((vector_number_t) (state_number_as_int (nstates) + Symbol - ntokens))
121    
122  static int nvectors;  static int nvectors;
123  static int nentries;  
124  static state_number_t **froms = NULL;  
125  static state_number_t **tos = NULL;  /* FROMS and TOS are indexed by vector_number_t.
126    
127       If VECTOR is a nonterminal, (FROMS[VECTOR], TOS[VECTOR]) form an
128       array of state numbers of the non defaulted GOTO on VECTOR.
129    
130       If VECTOR is a state, TOS[VECTOR] is the array of actions to do on
131       the (array of) symbols FROMS[VECTOR].
132    
133       In both cases, TALLY[VECTOR] is the size of the arrays
134       FROMS[VECTOR], TOS[VECTOR]; and WIDTH[VECTOR] =
135       (FROMS[VECTOR][SIZE] - FROMS[VECTOR][0] + 1) where SIZE =
136       TALLY[VECTOR].
137    
138       FROMS therefore contains symbol_number_t and action_number_t,
139       TOS state_number_t and action_number_t,
140       TALLY sizes,
141       WIDTH differences of FROMS.
142    
143       Let base_t be the type of FROMS, TOS, and WIDTH.  */
144    typedef int base_t;
145    #define BASE_MAX ((base_t) INT_MAX)
146    #define BASE_MIN ((base_t) INT_MIN)
147    
148    static base_t **froms = NULL;
149    static base_t **tos = NULL;
150  static unsigned int **conflict_tos = NULL;  static unsigned int **conflict_tos = NULL;
151  static short *tally = NULL;  static short *tally = NULL;
152  static short *width = NULL;  static base_t *width = NULL;
153  static short *actrow = NULL;  
154  static short *conflrow = NULL;  
155  static short *state_count = NULL;  /* For a given state, N = ACTROW[SYMBOL]:
156  static short *order = NULL;  
157  static short *base = NULL;     If N = 0, stands for `run the default action'.
158  static short *pos = NULL;     If N = MIN, stands for `raise a parse error'.
159       If N > 0, stands for `shift SYMBOL and go to n'.
160       If N < 0, stands for `reduce -N'.  */
161    typedef short action_t;
162    #define ACTION_MAX ((action_t) SHRT_MAX)
163    #define ACTION_MIN ((action_t) SHRT_MIN)
164    
165    static action_t *actrow = NULL;
166    
167    /* FROMS and TOS are reordered to be compressed.  ORDER[VECTOR] is the
168       new vector number of VECTOR.  We skip `empty' vectors (i.e.,
169       TALLY[VECTOR] = 0), and call these `entries'.  */
170    static vector_number_t *order = NULL;
171    static int nentries;
172    
173    static base_t *base = NULL;
174    /* A distinguished value of BASE, negative infinite.  During the
175       computation equals to BASE_MIN, later mapped to BASE_NINF to
176       keep parser tables small.  */
177    base_t base_ninf = 0;
178    static base_t *pos = NULL;
179    
180    static unsigned int *conflrow = NULL;
181  static unsigned int *conflict_table = NULL;  static unsigned int *conflict_table = NULL;
182  static unsigned int *conflict_list = NULL;  static unsigned int *conflict_list = NULL;
183  static int conflict_list_cnt;  static int conflict_list_cnt;
# Line 127  static int conflict_list_free; Line 187  static int conflict_list_free;
187     We start with the original hard-coded value: SHRT_MAX     We start with the original hard-coded value: SHRT_MAX
188     (yes, not USHRT_MAX). */     (yes, not USHRT_MAX). */
189  static size_t table_size = SHRT_MAX;  static size_t table_size = SHRT_MAX;
190  static short *table = NULL;  static base_t *table = NULL;
191  static short *check = NULL;  static base_t *check = NULL;
192    /* The value used in TABLE to denote explicit parse errors
193       (%nonassoc), a negative infinite.  First defaults to ACTION_MIN,
194       but in order to keep small tables, renumbered as TABLE_ERROR, which
195       is the smallest (non error) value minus 1.  */
196    base_t table_ninf = 0;
197  static int lowzero;  static int lowzero;
198  static int high;  static int high;
199    
# Line 155  table_grow (size_t desired) Line 220  table_grow (size_t desired)
220      fprintf (stderr, "growing table and check from: %d to %d\n",      fprintf (stderr, "growing table and check from: %d to %d\n",
221               old_size, table_size);               old_size, table_size);
222    
223    table = XREALLOC (table, short, table_size);    table = XREALLOC (table, base_t, table_size);
224    check = XREALLOC (check, short, table_size);    check = XREALLOC (check, base_t, table_size);
225    if (glr_parser)    if (glr_parser)
226      conflict_table = XREALLOC (conflict_table, unsigned int, table_size);      conflict_table = XREALLOC (conflict_table, unsigned int, table_size);
227    
# Line 185  Name (const char *name,                                                        \ Line 250  Name (const char *name,                                                        \
250        int begin,                                                        \        int begin,                                                        \
251        int end)                                                          \        int end)                                                          \
252  {                                                                       \  {                                                                       \
253      Type min = first;                                                     \
254    Type max = first;                                                     \    Type max = first;                                                     \
255    int i;                                                                \    int i;                                                                \
256    int j = 1;                                                            \    int j = 1;                                                            \
# Line 201  Name (const char *name,                                                        \ Line 267  Name (const char *name,                                                        \
267        else                                                              \        else                                                              \
268          ++j;                                                            \          ++j;                                                            \
269        obstack_fgrow1 (&format_obstack, "%6d", table_data[i]);           \        obstack_fgrow1 (&format_obstack, "%6d", table_data[i]);           \
270        if (table_data[i] > max)                                          \        if (table_data[i] < min)                                          \
271            min = table_data[i];                                            \
272          if (max < table_data[i])                                          \
273          max = table_data[i];                                            \          max = table_data[i];                                            \
274      }                                                                   \      }                                                                   \
275    obstack_1grow (&format_obstack, 0);                                   \    obstack_1grow (&format_obstack, 0);                                   \
276    muscle_insert (name, obstack_finish (&format_obstack));               \    muscle_insert (name, obstack_finish (&format_obstack));               \
277                                                                          \                                                                          \
278    /* Build `NAME_max' in the obstack. */                                \    /* Build `NAME_min' and `NAME_max' in the obstack. */                 \
279      obstack_fgrow1 (&format_obstack, "%s_min", name);                     \
280      obstack_1grow (&format_obstack, 0);                                   \
281      MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack),             \
282                              (long int) min);                              \
283    obstack_fgrow1 (&format_obstack, "%s_max", name);                     \    obstack_fgrow1 (&format_obstack, "%s_max", name);                     \
284    obstack_1grow (&format_obstack, 0);                                   \    obstack_1grow (&format_obstack, 0);                                   \
285    MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack),             \    MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack),             \
# Line 215  Name (const char *name,                                                        \ Line 287  Name (const char *name,                                                        \
287  }  }
288    
289  GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int)  GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int)
290    GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_int_table, int)
291  GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_short_table, short)  GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_short_table, short)
292    GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_base_table, base_t)
293    GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_rule_number_table, rule_number_t)
294  GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table, symbol_number_t)  GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table, symbol_number_t)
295  GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number_t)  GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number_t)
296  GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number_t)  GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number_t)
# Line 266  prepare_tokens (void) Line 341  prepare_tokens (void)
341      muscle_insert ("tname", obstack_finish (&format_obstack));      muscle_insert ("tname", obstack_finish (&format_obstack));
342    }    }
343    
344      /* Output YYTOKNUM. */    /* Output YYTOKNUM. */
345    {    {
346      int i;      int i;
347      short *values = XCALLOC (short, ntokens + 1);      int *values = XCALLOC (int, ntokens + 1);
348      for (i = 0; i < ntokens + 1; ++i)      for (i = 0; i < ntokens + 1; ++i)
349        values[i] = symbols[i]->user_token_number;        values[i] = symbols[i]->user_token_number;
350      muscle_insert_short_table ("toknum", values,      muscle_insert_int_table ("toknum", values,
351                                 0, 1, ntokens + 1);                               0, 1, ntokens + 1);
352      free (values);      free (values);
353    }    }
354  }  }
# Line 356  prepare_states (void) Line 431  prepare_states (void)
431    
432  /*-------------------------------------------------------------------.  /*-------------------------------------------------------------------.
433  | For GLR parsers, for each conflicted token in STATE, as indicated  |  | For GLR parsers, for each conflicted token in STATE, as indicated  |
434  | by non-zero entries in conflrow, create a list of possible         |  | by non-zero entries in CONFLROW, create a list of possible         |
435  | reductions that are alternatives to the shift or reduction         |  | reductions that are alternatives to the shift or reduction         |
436  | currently recorded for that token in STATE.  Store the alternative |  | currently recorded for that token in STATE.  Store the alternative |
437  | reductions followed by a 0 in conflict_list, updating              |  | reductions followed by a 0 in conflict_list, updating              |
438  | conflict_list_cnt, and storing an index to the start of the list   |  | conflict_list_cnt, and storing an index to the start of the list   |
439  | back into conflrow.                                                |  | back into CONFLROW.                                                |
440  `-------------------------------------------------------------------*/  `-------------------------------------------------------------------*/
441    
442  static void  static void
# Line 377  conflict_row (state_t *state) Line 452  conflict_row (state_t *state)
452        {        {
453          conflrow[j] = conflict_list_cnt;          conflrow[j] = conflict_list_cnt;
454    
455          /* find all reductions for token j, and record all that do          /* Find all reductions for token J, and record all that do not
456           * not match actrow[j] */             match ACTROW[J].  */
457          for (i = 0; i < state->nlookaheads; i += 1)          for (i = 0; i < state->nlookaheads; i += 1)
458            if (bitset_test (state->lookaheads[i], j)            if (bitset_test (state->lookaheads[i], j)
459                && actrow[j] != -state->lookaheads_rule[i]->number)                && (actrow[j]
460                      != rule_number_as_item_number (state->lookaheads_rule[i]->number)))
461              {              {
462                assert (conflict_list_free > 0);                assert (conflict_list_free > 0);
463                conflict_list[conflict_list_cnt]                conflict_list[conflict_list_cnt]
# Line 390  conflict_row (state_t *state) Line 466  conflict_row (state_t *state)
466                conflict_list_free -= 1;                conflict_list_free -= 1;
467              }              }
468    
469          /* Leave a 0 at the end */          /* Leave a 0 at the end.  */
470          assert (conflict_list_free > 0);          assert (conflict_list_free > 0);
471          conflict_list_cnt += 1;          conflict_list_cnt += 1;
472          conflict_list_free -= 1;          conflict_list_free -= 1;
# Line 401  conflict_row (state_t *state) Line 477  conflict_row (state_t *state)
477  /*------------------------------------------------------------------.  /*------------------------------------------------------------------.
478  | Decide what to do for each type of token if seen as the lookahead |  | Decide what to do for each type of token if seen as the lookahead |
479  | token in specified state.  The value returned is used as the      |  | token in specified state.  The value returned is used as the      |
480  | default action (yydefact) for the state.  In addition, actrow is  |  | default action (yydefact) for the state.  In addition, ACTROW is  |
481  | filled with what to do for each kind of token, index by symbol    |  | filled with what to do for each kind of token, index by symbol    |
482  | number, with zero meaning do the default action.  The value       |  | number, with zero meaning do the default action.  The value       |
483  | SHRT_MIN, a very negative number, means this situation is an      |  | ACTION_MIN, a very negative number, means this situation is an    |
484  | error.  The parser recognizes this value specially.               |  | error.  The parser recognizes this value specially.               |
485  |                                                                   |  |                                                                   |
486  | This is where conflicts are resolved.  The loop over lookahead    |  | This is where conflicts are resolved.  The loop over lookahead    |
487  | rules considered lower-numbered rules last, and the last rule     |  | rules considered lower-numbered rules last, and the last rule     |
488  | considered that likes a token gets to handle it.                  |  | considered that likes a token gets to handle it.                  |
489  |                                                                   |  |                                                                   |
490  | For GLR parsers, also sets conflrow[SYM] to an index into         |  | For GLR parsers, also sets CONFLROW[SYM] to an index into         |
491  | conflict_list iff there is an unresolved conflict (s/r or r/r)    |  | conflict_list iff there is an unresolved conflict (s/r or r/r)    |
492  | with symbol SYM. The default reduction is not used for a symbol   |  | with symbol SYM. The default reduction is not used for a symbol   |
493  | that has any such conflicts.                                      |  | that has any such conflicts.                                      |
494  `------------------------------------------------------------------*/  `------------------------------------------------------------------*/
495    
496  static int  static rule_number_t
497  action_row (state_t *state)  action_row (state_t *state)
498  {  {
499    int i;    int i;
# Line 447  action_row (state_t *state) Line 523  action_row (state_t *state)
523               token follows.  */               token follows.  */
524            if (actrow[j] != 0)            if (actrow[j] != 0)
525              conflicted = conflrow[j] = 1;              conflicted = conflrow[j] = 1;
526            actrow[j] = -state->lookaheads_rule[i]->number;            actrow[j] = rule_number_as_item_number (state->lookaheads_rule[i]->number);
527          }          }
528      }      }
529    
# Line 471  action_row (state_t *state) Line 547  action_row (state_t *state)
547        }        }
548    
549    /* See which tokens are an explicit error in this state (due to    /* See which tokens are an explicit error in this state (due to
550       %nonassoc).  For them, record SHRT_MIN as the action.  */       %nonassoc).  For them, record ACTION_MIN as the action.  */
551    for (i = 0; i < errp->num; i++)    for (i = 0; i < errp->num; i++)
552      {      {
553        symbol_number_t symbol = errp->symbols[i];        symbol_number_t symbol = errp->symbols[i];
554        actrow[symbol] = SHRT_MIN;        actrow[symbol] = ACTION_MIN;
555      }      }
556    
557    /* Now find the most common reduction and make it the default action    /* Now find the most common reduction and make it the default action
# Line 495  action_row (state_t *state) Line 571  action_row (state_t *state)
571                symbol_number_t j;                symbol_number_t j;
572    
573                for (j = 0; j < ntokens; j++)                for (j = 0; j < ntokens; j++)
574                  if (actrow[j] == -rule)                  if (actrow[j] == rule_number_as_item_number (rule))
575                    count++;                    count++;
576    
577                if (count > max)                if (count > max)
# Line 515  action_row (state_t *state) Line 591  action_row (state_t *state)
591              {              {
592                int j;                int j;
593                for (j = 0; j < ntokens; j++)                for (j = 0; j < ntokens; j++)
594                  if (actrow[j] == -default_rule                  if (actrow[j] == rule_number_as_item_number (default_rule)
595                      && ! (glr_parser && conflrow[j]))                      && ! (glr_parser && conflrow[j]))
596                    actrow[j] = 0;                    actrow[j] = 0;
597              }              }
# Line 527  action_row (state_t *state) Line 603  action_row (state_t *state)
603    
604    if (default_rule == 0)    if (default_rule == 0)
605      for (i = 0; i < ntokens; i++)      for (i = 0; i < ntokens; i++)
606        if (actrow[i] == SHRT_MIN)        if (actrow[i] == ACTION_MIN)
607          actrow[i] = 0;          actrow[i] = 0;
608    
609    if (conflicted)    if (conflicted)
# Line 537  action_row (state_t *state) Line 613  action_row (state_t *state)
613  }  }
614    
615    
616    /*--------------------------------------------.
617    | Set FROMS, TOS, TALLY and WIDTH for STATE.  |
618    `--------------------------------------------*/
619    
620  static void  static void
621  save_row (state_number_t state)  save_row (state_number_t state)
622  {  {
623    symbol_number_t i;    symbol_number_t i;
624    int count;    int count;
625    short *sp = NULL;    base_t *sp = NULL;
626    short *sp1 = NULL;    base_t *sp1 = NULL;
627    short *sp2 = NULL;    base_t *sp2 = NULL;
628    unsigned int *sp3 = NULL;    unsigned int *sp3 = NULL;
629    
630      /* Number of non default actions in STATE.  */
631    count = 0;    count = 0;
632    for (i = 0; i < ntokens; i++)    for (i = 0; i < ntokens; i++)
633      if (actrow[i] != 0)      if (actrow[i] != 0)
# Line 555  save_row (state_number_t state) Line 636  save_row (state_number_t state)
636    if (count == 0)    if (count == 0)
637      return;      return;
638    
639    froms[state] = sp1 = sp = XCALLOC (short, count);    /* Allocate non defaulted actions.  */
640    tos[state] = sp2 = XCALLOC (short, count);    froms[state] = sp1 = sp = XCALLOC (base_t, count);
641      tos[state] = sp2 = XCALLOC (base_t, count);
642    if (glr_parser)    if (glr_parser)
643      conflict_tos[state] = sp3 = XCALLOC (unsigned int, count);      conflict_tos[state] = sp3 = XCALLOC (unsigned int, count);
644    else    else
645      conflict_tos[state] = NULL;      conflict_tos[state] = NULL;
646    
647      /* Store non defaulted actions.  */
648    for (i = 0; i < ntokens; i++)    for (i = 0; i < ntokens; i++)
649      if (actrow[i] != 0)      if (actrow[i] != 0)
650        {        {
# Line 590  token_actions (void) Line 673  token_actions (void)
673    state_number_t i;    state_number_t i;
674    int nconflict = conflicts_total_count ();    int nconflict = conflicts_total_count ();
675    
676    short *yydefact = XCALLOC (short, nstates);    rule_number_t *yydefact = XCALLOC (rule_number_t, nstates);
677    
678    actrow = XCALLOC (short, ntokens);    actrow = XCALLOC (action_t, ntokens);
679      conflrow = XCALLOC (unsigned int, ntokens);
680    
   conflrow = XCALLOC (short, ntokens);  
681    if (glr_parser)    if (glr_parser)
682      {      {
683        conflict_list = XCALLOC (unsigned int, 1 + 2 * nconflict);        conflict_list = XCALLOC (unsigned int, 1 + 2 * nconflict);
# Line 610  token_actions (void) Line 693  token_actions (void)
693        save_row (i);        save_row (i);
694      }      }
695    
696    muscle_insert_short_table ("defact", yydefact,    muscle_insert_rule_number_table ("defact", yydefact,
697                               yydefact[0], 1, nstates);                                     yydefact[0], 1, nstates);
698    XFREE (actrow);    XFREE (actrow);
699    XFREE (conflrow);    XFREE (conflrow);
700    XFREE (yydefact);    XFREE (yydefact);
# Line 783  symbol_printers_output (FILE *out) Line 866  symbol_printers_output (FILE *out)
866  }  }
867    
868    
869    /*------------------------------------------------------------------.
870    | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], |
871    | i.e., the information related to non defaulted GOTO on the nterm  |
872    | SYMBOL.                                                           |
873    |                                                                   |
874    | DEFAULT_STATE is the principal destination on SYMBOL, i.e., the   |
875    | default GOTO destination on SYMBOL.                               |
876    `------------------------------------------------------------------*/
877    
878  static void  static void
879  save_column (symbol_number_t symbol, state_number_t default_state)  save_column (symbol_number_t symbol, state_number_t default_state)
880  {  {
881    int i;    int i;
882    state_number_t *sp;    base_t *sp;
883    state_number_t *sp1;    base_t *sp1;
884    state_number_t *sp2;    base_t *sp2;
885    int count;    int count;
886    int symno = symbol - ntokens + state_number_as_int (nstates);    vector_number_t symno = symbol_number_to_vector_number (symbol);
887    
888    goto_number_t begin = goto_map[symbol];    goto_number_t begin = goto_map[symbol];
889    goto_number_t end = goto_map[symbol + 1];    goto_number_t end = goto_map[symbol + 1];
890    
891      /* Number of non default GOTO.  */
892    count = 0;    count = 0;
893    for (i = begin; i < end; i++)    for (i = begin; i < end; i++)
894      if (to_state[i] != default_state)      if (to_state[i] != default_state)
# Line 804  save_column (symbol_number_t symbol, sta Line 897  save_column (symbol_number_t symbol, sta
897    if (count == 0)    if (count == 0)
898      return;      return;
899    
900    froms[symno] = sp1 = sp = XCALLOC (short, count);    /* Allocate room for non defaulted gotos.  */
901    tos[symno] = sp2 = XCALLOC (short, count);    froms[symno] = sp1 = sp = XCALLOC (base_t, count);
902      tos[symno] = sp2 = XCALLOC (base_t, count);
903    
904      /* Store the state numbers of the non defaulted gotos.  */
905    for (i = begin; i < end; i++)    for (i = begin; i < end; i++)
906      if (to_state[i] != default_state)      if (to_state[i] != default_state)
907        {        {
# Line 819  save_column (symbol_number_t symbol, sta Line 914  save_column (symbol_number_t symbol, sta
914  }  }
915    
916    
917    /*----------------------------------------------------------------.
918    | Return `the' most common destination GOTO on SYMBOL (a nterm).  |
919    `----------------------------------------------------------------*/
920    
921  static state_number_t  static state_number_t
922  default_goto (symbol_number_t symbol)  default_goto (symbol_number_t symbol, short state_count[])
923  {  {
924    state_number_t s;    state_number_t s;
925    int i;    int i;
# Line 862  static void Line 961  static void
961  goto_actions (void)  goto_actions (void)
962  {  {
963    symbol_number_t i;    symbol_number_t i;
964    state_number_t *yydefgoto = XMALLOC (state_number_t, nsyms - ntokens);    state_number_t *yydefgoto = XMALLOC (state_number_t, nvars);
965    
966    state_count = XCALLOC (short, nstates);    /* For a given nterm I, STATE_COUNT[S] is the number of times there
967         is a GOTO to S on I.  */
968      short *state_count = XCALLOC (short, nstates);
969    for (i = ntokens; i < nsyms; ++i)    for (i = ntokens; i < nsyms; ++i)
970      {      {
971        state_number_t default_state = default_goto (i);        state_number_t default_state = default_goto (i, state_count);
972        save_column (i, default_state);        save_column (i, default_state);
973        yydefgoto[i - ntokens] = default_state;        yydefgoto[i - ntokens] = default_state;
974      }      }
# Line 879  goto_actions (void) Line 980  goto_actions (void)
980  }  }
981    
982    
983  /* The next few functions decide how to pack the actions and gotos  /*------------------------------------------------------------------.
984     information into yytable. */  | Compute ORDER, a reordering of vectors, in order to decide how to |
985    | pack the actions and gotos information into yytable.              |
986    `------------------------------------------------------------------*/
987    
988  static void  static void
989  sort_actions (void)  sort_actions (void)
# Line 912  sort_actions (void) Line 1015  sort_actions (void)
1015  }  }
1016    
1017    
1018  static int  /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY
1019  matching_state (int vector)     and WIDTH of VECTOR) are common to a previous state, return this
1020       state number.
1021    
1022       In any other case, return -1.  */
1023    
1024    static state_number_t
1025    matching_state (vector_number_t vector)
1026  {  {
1027    int i = order[vector];    vector_number_t i = order[vector];
1028    int t;    int t;
1029    int w;    int w;
1030    int prev;    int prev;
1031    
1032      /* If VECTOR is a nterm, return -1.  */
1033    if (i >= (int) nstates)    if (i >= (int) nstates)
1034      return -1;      return -1;
1035    
# Line 928  matching_state (int vector) Line 1038  matching_state (int vector)
1038    
1039    for (prev = vector - 1; prev >= 0; prev--)    for (prev = vector - 1; prev >= 0; prev--)
1040      {      {
1041        int j = order[prev];        vector_number_t j = order[prev];
1042        int k;        int k;
1043        int match = 1;        int match = 1;
1044    
1045          /* Given how ORDER was computed, if the WIDTH or TALLY is
1046             different, there cannot be a matching state.  */
1047        if (width[j] != w || tally[j] != t)        if (width[j] != w || tally[j] != t)
1048          return -1;          return -1;
1049    
# Line 947  matching_state (int vector) Line 1059  matching_state (int vector)
1059  }  }
1060    
1061    
1062  static int  static base_t
1063  pack_vector (int vector)  pack_vector (vector_number_t vector)
1064  {  {
1065    int i = order[vector];    vector_number_t i = order[vector];
1066    int j;    int j;
1067    int t = tally[i];    int t = tally[i];
1068    int loc = 0;    int loc = 0;
1069    short *from = froms[i];    base_t *from = froms[i];
1070    short *to = tos[i];    base_t *to = tos[i];
1071    unsigned int *conflict_to = conflict_tos[i];    unsigned int *conflict_to = conflict_tos[i];
1072    
1073    assert (t);    assert (t);
# Line 983  pack_vector (int vector) Line 1095  pack_vector (int vector)
1095          {          {
1096            for (k = 0; k < t; k++)            for (k = 0; k < t; k++)
1097              {              {
1098                loc = j + state_number_as_int (from[k]);                loc = j + from[k];
1099                table[loc] = state_number_as_int (to[k]);                table[loc] = to[k];
1100                if (glr_parser && conflict_to != NULL)                if (glr_parser && conflict_to != NULL)
1101                  conflict_table[loc] = conflict_to[k];                  conflict_table[loc] = conflict_to[k];
1102                check[loc] = state_number_as_int (from[k]);                check[loc] = from[k];
1103              }              }
1104    
1105            while (table[lowzero] != 0)            while (table[lowzero] != 0)
# Line 996  pack_vector (int vector) Line 1108  pack_vector (int vector)
1108            if (loc > high)            if (loc > high)
1109              high = loc;              high = loc;
1110    
1111              if (j < BASE_MIN || BASE_MAX < j)
1112                fatal ("base_t too small to hold %d\n", j);
1113            return j;            return j;
1114          }          }
1115      }      }
# Line 1005  pack_vector (int vector) Line 1119  pack_vector (int vector)
1119  }  }
1120    
1121    
1122    /*-------------------------------------------------------------.
1123    | Remap the negative infinite in TAB from NINF to the greatest |
1124    | possible smallest value.  Return it.                         |
1125    |                                                              |
1126    | In most case this allows us to use shorts instead of ints in |
1127    | parsers.                                                     |
1128    `-------------------------------------------------------------*/
1129    
1130    static base_t
1131    table_ninf_remap (base_t tab[], size_t size, base_t ninf)
1132    {
1133      base_t res = 0;
1134      size_t i;
1135    
1136      for (i = 0; i < size; i++)
1137        if (tab[i] < res && tab[i] != ninf)
1138          res = base[i];
1139    
1140      --res;
1141    
1142      for (i = 0; i < size; i++)
1143        if (tab[i] == ninf)
1144          tab[i] = res;
1145    
1146      return res;
1147    }
1148    
1149  static void  static void
1150  pack_table (void)  pack_table (void)
1151  {  {
1152    int i;    int i;
   int place;  
   int state;  
1153    
1154    base = XCALLOC (short, nvectors);    base = XCALLOC (base_t, nvectors);
1155    pos = XCALLOC (short, nentries);    pos = XCALLOC (base_t, nentries);
1156    table = XCALLOC (short, table_size);    table = XCALLOC (base_t, table_size);
1157    if (glr_parser)    if (glr_parser)
1158      conflict_table = XCALLOC (unsigned int, table_size);      conflict_table = XCALLOC (unsigned int, table_size);
1159    check = XCALLOC (short, table_size);    check = XCALLOC (base_t, table_size);
1160    
1161    lowzero = 0;    lowzero = 0;
1162    high = 0;    high = 0;
1163    
1164    for (i = 0; i < nvectors; i++)    for (i = 0; i < nvectors; i++)
1165      base[i] = SHRT_MIN;      base[i] = BASE_MIN;
1166    
1167    for (i = 0; i < (int) table_size; i++)    for (i = 0; i < (int) table_size; i++)
1168      check[i] = -1;      check[i] = -1;
1169    
1170    for (i = 0; i < nentries; i++)    for (i = 0; i < nentries; i++)
1171      {      {
1172        state = matching_state (i);        state_number_t state = matching_state (i);
1173          base_t place;
1174    
1175        if (state < 0)        if (state < 0)
1176            /* A new set of state actions, or a nonterminal.  */
1177          place = pack_vector (i);          place = pack_vector (i);
1178        else        else
1179            /* Action of I were already coded for STATE.  */
1180          place = base[state];          place = base[state];
1181    
1182        pos[i] = place;        pos[i] = place;
1183        base[order[i]] = place;        base[order[i]] = place;
1184      }      }
1185    
1186      /* Use the greatest possible negative infinites.  */
1187      base_ninf = table_ninf_remap (base, nvectors, BASE_MIN);
1188      table_ninf = table_ninf_remap (table, high + 1, ACTION_MIN);
1189    
1190    for (i = 0; i < nvectors; i++)    for (i = 0; i < nvectors; i++)
1191      {      {
1192        XFREE (froms[i]);        XFREE (froms[i]);
# Line 1054  pack_table (void) Line 1200  pack_table (void)
1200    free (pos);    free (pos);
1201  }  }
1202    
1203    
1204  /* the following functions output yytable, yycheck, yyconflp, yyconfl,  /* the following functions output yytable, yycheck, yyconflp, yyconfl,
1205     and the vectors whose elements index the portion starts */     and the vectors whose elements index the portion starts.  */
1206    
1207  static void  static void
1208  output_base (void)  output_base (void)
1209  {  {
1210    /* Output pact. */    /* Output PACT. */
1211    muscle_insert_short_table ("pact", base,    muscle_insert_base_table ("pact", base,
1212                               base[0], 1, nstates);                               base[0], 1, nstates);
1213      MUSCLE_INSERT_INT ("pact_ninf", base_ninf);
1214    
1215    /* Output pgoto. */    /* Output PGOTO. */
1216    muscle_insert_short_table ("pgoto", base,    muscle_insert_base_table ("pgoto", base,
1217                               base[nstates], nstates + 1, nvectors);                               base[nstates], nstates + 1, nvectors);
1218    XFREE (base);    XFREE (base);
1219  }  }
# Line 1074  output_base (void) Line 1222  output_base (void)
1222  static void  static void
1223  output_table (void)  output_table (void)
1224  {  {
1225    muscle_insert_short_table ("table", table,    muscle_insert_base_table ("table", table,
1226                               table[0], 1, high + 1);                              table[0], 1, high + 1);
1227      MUSCLE_INSERT_INT ("table_ninf", table_ninf);
1228    XFREE (table);    XFREE (table);
1229  }  }
1230    
# Line 1105  output_conflicts (void) Line 1254  output_conflicts (void)
1254  static void  static void
1255  output_check (void)  output_check (void)
1256  {  {
1257    muscle_insert_short_table ("check", check,    muscle_insert_base_table ("check", check,
1258                               check[0], 1, high + 1);                              check[0], 1, high + 1);
1259    XFREE (check);    XFREE (check);
1260  }  }
1261    
# Line 1126  prepare_actions (void) Line 1275  prepare_actions (void)
1275    
1276    nvectors = state_number_as_int (nstates) + nvars;    nvectors = state_number_as_int (nstates) + nvars;
1277    
1278    froms = XCALLOC (short *, nvectors);    froms = XCALLOC (base_t *, nvectors);
1279    tos = XCALLOC (short *, nvectors);    tos = XCALLOC (base_t *, nvectors);
1280    conflict_tos = XCALLOC (unsigned int *, nvectors);    conflict_tos = XCALLOC (unsigned int *, nvectors);
1281    tally = XCALLOC (short, nvectors);    tally = XCALLOC (short, nvectors);
1282    width = XCALLOC (short, nvectors);    width = XCALLOC (base_t, nvectors);
1283    
1284    token_actions ();    token_actions ();
1285    bitsetv_free (LA);    bitsetv_free (LA);
# Line 1141  prepare_actions (void) Line 1290  prepare_actions (void)
1290    XFREE (from_state);    XFREE (from_state);
1291    XFREE (to_state);    XFREE (to_state);
1292    
1293    order = XCALLOC (short, nvectors);    order = XCALLOC (vector_number_t, nvectors);
1294    sort_actions ();    sort_actions ();
1295    pack_table ();    pack_table ();
1296    free (order);    free (order);
# Line 1237  prepare (void) Line 1386  prepare (void)
1386    
1387    /* States. */    /* States. */
1388    MUSCLE_INSERT_INT ("last", high);    MUSCLE_INSERT_INT ("last", high);
   MUSCLE_INSERT_INT ("flag", SHRT_MIN);  
1389    MUSCLE_INSERT_INT ("final_state_number", final_state->number);    MUSCLE_INSERT_INT ("final_state_number", final_state->number);
1390    MUSCLE_INSERT_INT ("states_number", nstates);    MUSCLE_INSERT_INT ("states_number", nstates);
1391    

Legend:
Removed from v.1.185  
changed lines
  Added in v.1.186

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26