]>
git.saurik.com Git - bison.git/blob - src/lalr.c
   1 /* Compute look-ahead criteria for bison, 
   2    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc. 
   4 This file is part of Bison, the GNU Compiler Compiler. 
   6 Bison is free software; you can redistribute it and/or modify 
   7 it under the terms of the GNU General Public License as published by 
   8 the Free Software Foundation; either version 2, or (at your option) 
  11 Bison is distributed in the hope that it will be useful, 
  12 but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14 GNU General Public License for more details. 
  16 You should have received a copy of the GNU General Public License 
  17 along with Bison; see the file COPYING.  If not, write to 
  18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */ 
  21 /* Compute how to make the finite state machine deterministic; 
  22  find which rules need lookahead in each state, and which lookahead tokens they accept. 
  24 lalr(), the entry point, builds these data structures: 
  26 goto_map, from_state and to_state  
  27  record each shift transition which accepts a variable (a nonterminal). 
  28 ngotos is the number of such transitions. 
  29 from_state[t] is the state number which a transition leads from 
  30 and to_state[t] is the state number it leads to. 
  31 All the transitions that accept a particular variable are grouped together and 
  32 goto_map[i - ntokens] is the index in from_state and to_state of the first of them. 
  34 consistent[s] is nonzero if no lookahead is needed to decide what to do in state s. 
  36 LAruleno is a vector which records the rules that need lookahead in various states. 
  37 The elements of LAruleno that apply to state s are those from 
  38  lookaheads[s] through lookaheads[s+1]-1. 
  39 Each element of LAruleno is a rule number. 
  41 If lr is the length of LAruleno, then a number from 0 to lr-1  
  42 can specify both a rule and a state where the rule might be applied. 
  44 LA is a lr by ntokens matrix of bits. 
  45 LA[l, i] is 1 if the rule LAruleno[l] is applicable in the appropriate state 
  46  when the next token is symbol i. 
  47 If LA[l, i] and LA[l, j] are both 1 for i != j, it is a conflict. 
  59 extern short **derives
; 
  60 extern char *nullable
; 
  67 short *accessing_symbol
; 
  71 reductions 
**reduction_table
; 
  77 void set_state_table(); 
  78 void set_accessing_symbol(); 
  79 void set_shift_table(); 
  80 void set_reduction_table(); 
  85 void build_relations(); 
  86 void add_lookback_edge(); 
  87 void compute_FOLLOWS(); 
  88 void compute_lookaheads(); 
  92 extern void toomany(); 
  99 static short **includes
; 
 100 static shorts 
**lookback
; 
 103 static short *VERTICES
; 
 110   tokensetsize 
= WORDSIZE(ntokens
); 
 113   set_accessing_symbol(); 
 115   set_reduction_table(); 
 122   compute_lookaheads(); 
 131   state_table 
= NEW2(nstates
, core 
*); 
 133   for (sp 
= first_state
; sp
; sp 
= sp
->next
) 
 134     state_table
[sp
->number
] = sp
; 
 139 set_accessing_symbol() 
 143   accessing_symbol 
= NEW2(nstates
, short); 
 145   for (sp 
= first_state
; sp
; sp 
= sp
->next
) 
 146     accessing_symbol
[sp
->number
] = sp
->accessing_symbol
; 
 155   shift_table 
= NEW2(nstates
, shifts 
*); 
 157   for (sp 
= first_shift
; sp
; sp 
= sp
->next
) 
 158     shift_table
[sp
->number
] = sp
; 
 163 set_reduction_table() 
 165   register reductions 
*rp
; 
 167   reduction_table 
= NEW2(nstates
, reductions 
*); 
 169   for (rp 
= first_reduction
; rp
; rp 
= rp
->next
) 
 170     reduction_table
[rp
->number
] = rp
; 
 177   register short *itemp
; 
 183   for (itemp 
= ritem
; *itemp
; itemp
++) 
 191           if (length 
> max
) max 
= length
; 
 206   register reductions 
*rp
; 
 210   consistent 
= NEW2(nstates
, char); 
 211   lookaheads 
= NEW2(nstates 
+ 1, short); 
 214   for (i 
= 0; i 
< nstates
; i
++) 
 218       lookaheads
[i
] = count
; 
 220       rp 
= reduction_table
[i
]; 
 222       if (rp 
&& (rp
->nreds 
> 1 
 223           || (sp 
&& ! ISVAR(accessing_symbol
[sp
->shifts
[0]])))) 
 229         for (k 
= 0; k 
< sp
->nshifts
; k
++) 
 231             if (accessing_symbol
[sp
->shifts
[k
]] == error_token_number
) 
 239   lookaheads
[nstates
] = count
; 
 243       LA 
= NEW2(1 * tokensetsize
, unsigned); 
 244       LAruleno 
= NEW2(1, short); 
 245       lookback 
= NEW2(1, shorts 
*); 
 249       LA 
= NEW2(count 
* tokensetsize
, unsigned); 
 250       LAruleno 
= NEW2(count
, short); 
 251       lookback 
= NEW2(count
, shorts 
*); 
 255   for (i 
= 0; i 
< nstates
; i
++) 
 259           if (rp 
= reduction_table
[i
]) 
 260             for (j 
= 0; j 
< rp
->nreds
; j
++) 
 261               *np
++ = rp
->rules
[j
]; 
 274   register short *temp_map
; 
 278   goto_map 
= NEW2(nvars 
+ 1, short) - ntokens
; 
 279   temp_map 
= NEW2(nvars 
+ 1, short) - ntokens
; 
 282   for (sp 
= first_shift
; sp
; sp 
= sp
->next
) 
 284       for (i 
= sp
->nshifts 
- 1; i 
>= 0; i
--) 
 286           symbol 
= accessing_symbol
[sp
->shifts
[i
]]; 
 288           if (ISTOKEN(symbol
)) break; 
 290           if (ngotos 
== MAXSHORT
) 
 299   for (i 
= ntokens
; i 
< nsyms
; i
++) 
 305   for (i 
= ntokens
; i 
< nsyms
; i
++) 
 306     goto_map
[i
] = temp_map
[i
]; 
 308   goto_map
[nsyms
] = ngotos
; 
 309   temp_map
[nsyms
] = ngotos
; 
 311   from_state 
= NEW2(ngotos
, short); 
 312   to_state 
= NEW2(ngotos
, short); 
 314   for (sp 
= first_shift
; sp
; sp 
= sp
->next
) 
 317       for (i 
= sp
->nshifts 
- 1; i 
>= 0; i
--) 
 319           state2 
= sp
->shifts
[i
]; 
 320           symbol 
= accessing_symbol
[state2
]; 
 322           if (ISTOKEN(symbol
)) break; 
 324           k 
= temp_map
[symbol
]++; 
 325           from_state
[k
] = state1
; 
 326           to_state
[k
] = state2
; 
 330   FREE(temp_map 
+ ntokens
); 
 335 /*  Map_goto maps a state/symbol pair into its numeric representation.  */ 
 338 map_goto(state
, symbol
) 
 347   low 
