1 /* Output the generated parsing program for Bison. 
   2    Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002 
   3    Free Software Foundation, Inc. 
   5    This file is part of Bison, the GNU Compiler Compiler. 
   7    Bison is free software; you can redistribute it and/or modify it 
   8    under the terms of the GNU General Public License as published by 
   9    the Free Software Foundation; either version 2, or (at your option) 
  12    Bison is distributed in the hope that it will be useful, but 
  13    WITHOUT ANY WARRANTY; without even the implied warranty of 
  14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
  15    General Public License for more details. 
  17    You should have received a copy of the GNU General Public License 
  18    along with Bison; see the file COPYING.  If not, write to the Free 
  19    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
  33 #include "conflicts.h" 
  36 /* Several tables are indexed both by state and nonterminal numbers. 
  37    We call such an index a `vector'; i.e., a vector is either a state 
  38    or a nonterminal number. 
  40    Of course vector_number_t ought to be wide enough to contain 
  41    state_number_t and symbol_number_t.  */ 
  42 typedef short vector_number_t
; 
  43 #define VECTOR_NUMBER_MAX ((vector_number_t) SHRT_MAX) 
  44 #define VECTOR_NUMBER_MIN ((vector_number_t) SHRT_MIN) 
  45 #define state_number_to_vector_number(State) \ 
  46    ((vector_number_t) State) 
  47 #define symbol_number_to_vector_number(Symbol) \ 
  48    ((vector_number_t) (state_number_as_int (nstates) + Symbol - ntokens)) 
  53 /* FROMS and TOS are indexed by vector_number_t. 
  55    If VECTOR is a nonterminal, (FROMS[VECTOR], TOS[VECTOR]) form an 
  56    array of state numbers of the non defaulted GOTO on VECTOR. 
  58    If VECTOR is a state, TOS[VECTOR] is the array of actions to do on 
  59    the (array of) symbols FROMS[VECTOR]. 
  61    In both cases, TALLY[VECTOR] is the size of the arrays 
  62    FROMS[VECTOR], TOS[VECTOR]; and WIDTH[VECTOR] = 
  63    (FROMS[VECTOR][SIZE] - FROMS[VECTOR][0] + 1) where SIZE = 
  66    FROMS therefore contains symbol_number_t and action_number_t, 
  67    TOS state_number_t and action_number_t, 
  69    WIDTH differences of FROMS. 
  71    Let base_t be the type of FROMS, TOS, and WIDTH.  */ 
  72 #define BASE_MAX ((base_t) INT_MAX) 
  73 #define BASE_MIN ((base_t) INT_MIN) 
  75 static base_t 
**froms 
= NULL
; 
  76 static base_t 
**tos 
= NULL
; 
  77 static unsigned int **conflict_tos 
= NULL
; 
  78 static short *tally 
= NULL
; 
  79 static base_t 
*width 
= NULL
; 
  82 /* For a given state, N = ACTROW[SYMBOL]: 
  84    If N = 0, stands for `run the default action'. 
  85    If N = MIN, stands for `raise a syntax error'. 
  86    If N > 0, stands for `shift SYMBOL and go to n'. 
  87    If N < 0, stands for `reduce -N'.  */ 
  88 typedef short action_t
; 
  89 #define ACTION_MAX ((action_t) SHRT_MAX) 
  90 #define ACTION_MIN ((action_t) SHRT_MIN) 
  92 static action_t 
*actrow 
= NULL
; 
  94 /* FROMS and TOS are reordered to be compressed.  ORDER[VECTOR] is the 
  95    new vector number of VECTOR.  We skip `empty' vectors (i.e., 
  96    TALLY[VECTOR] = 0), and call these `entries'.  */ 
  97 static vector_number_t 
*order 
= NULL
; 
 101 /* A distinguished value of BASE, negative infinite.  During the 
 102    computation equals to BASE_MIN, later mapped to BASE_NINF to 
 103    keep parser tables small.  */ 
 104 base_t base_ninf 
= 0; 
 105 static base_t 
*pos 
= NULL
; 
 107 static unsigned int *conflrow 
