]>
git.saurik.com Git - bison.git/blob - src/lalr.c
   1 /* Compute look-ahead criteria for bison, 
   2    Copyright (C) 1984, 1986, 1989, 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 
   8    it 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, 
  13    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  15    GNU 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 
  19    the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
  20    Boston, MA 02111-1307, USA.  */ 
  23 /* Compute how to make the finite state machine deterministic; find 
  24    which rules need lookahead in each state, and which lookahead 
  25    tokens they accept.  */ 
  42 goto_number_t 
*goto_map 
= NULL
; 
  43 static goto_number_t ngotos 
= 0; 
  44 state_number_t 
*from_state 
= NULL
; 
  45 state_number_t 
*to_state 
= NULL
; 
  47 /* Linked list of goto numbers.  */ 
  48 typedef struct goto_list_s
 
  50   struct goto_list_s 
*next
; 
  55 rule_t 
**LArule 
= NULL
; 
  60 /* And for the famous F variable, which name is so descriptive that a 
  61    comment is hardly needed.  <grin>.  */ 
  62 static bitsetv F 
= NULL
; 
  64 static goto_number_t 
**includes
; 
  65 static goto_list_t 
**lookback
; 
  77   /* Avoid having to special case 0.  */ 
  81   LA 
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
); 
  82   LArule 
= XCALLOC (rule_t 
*, nLA
); 
  83   lookback 
= XCALLOC (goto_list_t 
*, nLA
); 
  86   for (i 
= 0; i 
< nstates
; i
++) 
  87     if (!states
[i
]->consistent
) 
  88       for (j 
= 0; j 
< states
[i
]->reductions
->num
; j
++) 
  89         *np
++ = &rules
[states
[i
]->reductions
->rules
[j
]]; 
  97   goto_number_t 
*temp_map
; 
  99   goto_map 
= XCALLOC (goto_number_t
, nvars 
+ 1) - ntokens
; 
 100   temp_map 
= XCALLOC (goto_number_t
, nvars 
+ 1) - ntokens
; 
 103   for (state 
= 0; state 
< nstates
; ++state
) 
 105       transitions_t 
*sp 
= states
[state
]->transitions
; 
 107       for (i 
= sp
->num 
- 1; i 
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
) 
 109           if (ngotos 
== GOTO_NUMBER_MAX
) 
 110             fatal (_("too many gotos (max %d)"), GOTO_NUMBER_MAX
); 
 113           goto_map
[TRANSITION_SYMBOL (sp
, i
)]++; 
 120     for (i 
= ntokens
; i 
< nsyms
; i
++) 
 126     for (i 
= ntokens
; i 
< nsyms
; i
++) 
 127       goto_map
[i
] = temp_map
[i
]; 
 129     goto_map
[nsyms
] = ngotos
; 
 130     temp_map
[nsyms
] = ngotos
; 
 133   from_state 
= XCALLOC (state_number_t
, ngotos
); 
 134   to_state 
= XCALLOC (state_number_t
, ngotos
); 
 136   for (state 
= 0; state 
< nstates
; ++state
) 
 138       transitions_t 
*sp 
= states
[state
]->transitions
; 
 140       for (i 
= sp
->num 
- 1; i 
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
) 
 142           int k 
= temp_map
[TRANSITION_SYMBOL (sp
, i
)]++; 
 143           from_state
[k
] = state
; 
 144           to_state
[k
] = sp
->states
[i
]; 
 148   XFREE (temp_map 
+ ntokens
); 
 153 /*----------------------------------------------------------. 
 154 | Map a state/symbol pair into its numeric representation.  | 
 155 `----------------------------------------------------------*/ 
 158 map_goto (state_number_t state
, symbol_number_t symbol
) 
 165   low 
= goto_map
[symbol
]; 
 166   high 
= goto_map
[symbol 
+ 1] - 1; 
 170       middle 
= (low 
+ high
) / 2; 
 171       s 
= from_state
[middle
]; 
 189   goto_number_t 
**reads 
= XCALLOC (goto_number_t 
*, ngotos
); 
 190   goto_number_t 
*edge 
= XCALLOC (goto_number_t
, ngotos 
+ 1); 
 195   F 
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
); 
 197   for (i 
= 0; i 
< ngotos
; i
++) 
 199       state_number_t stateno 
= to_state
[i
]; 
 200       transitions_t 
*sp 
= states
[stateno
]->transitions
; 
 203       for (j 
= 0; j 
< sp
->num 
&& TRANSITION_IS_SHIFT (sp
, j
); j
++) 
 204         bitset_set (F
[i
], TRANSITION_SYMBOL (sp
, j
)); 
 206       for (; j 
< sp
->num
; j
++) 
 208           symbol_number_t symbol 
= TRANSITION_SYMBOL (sp
, j
); 
 209           if (nullable
[symbol
]) 
 210             edge
[nedges
++] = map_goto (stateno
, symbol
); 
 215           reads
[i
] = XCALLOC (goto_number_t
, nedges 
+ 1); 
 216           memcpy (reads
[i
], edge
, nedges 
* sizeof (edge
[0])); 
 217           reads
[i
][nedges
] = -1; 
 222   relation_digraph (reads
, ngotos
, &F
); 
 224   for (i 
= 0; i 
< ngotos
; i
++) 
 233 add_lookback_edge (state_t 
*state
, rule_number_t ruleno
, int gotono
) 
 238   for (i 
= 0; i 
< state
->nlookaheads
; ++i
) 
 239     if (state
->lookaheads_rule
[i
]->number 
== ruleno
) 
 242   assert (state
->lookaheads_rule
[i
]->number 
== ruleno
); 
 244   sp 
