]>
git.saurik.com Git - bison.git/blob - src/lalr.c
   1 /* Compute look-ahead criteria for Bison. 
   3    Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002, 2003 
   4    Free Software Foundation, Inc. 
   6    This file is part of Bison, the GNU Compiler Compiler. 
   8    Bison is free software; you can redistribute it and/or modify 
   9    it under the terms of the GNU General Public License as published by 
  10    the Free Software Foundation; either version 2, or (at your option) 
  13    Bison is distributed in the hope that it will be useful, 
  14    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  16    GNU General Public License for more details. 
  18    You should have received a copy of the GNU General Public License 
  19    along with Bison; see the file COPYING.  If not, write to 
  20    the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
  21    Boston, MA 02111-1307, USA.  */ 
  24 /* Compute how to make the finite state machine deterministic; find 
  25    which rules need lookahead in each state, and which lookahead 
  26    tokens they accept.  */ 
  45 goto_number 
*goto_map 
= NULL
; 
  46 static goto_number ngotos 
= 0; 
  47 state_number 
*from_state 
= NULL
; 
  48 state_number 
*to_state 
= NULL
; 
  50 /* Linked list of goto numbers.  */ 
  51 typedef struct goto_list
 
  53   struct goto_list 
*next
; 
  58 /* LA is a LR by NTOKENS matrix of bits.  LA[l, i] is 1 if the rule 
  59    LArule[l] is applicable in the appropriate state when the next 
  60    token is symbol i.  If LA[l, i] and LA[l, j] are both 1 for i != j, 
  63 static bitsetv LA 
= NULL
; 
  67 /* And for the famous F variable, which name is so descriptive that a 
  68    comment is hardly needed.  <grin>.  */ 
  69 static bitsetv F 
= NULL
; 
  71 static goto_number 
**includes
; 
  72 static goto_list 
**lookback
; 
  81   goto_number 
*temp_map
; 
  83   CALLOC (goto_map
, nvars 
+ 1); 
  84   CALLOC (temp_map
, nvars 
+ 1); 
  87   for (s 
= 0; s 
< nstates
; ++s
) 
  89       transitions 
*sp 
= states
[s
]->transitions
; 
  91       for (i 
= sp
->num 
- 1; i 
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
) 
  93           if (ngotos 
>= GOTO_NUMBER_MAXIMUM
) 
  96           goto_map
[TRANSITION_SYMBOL (sp
, i
) - ntokens
]++; 
 103     for (i 
= ntokens
; i 
< nsyms
; i
++) 
 105         temp_map
[i 
- ntokens
] = k
; 
 106         k 
+= goto_map
[i 
- ntokens
]; 
 109     for (i 
= ntokens
; i 
< nsyms
; i
++) 
 110       goto_map
[i 
- ntokens
] = temp_map
[i 
- ntokens
]; 
 112     goto_map
[nsyms 
- ntokens
] = ngotos
; 
 113     temp_map
[nsyms 
- ntokens
] = ngotos
; 
 116   CALLOC (from_state
, ngotos
); 
 117   CALLOC (to_state
, ngotos
); 
 119   for (s 
= 0; s 
< nstates
; ++s
) 
 121       transitions 
*sp 
= states
[s
]->transitions
; 
 123       for (i 
= sp
->num 
- 1; i 
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
) 
 125           int k 
= temp_map
[TRANSITION_SYMBOL (sp
, i
) - ntokens
]++; 
 127           to_state
[k
] = sp
->states
[i
]->number
; 
 136 /*----------------------------------------------------------. 
 137 | Map a state/symbol pair into its numeric representation.  | 
 138 `----------------------------------------------------------*/ 
 141 map_goto (state_number s0
, symbol_number sym
) 
 148   low 
= goto_map
[sym 
- ntokens
]; 
 149   high 
= goto_map
[sym 
- ntokens 
+ 1] - 1; 
 155       middle 
= (low 
+ high
) / 2; 
 156       s 
= from_state
[middle
]; 
 170   goto_number 
**reads 
= CALLOC (reads
, ngotos
); 
 171   goto_number 
*edge 
= CALLOC (edge
, ngotos 
+ 1); 
 176   F 
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
); 
 178   for (i 
= 0; i 
< ngotos
; i
++) 
 180       state_number stateno 
= to_state
[i
]; 
 181       transitions 
*sp 
= states
[stateno
]->transitions
; 
 184       FOR_EACH_SHIFT (sp
, j
) 
 185         bitset_set (F
[i
], TRANSITION_SYMBOL (sp
, j
)); 
 187       for (; j 
< sp
->num
; j
++) 
 189           symbol_number sym 
= TRANSITION_SYMBOL (sp
, j
); 
 190           if (nullable
[sym 
- ntokens
]) 
 191             edge
[nedges
++] = map_goto (stateno
, sym
); 
 196           CALLOC (reads
[i
], nedges 
+ 1); 
 197           memcpy (reads
[i
], edge
, nedges 
* sizeof (edge
[0])); 
 198           reads
[i
][nedges
] = -1; 
 203   relation_digraph (reads
, ngotos
, &F
); 
 205   for (i 
= 0; i 
< ngotos
; i
++) 
 214 add_lookback_edge (state 
*s
, rule 
*r
, int gotono
) 
 216   int ri 
= state_reduction_find (s
, r
); 
 217   goto_list 
*sp 
= MALLOC (sp
, 1); 
 218   sp
->next 
= lookback
[(s
->reductions
->lookaheads 
- LA
) + ri
]; 
 220   lookback