= NULL
; 
 108 unsigned int *conflict_table 
= NULL
; 
 109 unsigned int *conflict_list 
= NULL
; 
 110 int conflict_list_cnt
; 
 111 static int conflict_list_free
; 
 113 /* TABLE_SIZE is the allocated size of both TABLE and CHECK.  We start 
 114    with more or less the original hard-coded value (which was 
 116 static size_t table_size 
= 32768; 
 117 base_t 
*table 
= NULL
; 
 118 base_t 
*check 
= NULL
; 
 119 /* The value used in TABLE to denote explicit syntax errors 
 120    (%nonassoc), a negative infinite.  First defaults to ACTION_MIN, 
 121    but in order to keep small tables, renumbered as TABLE_ERROR, which 
 122    is the smallest (non error) value minus 1.  */ 
 123 base_t table_ninf 
= 0; 
 127 state_number_t 
*yydefgoto
; 
 128 rule_number_t 
*yydefact
; 
 130 /*----------------------------------------------------------------. 
 131 | If TABLE (and CHECK) appear to be small to be addressed at      | 
 132 | DESIRED, grow them.  Note that TABLE[DESIRED] is to be used, so | 
 133 | the desired size is at least DESIRED + 1.                       | 
 134 `----------------------------------------------------------------*/ 
 137 table_grow (size_t desired
) 
 139   size_t old_size 
= table_size
; 
 141   while (table_size 
<= desired
) 
 144   if (trace_flag 
& trace_resource
) 
 145     fprintf (stderr
, "growing table and check from: %d to %d\n", 
 146              old_size
, table_size
); 
 148   table 
= XREALLOC (table
, base_t
, table_size
); 
 149   check 
= XREALLOC (check
, base_t
, table_size
); 
 150   conflict_table 
= XREALLOC (conflict_table
, unsigned int, table_size
); 
 152   for (/* Nothing. */; old_size 
< table_size
; ++old_size
) 
 155       check
[old_size
] = -1; 
 162 /*-------------------------------------------------------------------. 
 163 | For GLR parsers, for each conflicted token in STATE, as indicated  | 
 164 | by non-zero entries in CONFLROW, create a list of possible         | 
 165 | reductions that are alternatives to the shift or reduction         | 
 166 | currently recorded for that token in STATE.  Store the alternative | 
 167 | reductions followed by a 0 in CONFLICT_LIST, updating              | 
 168 | CONFLICT_LIST_CNT, and storing an index to the start of the list   | 
 169 | back into CONFLROW.                                                | 
 170 `-------------------------------------------------------------------*/ 
 173 conflict_row (state_t 
*state
) 
 176   reductions_t 
*reds 
= state
->reductions
; 
 181   for (j 
= 0; j 
< ntokens
; j 
+= 1) 
 184         conflrow
[j
] = conflict_list_cnt
; 
 186         /* Find all reductions for token J, and record all that do not 
 188         for (i 
= 0; i 
< reds
->num
; i 
+= 1) 
 189           if (bitset_test (reds
->lookaheads
[i
], j
) 
 191                   != rule_number_as_item_number (reds
->rules
[i
]->number
))) 
 193               if (conflict_list_free 
<= 0) 
 195               conflict_list
[conflict_list_cnt
] = reds
->rules
[i
]->number 
+ 1; 
 196               conflict_list_cnt 
+= 1; 
 197               conflict_list_free 
-= 1; 
 200         /* Leave a 0 at the end.  */ 
 201         if (conflict_list_free 
<= 0) 
 203         conflict_list_cnt 
+= 1; 
 204         conflict_list_free 