= goto_map
[symbol
]; 
 348   high 
= goto_map
[symbol 
+ 1] - 1; 
 352       middle 
= (low 
+ high
) / 2; 
 353       s 
= from_state
[middle
]; 
 375   register short *edge
; 
 376   register unsigned *rowp
; 
 378   register short **reads
; 
 380   register int stateno
; 
 384   nwords 
= ngotos 
* tokensetsize
; 
 385   F 
= NEW2(nwords
, unsigned); 
 387   reads 
= NEW2(ngotos
, short *); 
 388   edge 
= NEW2(ngotos 
+ 1, short); 
 392   for (i 
= 0; i 
< ngotos
; i
++) 
 394       stateno 
= to_state
[i
]; 
 395       sp 
= shift_table
[stateno
]; 
 401           for (j 
= 0; j 
< k
; j
++) 
 403               symbol 
= accessing_symbol
[sp
->shifts
[j
]]; 
 406               SETBIT(rowp
, symbol
); 
 411               symbol 
= accessing_symbol
[sp
->shifts
[j
]]; 
 412               if (nullable
[symbol
]) 
 413                 edge
[nedges
++] = map_goto(stateno
, symbol
); 
 418               reads
[i
] = rp 
= NEW2(nedges 
+ 1, short); 
 420               for (j 
= 0; j 
< nedges
; j
++) 
 428       rowp 
+= tokensetsize
; 
 433   for (i 
= 0; i 
< ngotos
; i
++) 
 450   register short *rulep
; 
 457   register int stateno
; 
 458   register int symbol1
; 
 459   register int symbol2
; 
 460   register short *shortp
; 
 461   register short *edge
; 
 462   register short *states
; 
 463   register short **new_includes
; 
 465   includes 
= NEW2(ngotos
, short *); 
 466   edge 
= NEW2(ngotos 
+ 1, short); 
 467   states 
= NEW2(maxrhs 
+ 1, short); 
 469   for (i 
= 0; i 
< ngotos
; i
++) 
 472       state1 
