/[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.80 by akim, Sat Jun 15 18:23:50 2002 UTC revision 1.81 by akim, Sat Jun 15 18:24:08 2002 UTC
# Line 52  enum conflict_resolution_e Line 52  enum conflict_resolution_e
52    };    };
53    
54    
55    /*----------------------------------------------------------------.
56    | Explain how an SR conflict between TOKEN and RULE was resolved: |
57    | RESOLUTION.                                                     |
58    `----------------------------------------------------------------*/
59    
60  static inline void  static inline void
61  log_resolution (int lookahead, int token,  log_resolution (rule_t *rule, int token,
62                  enum conflict_resolution_e resolution)                  enum conflict_resolution_e resolution)
63  {  {
64    if (report_flag & report_solved_conflicts)    if (report_flag & report_solved_conflicts)
# Line 66  log_resolution (int lookahead, int token Line 71  log_resolution (int lookahead, int token
71            obstack_fgrow2 (&solved_conflicts_obstack,            obstack_fgrow2 (&solved_conflicts_obstack,
72                            _("\                            _("\
73      Conflict between rule %d and token %s resolved as shift"),      Conflict between rule %d and token %s resolved as shift"),
74                            LArule[lookahead]->number,                            rule->number,
75                            symbols[token]->tag);                            symbols[token]->tag);
76            break;            break;
77          case reduce_resolution:          case reduce_resolution:
# Line 74  log_resolution (int lookahead, int token Line 79  log_resolution (int lookahead, int token
79            obstack_fgrow2 (&solved_conflicts_obstack,            obstack_fgrow2 (&solved_conflicts_obstack,
80                            _("\                            _("\
81      Conflict between rule %d and token %s resolved as reduce"),      Conflict between rule %d and token %s resolved as reduce"),
82                            LArule[lookahead]->number,                            rule->number,
83                            symbols[token]->tag);                            symbols[token]->tag);
84            break;            break;
85          case nonassoc_resolution:          case nonassoc_resolution:
86            obstack_fgrow2 (&solved_conflicts_obstack,            obstack_fgrow2 (&solved_conflicts_obstack,
87                            _("\                            _("\
88      Conflict between rule %d and token %s resolved as an error"),      Conflict between rule %d and token %s resolved as an error"),
89                            LArule[lookahead]->number,                            rule->number,
90                            symbols[token]->tag);                            symbols[token]->tag);
91            break;            break;
92          }          }
# Line 92  log_resolution (int lookahead, int token Line 97  log_resolution (int lookahead, int token
97          case shift_resolution:          case shift_resolution:
98            obstack_fgrow2 (&solved_conflicts_obstack,            obstack_fgrow2 (&solved_conflicts_obstack,
99                            " (%s < %s)",                            " (%s < %s)",
100                            LArule[lookahead]->prec->tag,                            rule->prec->tag,
101                            symbols[token]->tag);                            symbols[token]->tag);
102            break;            break;
103    
# Line 100  log_resolution (int lookahead, int token Line 105  log_resolution (int lookahead, int token
105            obstack_fgrow2 (&solved_conflicts_obstack,            obstack_fgrow2 (&solved_conflicts_obstack,
106                            " (%s < %s)",                            " (%s < %s)",
107                            symbols[token]->tag,                            symbols[token]->tag,
108                            LArule[lookahead]->prec->tag);                            rule->prec->tag);
109            break;            break;
110    
111          case left_resolution:          case left_resolution:
# Line 151  flush_shift (state_t *state, int token) Line 156  flush_shift (state_t *state, int token)
156  `-------------------------------------------------------------------*/  `-------------------------------------------------------------------*/
157    
158  static void  static void
159  flush_reduce (int lookahead, int token)  flush_reduce (bitset lookaheads, int token)
160  {  {
161    bitset_reset (LA[lookahead], token);    bitset_reset (lookaheads, token);
162  }  }
163    
164    
# Line 162  flush_reduce (int lookahead, int token) Line 167  flush_reduce (int lookahead, int token)
167  | precedence declarations.  It has already been checked that the    |  | precedence declarations.  It has already been checked that the    |
168  | rule has a precedence.  A conflict is resolved by modifying the   |  | rule has a precedence.  A conflict is resolved by modifying the   |
169  | shift or reduce tables so that there is no longer a conflict.     |  | shift or reduce tables so that there is no longer a conflict.     |
170    |                                                                   |
171    | LOOKAHEAD is the number of the lookahead bitset to consider.      |
172  `------------------------------------------------------------------*/  `------------------------------------------------------------------*/
173    
174  static void  static void
175  resolve_sr_conflict (state_t *state, int lookahead)  resolve_sr_conflict (state_t *state, int lookahead)
176  {  {
177    int i;    int i;
178    /* find the rule to reduce by to get precedence of reduction  */    /* Find the rule to reduce by to get precedence of reduction.  */
179    int redprec = LArule[lookahead]->prec->prec;    rule_t *redrule = state->lookaheads_rule[lookahead];
180      int redprec = redrule->prec->prec;
181      bitset lookaheads = state->lookaheads[lookahead];
182    errs *errp = errs_new (ntokens + 1);    errs *errp = errs_new (ntokens + 1);
183    errp->nerrs = 0;    errp->nerrs = 0;
184    
185    for (i = 0; i < ntokens; i++)    for (i = 0; i < ntokens; i++)
186      if (bitset_test (LA[lookahead], i)      if (bitset_test (lookaheads, i)
187          && bitset_test (lookaheadset, i)          && bitset_test (lookaheadset, i)
188          && symbols[i]->prec)          && symbols[i]->prec)
189        {        {
# Line 183  resolve_sr_conflict (state_t *state, int Line 192  resolve_sr_conflict (state_t *state, int
192             The precedence of shifting is that of token i.  */             The precedence of shifting is that of token i.  */
193          if (symbols[i]->prec < redprec)          if (symbols[i]->prec < redprec)
194            {            {
195              log_resolution (lookahead, i, reduce_resolution);              log_resolution (redrule, i, reduce_resolution);
196              flush_shift (state, i);              flush_shift (state, i);
197            }            }
198          else if (symbols[i]->prec > redprec)          else if (symbols[i]->prec > redprec)
199            {            {
200              log_resolution (lookahead, i, shift_resolution);              log_resolution (redrule, i, shift_resolution);
201              flush_reduce (lookahead, i);              flush_reduce (lookaheads, i);
202            }            }
203          else          else
204            /* Matching precedence levels.            /* Matching precedence levels.
# Line 200  resolve_sr_conflict (state_t *state, int Line 209  resolve_sr_conflict (state_t *state, int
209            switch (symbols[i]->assoc)            switch (symbols[i]->assoc)
210              {              {
211              case right_assoc:              case right_assoc:
212                log_resolution (lookahead, i, right_resolution);                log_resolution (redrule, i, right_resolution);
213                flush_reduce (lookahead, i);                flush_reduce (lookaheads, i);
214                break;                break;
215    
216              case left_assoc:              case left_assoc:
217                log_resolution (lookahead, i, left_resolution);                log_resolution (redrule, i, left_resolution);
218                flush_shift (state, i);                flush_shift (state, i);
219                break;                break;
220    
221              case non_assoc:              case non_assoc:
222                log_resolution (lookahead, i, nonassoc_resolution);                log_resolution (redrule, i, nonassoc_resolution);
223                flush_shift (state, i);                flush_shift (state, i);
224                flush_reduce (lookahead, i);                flush_reduce (lookaheads, i);
225                /* Record an explicit error for this token.  */                /* Record an explicit error for this token.  */
226                errp->errs[errp->nerrs++] = i;                errp->errs[errp->nerrs++] = i;
227                break;                break;
# Line 254  set_conflicts (state_t *state) Line 263  set_conflicts (state_t *state)
263    
264    /* Loop over all rules which require lookahead in this state.  First    /* Loop over all rules which require lookahead in this state.  First
265       check for shift-reduce conflict, and try to resolve using       check for shift-reduce conflict, and try to resolve using
266       precedence */       precedence.  */
267    for (i = 0; i < state->nlookaheads; ++i)    for (i = 0; i < state->nlookaheads; ++i)
268      if (state->lookaheads_rule[i]->prec      if (state->lookaheads_rule[i]->prec
269          && state->lookaheads_rule[i]->prec->prec          && state->lookaheads_rule[i]->prec->prec
270          && !bitset_disjoint_p (state->lookaheads[i], lookaheadset))          && !bitset_disjoint_p (state->lookaheads[i], lookaheadset))
271        {        {
272          resolve_sr_conflict (state, (state->lookaheads - LA) + i);          resolve_sr_conflict (state, i);
273          break;          break;
274        }        }
275    

Legend:
Removed from v.1.80  
changed lines
  Added in v.1.81

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