-= 1; 
 209 /*------------------------------------------------------------------. 
 210 | Decide what to do for each type of token if seen as the lookahead | 
 211 | token in specified state.  The value returned is used as the      | 
 212 | default action (yydefact) for the state.  In addition, ACTROW is  | 
 213 | filled with what to do for each kind of token, index by symbol    | 
 214 | number, with zero meaning do the default action.  The value       | 
 215 | ACTION_MIN, a very negative number, means this situation is an    | 
 216 | error.  The parser recognizes this value specially.               | 
 218 | This is where conflicts are resolved.  The loop over lookahead    | 
 219 | rules considered lower-numbered rules last, and the last rule     | 
 220 | considered that likes a token gets to handle it.                  | 
 222 | For GLR parsers, also sets CONFLROW[SYM] to an index into         | 
 223 | CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r)    | 
 224 | with symbol SYM. The default reduction is not used for a symbol   | 
 225 | that has any such conflicts.                                      | 
 226 `------------------------------------------------------------------*/ 
 229 action_row (state_t 
*state
) 
 232   rule_t 
*default_rule 
= NULL
; 
 233   reductions_t 
*redp 
= state
->reductions
; 
 234   transitions_t 
*transitions 
= state
->transitions
; 
 235   errs_t 
*errp 
= state
->errs
; 
 236   /* Set to nonzero to inhibit having any default reduction.  */ 
 240   for (i 
= 0; i 
< ntokens
; i
++) 
 241     actrow
[i
] = conflrow
[i
] = 0; 
 243   if (redp
->lookaheads
) 
 246       bitset_iterator biter
; 
 247       /* loop over all the rules available here which require 
 248          lookahead (in reverse order to give precedence to the first 
 250       for (i 
= redp
->num 
- 1; i 
>= 0; --i
) 
 251         /* and find each token which the rule finds acceptable 
 253         BITSET_FOR_EACH (biter
, redp
->lookaheads
[i
], j
, 0) 
 255           /* and record this rule as the rule to use if that 
 258             conflicted 
= conflrow
[j
] = 1; 
 259           actrow
[j
] = rule_number_as_item_number (redp
->rules
[i
]->number
); 
 263   /* Now see which tokens are allowed for shifts in this state.  For 
 264      them, record the shift as the thing to do.  So shift is preferred 
 266   FOR_EACH_SHIFT (transitions
, i
) 
 268       symbol_number_t symbol 
= TRANSITION_SYMBOL (transitions
, i
); 
 269       state_t 
*shift_state 
= transitions
->states
[i
]; 
 271       if (actrow
[symbol
] != 0) 
 272         conflicted 
= conflrow
[symbol
] = 1; 
 273       actrow
[symbol
] = state_number_as_int (shift_state
->number
); 
 275       /* Do not use any default reduction if there is a shift for 
 277       if (symbol 
== errtoken
->number
) 
 281   /* See which tokens are an explicit error in this state (due to 
 282      %nonassoc).  For them, record ACTION_MIN as the action.  */ 
 283   for (i 
= 0; i 
< errp
->num
; i
++) 
 285       symbol_t 
*symbol 
= errp
->symbols
[i
]; 
 286       actrow
[symbol
->number
] = ACTION_MIN
; 
 289   /* Now find the most common reduction and make it the default action 
 292   if (redp
->num 
>= 1 && !nodefault
) 
 294       if (state
->consistent
) 
 295         default_rule 
= redp
->rules
[0]; 
 299           for (i 
= 0; i 
< redp
->num
; i
++) 
 302               rule_t 
*rule 
= redp
->rules
[i
]; 
 305               for (j 
= 0; j 
< ntokens
; j
++) 
 306                 if (actrow
[j
] == rule_number_as_item_number (rule
->number
)) 
 316           /* GLR parsers need space for conflict lists, so we can't 
 317              default conflicted entries.  For non-conflicted entries 
 318              or as long as we are not building a GLR parser, 
 319              actions that match the default are replaced with zero, 
 320              which means "use the default". */ 
 325               for (j 
= 0; j 
< ntokens
; j
++) 
 326                 if (actrow
[j
] == rule_number_as_item_number (default_rule
->number
) 
 327                     && ! (glr_parser 
&& conflrow
[j
])) 
 333   /* If have no default rule, the default is an error. 
 334      So replace any action which says "error" with "use default".  */ 
 337     for (i 
= 0; i 
< ntokens
; i
++) 
 338       if (actrow
[i
] == ACTION_MIN
) 
 342     conflict_row (state
); 
 348 /*--------------------------------------------. 
 349 | Set FROMS, TOS, TALLY and WIDTH for STATE.  | 
 350 `--------------------------------------------*/ 
 353 save_row (state_number_t state
) 
 360   unsigned int *sp3 