= XCALLOC (goto_list_t
, 1); 
 245   sp
->next 
= lookback
[(state
->lookaheads 
- LA
) + i
]; 
 247   lookback
[(state
->lookaheads 
- LA
) + i
] = sp
; 
 253 build_relations (void) 
 255   goto_number_t 
*edge 
= XCALLOC (goto_number_t
, ngotos 
+ 1); 
 256   state_number_t 
*states1 
= XCALLOC (state_number_t
, ritem_longest_rhs () + 1); 
 259   includes 
= XCALLOC (goto_number_t 
*, ngotos
); 
 261   for (i 
= 0; i 
< ngotos
; i
++) 
 264       symbol_number_t symbol1 
= states
[to_state
[i
]]->accessing_symbol
; 
 265       rule_number_t 
*rulep
; 
 267       for (rulep 
= derives
[symbol1
]; *rulep 
> 0; rulep
++) 
 272           state_t 
*state 
= states
[from_state
[i
]]; 
 273           states1
[0] = state
->number
; 
 275           for (rp 
= rules
[*rulep
].rhs
; *rp 
>= 0; rp
++) 
 277               state 
= transitions_to (state
->transitions
, 
 278                                       item_number_as_symbol_number (*rp
)); 
 279               states1
[length
++] = state
->number
; 
 282           if (!state
->consistent
) 
 283             add_lookback_edge (state
, *rulep
, i
); 
 291               /* JF added rp>=ritem &&   I hope to god its right! */ 
 292               if (rp 
>= ritem 
&& ISVAR (*rp
)) 
 294                   /* Downcasting from item_number_t to symbol_number_t. */ 
 295                   edge
[nedges
++] = map_goto (states1
[--length
], 
 296                                              item_number_as_symbol_number (*rp
)); 
 306           includes
[i
] = XCALLOC (goto_number_t
, nedges 
+ 1); 
 307           for (j 
= 0; j 
< nedges
; j
++) 
 308             includes
[i
][j
] = edge
[j
]; 
 309           includes
[i
][nedges
] = -1; 
 316   relation_transpose (&includes
, ngotos
); 
 322 compute_FOLLOWS (void) 
 326   relation_digraph (includes
, ngotos
, &F
); 
 328   for (i 
= 0; i 
< ngotos
; i
++) 
 336 compute_lookaheads (void) 
 341   for (i 
= 0; i 
< nLA
; i
++) 
 342     for (sp 
= lookback
[i
]; sp
; sp 
= sp
->next
) 
 343       bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]); 
 346   for (i 
= 0; i 
< nLA
; i
++) 
 347     LIST_FREE (goto_list_t
, lookback
[i
]); 
 354 /*-------------------------------------------------------------. 
 355 | Count the number of lookaheads required for each state       | 
 356 | (NLOOKAHEADS member).  Compute the total number of LA, NLA.  | 
 357 `-------------------------------------------------------------*/ 
 360 states_lookaheads_count (void) 
 366   for (i 
= 0; i 
< nstates
; i
++) 
 370       reductions_t 
*rp 
= states
[i
]->reductions
; 
 371       transitions_t 
*sp 
= states
[i
]->transitions
; 
 373       /* We need a lookahead either to distinguish different 
 374          reductions (i.e., there are two or more), or to distinguish a 
 375          reduction from a shift.  Otherwise, it is straightforward, 
 376          and the state is `consistent'.  */ 
 378           || (rp
->num 
== 1 && sp
->num 
&& TRANSITION_IS_SHIFT (sp
, 0))) 
 379         nlookaheads 
+= rp
->num
; 
 381         states
[i
]->consistent 
= 1; 
 383       for (k 
= 0; k 
< sp
->num
; k
++) 
 384         if (TRANSITION_IS_ERROR (sp
, k
)) 
 386             states
[i
]->consistent 
= 0; 
 390       states
[i
]->nlookaheads 
= nlookaheads
; 
 396 /*--------------------------------------. 
 397 | Initializing the lookaheads members.  | 
 398 `--------------------------------------*/ 
 401 states_lookaheads_initialize (void) 
 405   rule_t 
**pLArule 
= LArule
; 
 407   /* Initialize the members LOOKAHEADS and LOOKAHEADS_RULE for each 
 409   for (i 
= 0; i 
< nstates
; i
++) 
 411       states
[i
]->lookaheads 
= pLA
; 
 412       states
[i
]->lookaheads_rule 
= pLArule
; 
 413       pLA 
+= states
[i
]->nlookaheads
; 
 414       pLArule 
+= states
[i
]->nlookaheads
; 
 419 /*---------------------------------------. 
 420 | Output the lookaheads for each state.  | 
 421 `---------------------------------------*/ 
 424 lookaheads_print (FILE *out
) 
 428   fprintf (out
, "Lookaheads: BEGIN\n"); 
 429   for (i 
= 0; i 
< nstates
; ++i
) 
 431       bitset_iterator iter
; 
 433       fprintf (out
, "State %d: %d lookaheads\n", 
 434                i
, states
[i
]->nlookaheads
); 
 436       for (j 
= 0; j 
< states
[i
]->nlookaheads
; ++j
) 
 437         BITSET_FOR_EACH (iter
, states
[i
]->lookaheads
[j
], k
, 0) 
 439           fprintf (out
, "   on %d (%s) -> rule %d\n", 
 441                    states
[i
]->lookaheads_rule
[j
]->number 
- 1); 
 444   fprintf (out
, "Lookaheads: END\n"); 
 450   states_lookaheads_count (); 
 452   states_lookaheads_initialize (); 
 457   compute_lookaheads (); 
 460     lookaheads_print (stderr
);