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

Diff of /bison/src/conflicts.c

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

revision 1.57 by akim, Sat Dec 22 14:42:35 2001 UTC revision 1.58 by akim, Thu Dec 27 18:05:05 2001 UTC
# Line 38  static unsigned *lookaheadset = NULL; Line 38  static unsigned *lookaheadset = NULL;
38    
39    
40  static inline void  static inline void
41  log_resolution (int state, int LAno, int token, char *resolution)  log_resolution (state_t *state, int LAno, int token, char *resolution)
42  {  {
43    if (verbose_flag)    if (verbose_flag)
44      obstack_fgrow4 (&output_obstack,      obstack_fgrow4 (&output_obstack,
45                      _("\                      _("\
46  Conflict in state %d between rule %d and token %s resolved as %s.\n"),  Conflict in state %d between rule %d and token %s resolved as %s.\n"),
47                      state, LAruleno[LAno], tags[token], resolution);                      state->number, LAruleno[LAno], tags[token], resolution);
48  }  }
49    
50    
# Line 55  Conflict in state %d between rule %d and Line 55  Conflict in state %d between rule %d and
55  `------------------------------------------------------------------*/  `------------------------------------------------------------------*/
56    
57  static void  static void
58  flush_shift (int state, int token)  flush_shift (state_t *state, int token)
59  {  {
60    shifts *shiftp = state_table[state]->shifts;    shifts *shiftp = state->shifts;
61    int i;    int i;
62    
63    RESETBIT (lookaheadset, token);    RESETBIT (lookaheadset, token);
# Line 88  flush_reduce (int lookahead, int token) Line 88  flush_reduce (int lookahead, int token)
88  `------------------------------------------------------------------*/  `------------------------------------------------------------------*/
89    
90  static void  static void
91  resolve_sr_conflict (int state, int lookahead)  resolve_sr_conflict (state_t *state, int lookahead)
92  {  {
93    int i;    int i;
94    /* find the rule to reduce by to get precedence of reduction  */    /* find the rule to reduce by to get precedence of reduction  */
# Line 146  resolve_sr_conflict (int state, int look Line 146  resolve_sr_conflict (int state, int look
146    /* Some tokens have been explicitly made errors.  Allocate a    /* Some tokens have been explicitly made errors.  Allocate a
147       permanent errs structure for this state, to record them.  */       permanent errs structure for this state, to record them.  */
148    i = (char *) errtokens - (char *) errp;    i = (char *) errtokens - (char *) errp;
149    state_table[state]->errs = ERRS_ALLOC (i + 1);    state->errs = ERRS_ALLOC (i + 1);
150    memcpy (state_table[state]->errs, errp, i);    memcpy (state->errs, errp, i);
151    free (errp);    free (errp);
152  }  }
153    
154    
155  static void  static void
156  set_conflicts (int state)  set_conflicts (state_t *state)
157  {  {
158    int i, j;    int i, j;
159    shifts *shiftp;    shifts *shiftp;
160    
161    if (state_table[state]->consistent)    if (state->consistent)
162      return;      return;
163    
164    for (i = 0; i < tokensetsize; i++)    for (i = 0; i < tokensetsize; i++)
165      lookaheadset[i] = 0;      lookaheadset[i] = 0;
166    
167    shiftp = state_table[state]->shifts;    shiftp = state->shifts;
168    for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)    for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
169      if (!SHIFT_IS_DISABLED (shiftp, i))      if (!SHIFT_IS_DISABLED (shiftp, i))
170        SETBIT (lookaheadset, SHIFT_SYMBOL (shiftp, i));        SETBIT (lookaheadset, SHIFT_SYMBOL (shiftp, i));
# Line 172  set_conflicts (int state) Line 172  set_conflicts (int state)
172    /* Loop over all rules which require lookahead in this state.  First    /* Loop over all rules which require lookahead in this state.  First
173       check for shift-reduce conflict, and try to resolve using       check for shift-reduce conflict, and try to resolve using
174       precedence */       precedence */
175    for (i = state_table[state]->lookaheadsp;    for (i = 0; i < state->nlookaheads; ++i)
176         i < state_table[state + 1]->lookaheadsp;      if (rule_table[LAruleno[state->lookaheadsp + i]].prec)
        ++i)  
     if (rule_table[LAruleno[i]].prec)  
177        for (j = 0; j < tokensetsize; ++j)        for (j = 0; j < tokensetsize; ++j)
178          if (LA (i)[j] & lookaheadset[j])          if (LA (state->lookaheadsp + i)[j] & lookaheadset[j])
179            {            {
180              resolve_sr_conflict (state, i);              resolve_sr_conflict (state, state->lookaheadsp + i);
181              break;              break;
182            }            }
183    
184    
185    /* Loop over all rules which require lookahead in this state.  Check    /* Loop over all rules which require lookahead in this state.  Check
186       for conflicts not resolved above.  */       for conflicts not resolved above.  */
187    for (i = state_table[state]->lookaheadsp;    for (i = 0; i < state->nlookaheads; ++i)
        i < state_table[state + 1]->lookaheadsp;  
        ++i)  
188      {      {
189        for (j = 0; j < tokensetsize; ++j)        for (j = 0; j < tokensetsize; ++j)
190          if (LA (i)[j] & lookaheadset[j])          if (LA (state->lookaheadsp + i)[j] & lookaheadset[j])
191            conflicts[state] = 1;            conflicts[state->number] = 1;
192    
193        for (j = 0; j < tokensetsize; ++j)        for (j = 0; j < tokensetsize; ++j)
194          lookaheadset[j] |= LA (i)[j];          lookaheadset[j] |= LA (state->lookaheadsp + i)[j];
195      }      }
196  }  }
197    
# Line 209  solve_conflicts (void) Line 205  solve_conflicts (void)
205    lookaheadset = XCALLOC (unsigned, tokensetsize);    lookaheadset = XCALLOC (unsigned, tokensetsize);
206    
207    for (i = 0; i < nstates; i++)    for (i = 0; i < nstates; i++)
208      set_conflicts (i);      set_conflicts (state_table[i]);
209  }  }
210    
211    
# Line 218  solve_conflicts (void) Line 214  solve_conflicts (void)
214  `---------------------------------------------*/  `---------------------------------------------*/
215    
216  static int  static int
217  count_sr_conflicts (int state)  count_sr_conflicts (state_t *state)
218  {  {
219    int i, k;    int i, k;
220    int src_count = 0;    int src_count = 0;
221    shifts *shiftp = state_table[state]->shifts;    shifts *shiftp = state->shifts;
222    
223    if (!shiftp)    if (!shiftp)
224      return 0;      return 0;
# Line 237  count_sr_conflicts (int state) Line 233  count_sr_conflicts (int state)
233      if (!SHIFT_IS_DISABLED (shiftp, i))      if (!SHIFT_IS_DISABLED (shiftp, i))
234        SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));        SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
235    
236    for (i = state_table[state]->lookaheadsp;    for (i = 0; i < state->nlookaheads; ++i)
        i < state_table[state + 1]->lookaheadsp;  
        ++i)  