= NULL
; 
 362   /* Number of non default actions in STATE.  */ 
 364   for (i 
= 0; i 
< ntokens
; i
++) 
 371   /* Allocate non defaulted actions.  */ 
 372   froms
[state
] = sp1 
= sp 
= XCALLOC (base_t
, count
); 
 373   tos
[state
] = sp2 
= XCALLOC (base_t
, count
); 
 375     conflict_tos
[state
] = sp3 
= XCALLOC (unsigned int, count
); 
 377     conflict_tos
[state
] = NULL
; 
 379   /* Store non defaulted actions.  */ 
 380   for (i 
= 0; i 
< ntokens
; i
++) 
 386           *sp3
++ = conflrow
[i
]; 
 389   tally
[state
] = count
; 
 390   width
[state
] = sp1
[-1] - sp
[0] + 1; 
 394 /*------------------------------------------------------------------. 
 395 | Figure out the actions for the specified state, indexed by        | 
 396 | lookahead token type.                                             | 
 398 | The YYDEFACT table is output now.  The detailed info is saved for | 
 399 | putting into YYTABLE later.                                       | 
 400 `------------------------------------------------------------------*/ 
 409   int nconflict 
= glr_parser 
? conflicts_total_count () : 0; 
 411   yydefact 
= XCALLOC (rule_number_t
, nstates
); 
 413   actrow 
= XCALLOC (action_t
, ntokens
); 
 414   conflrow 
= XCALLOC (unsigned int, ntokens
); 
 416   conflict_list 
= XCALLOC (unsigned int, 1 + 2 * nconflict
); 
 417   conflict_list_free 
= 2 * nconflict
; 
 418   conflict_list_cnt 
= 1; 
 420   /* Find the rules which are reduced.  */ 
 422     for (r 
= 0; r 
< nrules
; ++r
) 
 423       rules
[r
].useful 
= false; 
 425   for (i 
= 0; i 
< nstates
; ++i
) 
 427       rule_t 
*default_rule 
= action_row (states
[i
]); 
 428       yydefact
[i
] = default_rule 
? default_rule
->number 
+ 1 : 0; 
 431       /* Now that the parser was computed, we can find which rules are 
 432          really reduced, and which are not because of SR or RR 
 436           for (j 
= 0; j 
< ntokens
; ++j
) 
 437             if (actrow
[j
] < 0 && actrow
[j
] != ACTION_MIN
) 
 438               rules
[item_number_as_rule_number (actrow
[j
])].useful 
= true; 
 440             rules
[yydefact
[i
] - 1].useful 
= true; 
 449 /*------------------------------------------------------------------. 
 450 | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], | 
 451 | i.e., the information related to non defaulted GOTO on the nterm  | 
 454 | DEFAULT_STATE is the principal destination on SYMBOL, i.e., the   | 
 455 | default GOTO destination on SYMBOL.                               | 
 456 `------------------------------------------------------------------*/ 
 459 save_column (symbol_number_t symbol
, state_number_t default_state
) 
 466   vector_number_t symno 
= symbol_number_to_vector_number (symbol
); 
 468   goto_number_t begin 
= goto_map
[symbol
]; 
 469   goto_number_t end 
= goto_map
[symbol 
+ 1]; 
 471   /* Number of non default GOTO.  */ 
 473   for (i 
= begin
; i 
< end
; i
++) 
 474     if (to_state
[i
] != default_state
) 
 480   /* Allocate room for non defaulted gotos.  */ 
 481   froms
[symno
] = sp1 
= sp 
= XCALLOC (base_t
, count
); 
 482   tos
[symno
] = sp2 
= XCALLOC (base_t
, count
); 
 484   /* Store the state numbers of the non defaulted gotos.  */ 
 485   for (i 
= begin
; i 
< end
; i
++) 
 486     if (to_state
[i
] != default_state
) 
 488         *sp1