= from_state
[i
]; 
 473       symbol1 
= accessing_symbol
[to_state
[i
]]; 
 475       for (rulep 
= derives
[symbol1
]; *rulep 
> 0; rulep
++) 
 481           for (rp 
= ritem 
+ rrhs
[*rulep
]; *rp 
> 0; rp
++) 
 484               sp 
= shift_table
[stateno
]; 
 487               for (j 
= 0; j 
< k
; j
++) 
 489                   stateno 
= sp
->shifts
[j
]; 
 490                   if (accessing_symbol
[stateno
] == symbol2
) break; 
 493               states
[length
++] = stateno
; 
 496           if (!consistent
[stateno
]) 
 497             add_lookback_edge(stateno
, *rulep
, i
); 
 505                         /* JF added rp>=ritem &&   I hope to god its right! */ 
 506               if (rp
>=ritem 
&& ISVAR(*rp
)) 
 508                   stateno 
= states
[--length
]; 
 509                   edge
[nedges
++] = map_goto(stateno
, *rp
); 
 510                   if (nullable
[*rp
]) done 
= 0; 
 517           includes
[i
] = shortp 
= NEW2(nedges 
+ 1, short); 
 518           for (j 
= 0; j 
< nedges
; j
++) 
 524   new_includes 
= transpose(includes
, ngotos
); 
 526   for (i 
= 0; i 
< ngotos
; i
++) 
 532   includes 
= new_includes
; 
 540 add_lookback_edge(stateno
, ruleno
, gotono
) 
 550   i 
= lookaheads
[stateno
]; 
 551   k 
= lookaheads
[stateno 
+ 1]; 
 553   while (!found 
&& i 
< k
) 
 555       if (LAruleno
[i
] == ruleno
) 
 562     berror("add_lookback_edge"); 
 565   sp
->next 
= lookback
[i
]; 
 577   register short **new_R
; 
 578   register short **temp_R
; 
 579   register short *nedges
; 
 584   nedges 
= NEW2(n
, short); 
 586   for (i 
= 0; i 
< n
; i
++) 
 596   new_R 
= NEW2(n
, short *); 
 597   temp_R 
= NEW2(n
, short *); 
 599   for (i 
= 0; i 
< n
; i
++) 
 604           sp 
= NEW2(k 
+ 1, short); 
 613   for (i 
= 0; i 
< n
; i
++) 
 619             *temp_R
[*sp
++]++ = i
; 
 636   for (i 
= 0; i 
< ngotos
; i
++) 
 638       if (includes
[i
]) FREE(includes
[i
]); 
 650   register unsigned *fp1
; 
 651   register unsigned *fp2
; 
 652   register unsigned *fp3
; 
 654   register unsigned *rowp
; 
 655 /*   register short *rulep; JF unused */ 
 656 /*  register int count; JF unused */ 
 657   register shorts 
*sptmp
;/* JF */ 
 660   n 
= lookaheads
[nstates
]; 
 661   for (i 
= 0; i 
< n
; i
++) 
 663       fp3 
= rowp 
+ tokensetsize
; 
 664       for (sp 
= lookback
[i
]; sp
; sp 
= sp
->next
) 
 667           fp2 
= F 
+ tokensetsize 
* sp
->value
; 
 675   for (i 
= 0; i 
< n
; i
++) 
 676     {/* JF removed ref to freed storage */ 
 677       for (sp 
= lookback
[i
]; sp
; sp 
= sptmp
) { 
 694   infinity 
= ngotos 
+ 2; 
 695   INDEX 
= NEW2(ngotos 
+ 1, short); 
 696   VERTICES 
= NEW2(ngotos 
+ 1, short); 
 701   for (i 
= 0; i 
< ngotos
; i
++) 
 704   for (i 
= 0; i 
< ngotos
; i
++) 
 706       if (INDEX
[i
] == 0 && R
[i
]) 
 719   register unsigned *fp1
; 
 720   register unsigned *fp2
; 
 721   register unsigned *fp3
; 
 729   INDEX
[i
] = height 
= top
; 
 731   base 
= F 
+ i 
* tokensetsize
; 
 732   fp3 
= base 
+ tokensetsize
; 
 737       while ((j 
= *rp
++) >= 0) 
 742           if (INDEX
[i
] > INDEX
[j
]) 
 746           fp2 
= F 
+ j 
* tokensetsize
; 
 753   if (INDEX
[i
] == height
) 
 764           fp2 
= F 
+ j 
* tokensetsize
;