[(s
->reductions
->lookaheads 
- LA
) + ri
] = sp
; 
 226 build_relations (void) 
 228   goto_number 
*edge 
= CALLOC (edge
, ngotos 
+ 1); 
 229   state_number 
*states1 
= CALLOC (states1
, ritem_longest_rhs () + 1); 
 232   CALLOC (includes
, ngotos
); 
 234   for (i 
= 0; i 
< ngotos
; i
++) 
 237       symbol_number symbol1 
= states
[to_state
[i
]]->accessing_symbol
; 
 240       for (rulep 
= derives
[symbol1 
- ntokens
]; *rulep
; rulep
++) 
 245           state 
*s 
= states
[from_state
[i
]]; 
 246           states1
[0] = s
->number
; 
 248           for (rp 
= (*rulep
)->rhs
; *rp 
>= 0; rp
++) 
 250               s 
= transitions_to (s
->transitions
, 
 251                                   item_number_as_symbol_number (*rp
)); 
 252               states1
[length
++] = s
->number
; 
 256             add_lookback_edge (s
, *rulep
, i
); 
 264               /* JF added rp>=ritem &&   I hope to god its right! */ 
 265               if (rp 
>= ritem 
&& ISVAR (*rp
)) 
 267                   /* Downcasting from item_number to symbol_number.  */ 
 268                   edge
[nedges
++] = map_goto (states1
[--length
], 
 269                                              item_number_as_symbol_number (*rp
)); 
 270                   if (nullable
[*rp 
- ntokens
]) 
 279           CALLOC (includes
[i
], nedges 
+ 1); 
 280           for (j 
= 0; j 
< nedges
; j
++) 
 281             includes
[i
][j
] = edge
[j
]; 
 282           includes
[i
][nedges
] = -1; 
 289   relation_transpose (&includes
, ngotos
); 
 295 compute_FOLLOWS (void) 
 299   relation_digraph (includes
, ngotos
, &F
); 
 301   for (i 
= 0; i 
< ngotos
; i
++) 
 309 compute_lookaheads (void) 
 314   for (i 
= 0; i 
< nLA
; i
++) 
 315     for (sp 
= lookback
[i
]; sp
; sp 
= sp
->next
) 
 316       bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]); 
 319   for (i 
= 0; i 
< nLA
; i
++) 
 320     LIST_FREE (goto_list
, lookback
[i
]); 
 327 /*-----------------------------------------------------------. 
 328 | Count the number of lookaheads required for S (NLOOKAHEADS | 
 330 `-----------------------------------------------------------*/ 
 333 state_lookaheads_count (state 
*s
) 
 337   reductions 
*rp 
= s
->reductions
; 
 338   transitions 
*sp 
= s
->transitions
; 
 340   /* We need a lookahead either to distinguish different 
 341      reductions (i.e., there are two or more), or to distinguish a 
 342      reduction from a shift.  Otherwise, it is straightforward, 
 343      and the state is `consistent'.  */ 
 345       || (rp
->num 
== 1 && sp
->num 
&& 
 346           !TRANSITION_IS_DISABLED (sp
, 0) && TRANSITION_IS_SHIFT (sp
, 0))) 
 347     nlookaheads 
+= rp
->num
; 
 351   for (k 
= 0; k 
< sp
->num
; k
++) 
 352     if (!TRANSITION_IS_DISABLED (sp
, k
) && TRANSITION_IS_ERROR (sp
, k
)) 
 362 /*----------------------------------------------. 
 363 | Compute LA, NLA, and the lookaheads members.  | 
 364 `----------------------------------------------*/ 
 372   /* Compute the total number of reductions requiring a lookahead.  */ 
 374   for (i 
= 0; i 
< nstates
; i
++) 
 375     nLA 
+= state_lookaheads_count (states
[i
]); 
 376   /* Avoid having to special case 0.  */ 
 380   pLA 
= LA 
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
); 
 381   CALLOC (lookback
, nLA
); 
 383   /* Initialize the members LOOKAHEADS for each state which reductions 
 384      require lookaheads.  */ 
 385   for (i 
= 0; i 
< nstates
; i
++) 
 387       int count 
= state_lookaheads_count (states
[i
]); 
 390           states
[i
]->reductions
->lookaheads 
= pLA
; 
 397 /*---------------------------------------. 
 398 | Output the lookaheads for each state.  | 
 399 `---------------------------------------*/ 
 402 lookaheads_print (FILE *out
) 
 406   fprintf (out
, "Lookaheads: BEGIN\n"); 
 407   for (i 
= 0; i 
< nstates
; ++i
) 
 409       reductions 
*reds 
= states
[i
]->reductions
; 
 410       bitset_iterator iter
; 
 413       if (reds
->lookaheads
) 
 414         for (k 
= 0; k 
< reds
->num
; ++k
) 
 415           if (reds
->lookaheads
[k
]) 
 418       fprintf (out
, "State %d: %d lookaheads\n", 
 421       if (reds
->lookaheads
) 
 422         for (j 
= 0; j 
< reds
->num
; ++j
) 
 423           BITSET_FOR_EACH (iter
, reds
->lookaheads
[j
], k
, 0) 
 425             fprintf (out
, "   on %d (%s) -> rule %d\n", 
 427                      reds
->rules
[j
]->number
); 
 430   fprintf (out
, "Lookaheads: END\n"); 
 441   compute_lookaheads (); 
 443   if (trace_flag 
& trace_sets
) 
 444     lookaheads_print (stderr
); 
 452   for (s 
= 0; s 
< nstates
; ++s
) 
 453     states
[s
]->reductions
->lookaheads 
= NULL
;