++ = from_state
[i
]; 
 489         *sp2
++ = to_state
[i
]; 
 492   tally
[symno
] = count
; 
 493   width
[symno
] = sp1
[-1] - sp
[0] + 1; 
 497 /*----------------------------------------------------------------. 
 498 | Return `the' most common destination GOTO on SYMBOL (a nterm).  | 
 499 `----------------------------------------------------------------*/ 
 501 static state_number_t
 
 502 default_goto (symbol_number_t symbol
, short state_count
[]) 
 506   goto_number_t m 
= goto_map
[symbol
]; 
 507   goto_number_t n 
= goto_map
[symbol 
+ 1]; 
 508   state_number_t default_state 
= (state_number_t
) -1; 
 512     return (state_number_t
) -1; 
 514   for (s 
= 0; s 
< nstates
; s
++) 
 517   for (i 
= m
; i 
< n
; i
++) 
 518     state_count
[to_state
[i
]]++; 
 520   for (s 
= 0; s 
< nstates
; s
++) 
 521     if (state_count
[s
] > max
) 
 523         max 
= state_count
[s
]; 
 527   return default_state
; 
 531 /*-------------------------------------------------------------------. 
 532 | Figure out what to do after reducing with each rule, depending on  | 
 533 | the saved state from before the beginning of parsing the data that | 
 534 | matched this rule.                                                 | 
 536 | The YYDEFGOTO table is output now.  The detailed info is saved for | 
 537 | putting into YYTABLE later.                                        | 
 538 `-------------------------------------------------------------------*/ 
 544   short *state_count 
= XCALLOC (short, nstates
); 
 545   yydefgoto 
= XMALLOC (state_number_t
, nvars
); 
 547   /* For a given nterm I, STATE_COUNT[S] is the number of times there 
 548      is a GOTO to S on I.  */ 
 549   for (i 
= ntokens
; i 
< nsyms
; ++i
) 
 551       state_number_t default_state 
= default_goto (i
, state_count
); 
 552       save_column (i
, default_state
); 
 553       yydefgoto
[i 
- ntokens
] = default_state
; 
 559 /*------------------------------------------------------------------. 
 560 | Compute ORDER, a reordering of vectors, in order to decide how to | 
 561 | pack the actions and gotos information into yytable.              | 
 562 `------------------------------------------------------------------*/ 
 571   for (i 
= 0; i 
< nvectors
; i
++) 
 577         int j 
= nentries 
- 1; 
 579         while (j 
>= 0 && (width
[order
[j
]] < w
)) 
 582         while (j 
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
)) 
 585         for (k 
= nentries 
- 1; k 
> j
; k
--) 
 586           order
[k 
+ 1] = order
[k
]; 
 594 /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY 
 595    and WIDTH of VECTOR) are common to a previous state, return this 
 598    In any other case, return -1.  */ 
 600 static state_number_t
 
 601 matching_state (vector_number_t vector
) 
 603   vector_number_t i 
= order
[vector
]; 
 608   /* If VECTOR is a nterm, return -1.  */ 
 609   if (i 
>= (int) nstates
) 
 615   /* If VECTOR has GLR conflicts, return -1 */ 
 616   if (conflict_tos
[i
] != NULL
) 
 619       for (j 
= 0; j 
< t
; j 
+= 1) 
 620         if (conflict_tos
[i
][j
] != 0) 
 624   for (prev 
= vector 
- 1; prev 
>= 0; prev
--) 
 626       vector_number_t j 
= order
[prev
]; 
 630       /* Given how ORDER was computed, if the WIDTH or TALLY is 
 631          different, there cannot be a matching state.  */ 
 632       if (width
[j
] != w 
|| tally
[j
] != t
) 
 635       for (k 
= 0; match 
&& k 
< t
; k
++) 
 636         if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
] 
 637             || (conflict_tos
[j
] != NULL 
&& conflict_tos
[j
][k
] != 0)) 
 649 pack_vector (vector_number_t vector
) 
 651   vector_number_t i 