237      for (k = 0; k < tokensetsize; ++k)      for (k = 0; k < tokensetsize; ++k)
238        lookaheadset[k] |= LA (i)[k];        lookaheadset[k] |= LA (state->lookaheadsp + i)[k];
239    
240    for (k = 0; k < tokensetsize; ++k)    for (k = 0; k < tokensetsize; ++k)
241      lookaheadset[k] &= shiftset[k];      lookaheadset[k] &= shiftset[k];
# Line 259  count_sr_conflicts (int state) Line 253  count_sr_conflicts (int state)
253  `----------------------------------------------*/  `----------------------------------------------*/
254    
255  static int  static int
256  count_rr_conflicts (int state)  count_rr_conflicts (state_t *state)
257  {  {
258    int i;    int i;
259    int rrc_count = 0;    int rrc_count = 0;
260    
261    int m = state_table[state]->lookaheadsp;    if (state->nlookaheads < 2)
   int n = state_table[state + 1]->lookaheadsp;  
   
   if (n - m < 2)  
262      return 0;      return 0;
263    
264    for (i = 0; i < ntokens; i++)    for (i = 0; i < ntokens; i++)
265      {      {
266        int count = 0;        int count = 0;
267        int j;        int j;
268        for (j = m; j < n; j++)        for (j = 0; j < state->nlookaheads; ++j)
269          if (BITISSET (LA (m), j))          if (BITISSET (LA (state->lookaheadsp), state->lookaheadsp + j))
270            count++;            count++;
271    
272        if (count >= 2)        if (count >= 2)
# Line 337  conflicts_output (FILE *out) Line 328  conflicts_output (FILE *out)
328      if (conflicts[i])      if (conflicts[i])
329        {        {
330          fprintf (out, _("State %d contains "), i);          fprintf (out, _("State %d contains "), i);
331          fputs (conflict_report (count_sr_conflicts (i),          fputs (conflict_report (count_sr_conflicts (state_table[i]),
332                                  count_rr_conflicts (i)), out);                                  count_rr_conflicts (state_table[i])), out);
333          printed_sth = TRUE;          printed_sth = TRUE;
334        }        }
335    if (printed_sth)    if (printed_sth)
# Line 367  conflicts_print (void) Line 358  conflicts_print (void)
358    for (i = 0; i < nstates; i++)    for (i = 0; i < nstates; i++)
359      if (conflicts[i])      if (conflicts[i])
360        {        {
361          src_total += count_sr_conflicts (i);          src_total += count_sr_conflicts (state_table[i]);
362          rrc_total += count_rr_conflicts (i);          rrc_total += count_rr_conflicts (state_table[i]);
363        }        }
364    
365    src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);    src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
# Line 410  conflicts_print (void) Line 401  conflicts_print (void)
401    
402    
403  void  void
404  print_reductions (FILE *out, int state)  print_reductions (FILE *out, state_t *state)
405  {  {
406    int i;    int i;
407    int m = state_table[state]->lookaheadsp;    shifts *shiftp = state->shifts;
408    int n = state_table[state + 1]->lookaheadsp;    errs *errp = state->errs;
   shifts *shiftp = state_table[state]->shifts;  
   errs *errp = state_table[state]->errs;  
409    int nodefault = 0;    int nodefault = 0;
410    
411    for (i = 0; i < tokensetsize; i++)    for (i = 0; i < tokensetsize; i++)
# Line 437  print_reductions (FILE *out, int state) Line 426  print_reductions (FILE *out, int state)
426        if (errp->errs[i])        if (errp->errs[i])
427          SETBIT (shiftset, errp->errs[i]);          SETBIT (shiftset, errp->errs[i]);
428    
429    if (n - m == 1 && !nodefault)    if (state->nlookaheads == 1 && !nodefault)
430      {      {
431        int k;        int k;
432        int default_rule = LAruleno[m];        int default_rule = LAruleno[state->lookaheadsp];
433    
434        for (k = 0; k < tokensetsize; ++k)        for (k = 0; k < tokensetsize; ++k)
435          lookaheadset[k] = LA (m)[k] & shiftset[k];          lookaheadset[k] = LA (state->lookaheadsp)[k] & shiftset[k];
436    
437        for (i = 0; i < ntokens; i++)        for (i = 0; i < ntokens; i++)
438          if (BITISSET (lookaheadset, i))          if (BITISSET (lookaheadset, i))
# Line 454  print_reductions (FILE *out, int state) Line 443  print_reductions (FILE *out, int state)
443        fprintf (out, _("    $default\treduce using rule %d (%s)\n\n"),        fprintf (out, _("    $default\treduce using rule %d (%s)\n\n"),
444                 default_rule, tags[rule_table[default_rule].lhs]);                 default_rule, tags[rule_table[default_rule].lhs]);
445      }      }
446    else if (n - m >= 1)    else if (state->nlookaheads >= 1)
447      {      {
448        int cmax = 0;        int cmax = 0;
449        int default_LA = -1;        int default_LA = -1;
450        int default_rule = 0;        int default_rule = 0;
451    
452        if (!nodefault)        if (!nodefault)
453          for (i = m; i < n; i++)          for (i = 0; i < state->nlookaheads; ++i)
454            {            {
455              int count = 0;              int count = 0;
456              int j, k;              int j, k;
457    
458              for (k = 0; k < tokensetsize; ++k)              for (k = 0; k < tokensetsize; ++k)
459                lookaheadset[k] = LA (i)[k] & ~shiftset[k];                lookaheadset[k] = LA (state->lookaheadsp + i)[k] & ~shiftset[k];
460    
461              for (j = 0; j < ntokens; j++)              for (j = 0; j < ntokens; j++)
462                if (BITISSET (lookaheadset, j))                if (BITISSET (lookaheadset, j))
# Line 476  print_reductions (FILE *out, int state) Line 465  print_reductions (FILE *out, int state)
465              if (count > cmax)              if (count > cmax)
466                {                {
467                  cmax = count;                  cmax = count;
468                  default_LA = i;                  default_LA = state->lookaheadsp + i;
469                  default_rule = LAruleno[i];                  default_rule = LAruleno[state->lookaheadsp + i];
470                }                }
471    
472              for (k = 0; k < tokensetsize; ++k)              for (k = 0; k < tokensetsize; ++k)
# Line 497  print_reductions (FILE *out, int state) Line 486  print_reductions (FILE *out, int state)
486            int defaulted = 0;            int defaulted = 0;
487            int count = BITISSET (shiftset, i);            int count = BITISSET (shiftset, i);
488    
489            for (j = m; j < n; j++)            for (j = 0; j < state->nlookaheads; ++j)
490              {              {
491                if (BITISSET (LA (j), i))                if (BITISSET (LA (state->lookaheadsp + j), i))
492                  {                  {
493                    if (count == 0)                    if (count == 0)
494                      {                      {
495                        if (j != default_LA)                        if (state->lookaheadsp + j != default_LA)
496                          fprintf (out,                          fprintf (out,
497                                   _("    %-4s\treduce using rule %d (%s)\n"),                                   _("    %-4s\treduce using rule %d (%s)\n"),
498                                   tags[i],                                   tags[i],
499                                   LAruleno[j],                                   LAruleno[state->lookaheadsp + j],
500                                   tags[rule_table[LAruleno[j]].lhs]);                                   tags[rule_table[LAruleno[state->lookaheadsp + j]].lhs]);
501                        else                        else
502                          defaulted = 1;                          defaulted = 1;
503    
# Line 526  print_reductions (FILE *out, int state) Line 515  print_reductions (FILE *out, int state)
515                        fprintf (out,                        fprintf (out,
516                                 _("    %-4s\t[reduce using rule %d (%s)]\n"),                                 _("    %-4s\t[reduce using rule %d (%s)]\n"),
517                                 tags[i],                                 tags[i],
518                                 LAruleno[j],                                 LAruleno[state->lookaheadsp + j],
519                                 tags[rule_table[LAruleno[j]].lhs]);                                 tags[rule_table[LAruleno[state->lookaheadsp + j]].lhs]);
520                      }                      }
521                  }                  }
522              }              }

Legend:
Removed from v.1.57  
changed lines
  Added in v.1.58

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