= order
[vector
]; 
 655   base_t 
*from 
= froms
[i
]; 
 657   unsigned int *conflict_to 
= conflict_tos
[i
]; 
 662   for (j 
= lowzero 
- from
[0]; ; j
++) 
 667       if ((int) table_size 
<= j
) 
 670       for (k 
= 0; ok 
&& k 
< t
; k
++) 
 672           loc 
= j 
+ state_number_as_int (from
[k
]); 
 673           if (loc 
>= (int) table_size
) 
 680       for (k 
= 0; ok 
&& k 
< vector
; k
++) 
 686           for (k 
= 0; k 
< t
; k
++) 
 690               if (glr_parser 
&& conflict_to 
!= NULL
) 
 691                 conflict_table
[loc
] = conflict_to
[k
]; 
 692               check
[loc
] = from
[k
]; 
 695           while (table
[lowzero
] != 0) 
 701           if (! (BASE_MIN 
<= j 
&& j 
<= BASE_MAX
)) 
 709 /*-------------------------------------------------------------. 
 710 | Remap the negative infinite in TAB from NINF to the greatest | 
 711 | possible smallest value.  Return it.                         | 
 713 | In most case this allows us to use shorts instead of ints in | 
 715 `-------------------------------------------------------------*/ 
 718 table_ninf_remap (base_t tab
[], size_t size
, base_t ninf
) 
 723   for (i 
= 0; i 
< size
; i
++) 
 724     if (tab
[i
] < res 
&& tab
[i
] != ninf
) 
 729   for (i 
= 0; i 
< size
; i
++) 
 741   base 
= XCALLOC (base_t
, nvectors
); 
 742   pos 
= XCALLOC (base_t
, nentries
); 
 743   table 
= XCALLOC (base_t
, table_size
); 
 744   conflict_table 
= XCALLOC (unsigned int, table_size
); 
 745   check 
= XCALLOC (base_t
, table_size
); 
 750   for (i 
= 0; i 
< nvectors
; i
++) 
 753   for (i 
= 0; i 
< (int) table_size
; i
++) 
 756   for (i 
= 0; i 
< nentries
; i
++) 
 758       state_number_t state 
= matching_state (i
); 
 762         /* A new set of state actions, or a nonterminal.  */ 
 763         place 
= pack_vector (i
); 
 765         /* Action of I were already coded for STATE.  */ 
 769       base
[order
[i
]] = place
; 
 772   /* Use the greatest possible negative infinites.  */ 
 773   base_ninf 
= table_ninf_remap (base
, nvectors
, BASE_MIN
); 
 774   table_ninf 
= table_ninf_remap (table
, high 
+ 1, ACTION_MIN
); 
 781 /*-----------------------------------------------------------------. 
 782 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable | 
 784 `-----------------------------------------------------------------*/ 
 787 tables_generate (void) 
 791   /* This is a poor way to make sure the sizes are properly 
 792      correlated.  In particular the signedness is not taken into 
 793      account.  But it's not useless.  */ 
 794   verify (sizes_are_properly_correlated
, 
 795           (sizeof nstates 
<= sizeof nvectors
 
 796            && sizeof nvars 
<= sizeof nvectors
)); 
 798   nvectors 
= state_number_as_int (nstates
) + nvars
; 
 800   froms 
= XCALLOC (base_t 
*, nvectors
); 
 801   tos 
= XCALLOC (base_t 
*, nvectors
); 
 802   conflict_tos 
= XCALLOC (unsigned int *, nvectors
); 
 803   tally 
= XCALLOC (short, nvectors
); 
 804   width 
= XCALLOC (base_t
, nvectors
); 
 809   free (goto_map 
+ ntokens
); 
 813   order 
= XCALLOC (vector_number_t
, nvectors
); 
 821   for (i 
= 0; i 
< nvectors
; i
++) 
 825       XFREE (conflict_tos
[i
]); 
 834 /*-------------------------. 
 835 | Free the parser tables.  | 
 836 `-------------------------*/ 
 842   free (conflict_table
); 
 843   free (conflict_list
);