]>
git.saurik.com Git - bison.git/blob - src/lalr.c
0eb32411a92750dc33f209db6181b228d2f3da83
   1 /* Compute look-ahead criteria for bison, 
   2    Copyright 1984, 1986, 1989, 2000, 2001  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, Inc., 59 Temple Place - Suite 330, 
  19    Boston, MA 02111-1307, USA.  */ 
  22 /* Compute how to make the finite state machine deterministic; find 
  23    which rules need lookahead in each state, and which lookahead 
  24    tokens they accept.  */ 
  36 /* All the decorated states, indexed by the state number.  Warning: 
  37    there is a state_TABLE in LR0.c, but it is different and static. 
  39 state_t 
**state_table 
= NULL
; 
  50 /* And for the famous F variable, which name is so descriptive that a 
  51    comment is hardly needed.  <grin>.  */ 
  52 static unsigned *F 
= NULL
; 
  53 #define F(Rule)  (F + (Rule) * tokensetsize) 
  55 static short **includes
; 
  56 static shorts 
**lookback
; 
  59 /*---------------------------------------------------------------. 
  60 | digraph & traverse.                                            | 
  62 | The following variables are used as common storage between the | 
  64 `---------------------------------------------------------------*/ 
  68 static short *VERTICES
; 
  78   size_t size 
= F (i 
+ 1) - F(i
); 
  81   INDEX
[i
] = height 
= top
; 
  84     for (j 
= 0; R
[i
][j
] >= 0; ++j
) 
  86         if (INDEX
[R
[i
][j
]] == 0) 
  89         if (INDEX
[i
] > INDEX
[R
[i
][j
]]) 
  90           INDEX
[i
] = INDEX
[R
[i
][j
]]; 
  92         for (k 
= 0; k 
< size
; ++k
) 
  93           F (i
)[k
] |= F (R
[i
][j
])[k
]; 
  96   if (INDEX
[i
] == height
) 
 105         for (k 
= 0; k 
< size
; ++k
) 
 112 digraph (short **relation
) 
 116   infinity 
= ngotos 
+ 2; 
 117   INDEX 
= XCALLOC (short, ngotos 
+ 1); 
 118   VERTICES 
= XCALLOC (short, ngotos 
+ 1); 
 123   for (i 
= 0; i 
< ngotos
; i
++) 
 126   for (i 
= 0; i 
< ngotos
; i
++) 
 127     if (INDEX
[i
] == 0 && R
[i
]) 
 143   size_t nLA 
= state_table
[nstates
]->lookaheadsp
; 
 147   LA 
= XCALLOC (unsigned, nLA 
* tokensetsize
); 
 148   LAruleno 
= XCALLOC (short, nLA
); 
 149   lookback 
= XCALLOC (shorts 
*, nLA
); 
 152   for (i 
= 0; i 
< nstates
; i
++) 
 153     if (!state_table
[i
]->consistent
) 
 154       if ((rp 
= state_table
[i
]->reductions
)) 
 155         for (j 
= 0; j 
< rp
->nreds
; j
++) 
 156           *np
++ = rp
->rules
[j
]; 
 170   goto_map 
= XCALLOC (short, nvars 
+ 1) - ntokens
; 
 171   temp_map 
= XCALLOC (short, nvars 
+ 1) - ntokens
; 
 174   for (state 
= 0; state 
< nstates
; ++state
) 
 176       shifts 
*sp 
= state_table
[state
]->shifts
; 
 177       for (i 
= sp
->nshifts 
- 1; i 
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
) 
 179           symbol 
= state_table
[sp
->shifts
[i
]]->accessing_symbol
; 
 181           if (ngotos 
== MAXSHORT
) 
 182             fatal (_("too many gotos (max %d)"), MAXSHORT
); 
 190   for (i 
= ntokens
; i 
< nsyms
; i
++) 
 196   for (i 
= ntokens
; i 
< nsyms
; i
++) 
 197     goto_map
[i
] = temp_map
[i
]; 
 199   goto_map
[nsyms
] = ngotos
; 
 200   temp_map
[nsyms
] = ngotos
; 
 202   from_state 
= XCALLOC (short, ngotos
); 
 203   to_state 
= XCALLOC (short, ngotos
); 
 205   for (state 
= 0; state 
< nstates
; ++state
) 
 207       shifts 
*sp 
= state_table
[state
]->shifts
; 
 208       for (i 
= sp
->nshifts 
- 1; i 
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
) 
 210           for (i 
= sp
->nshifts 
- 1; i 
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
) 
 212               state2 
= sp
->shifts
[i
]; 
 213               symbol 
= state_table
[state2
]->accessing_symbol
; 
 215               k 
= temp_map
[symbol
]++; 
 216               from_state
[k
] = state
; 
 217               to_state
[k
] = state2
; 
 222   XFREE (temp_map 
+ ntokens
); 
 227 /*----------------------------------------------------------. 
 228 | Map a state/symbol pair into its numeric representation.  | 
 229 `----------------------------------------------------------*/ 
 232 map_goto (int state
, int symbol
) 
 239   low 
= goto_map
[symbol
]; 
 240   high 
= goto_map
[symbol 
+ 1] - 1; 
 244       middle 
= (low 
+ high
) / 2; 
 245       s 
= from_state
[middle
]; 
 263   short **reads 
= XCALLOC (short *, ngotos
); 
 264   short *edge 
= XCALLOC (short, ngotos 
+ 1); 
 269   F 
= XCALLOC (unsigned, ngotos 
* tokensetsize
); 
 271   for (i 
= 0; i 
< ngotos
; i
++) 
 273       int stateno 
= to_state
[i
]; 
 274       shifts 
*sp 
= state_table
[stateno
]->shifts
; 
 277       for (j 
= 0; j 
< sp
->nshifts 
&& SHIFT_IS_SHIFT (sp
, j
); j
++) 
 279           int symbol 
= state_table
[sp
->shifts
[j
]]->accessing_symbol
; 
 280           SETBIT (F (i
), symbol
); 
 283       for (; j 
< sp
->nshifts
; j
++) 
 285           int symbol 
= state_table
[sp
->shifts
[j
]]->accessing_symbol
; 
 286           if (nullable
[symbol
]) 
 287             edge
[nedges
++] = map_goto (stateno
, symbol
); 
 292           reads
[i
] = XCALLOC (short, nedges 
+ 1); 
 293           shortcpy (reads
[i
], edge
, nedges
); 
 294           reads
[i
][nedges
] = -1; 
 301   for (i 
= 0; i 
< ngotos
; i
++) 
 310 add_lookback_edge (int stateno
, int ruleno
, int gotono
) 
 315   for (i 
= 0; i 
< state_table
[stateno
]->nlookaheads
; ++i
) 
 316     if (LAruleno
[state_table
[stateno
]->lookaheadsp 
+ i
] == ruleno
) 
 319   assert (LAruleno
[state_table
[stateno
]->lookaheadsp 
+ i
] == ruleno
); 
 321   sp 
= XCALLOC (shorts
, 1); 
 322   sp
->next 
= lookback
[state_table
[stateno
]->lookaheadsp 
+ i
]; 
 324   lookback
[state_table
[stateno
]->lookaheadsp 
+ i
] = sp
; 
 329 matrix_print (FILE *out
, short **matrix
, int n
) 
 333   for (i 
= 0; i 
< n
; ++i
) 
 335       fprintf (out
, "%3d: ", i
); 
 337         for (j 
= 0; matrix
[i
][j
] != -1; ++j
) 
 338           fprintf (out
, "%3d ", matrix
[i
][j
]); 
 344 /*-------------------------------------------------------------------. 
 345 | Return the transpose of R_ARG, of size N.  Destroy R_ARG, as it is | 
 346 | replaced with the result.                                          | 
 348 | R_ARG[I] is NULL or a -1 terminated list of numbers.               | 
 350 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM | 
 352 `-------------------------------------------------------------------*/ 
 355 transpose (short **R_arg
, int n
) 
 358   short **new_R 
= XCALLOC (short *, n
); 
 359   /* END_R[I] -- next entry of NEW_R[I]. */ 
 360   short **end_R 
= XCALLOC (short *, n
); 
 361   /* NEDGES[I] -- total size of NEW_R[I]. */ 
 362   short *nedges 
= XCALLOC (short, n
); 
 367       fputs ("transpose: input\n", stderr
); 
 368       matrix_print (stderr
, R_arg
, n
); 
 372   for (i 
= 0; i 
< n
; i
++) 
 374       for (j 
= 0; R_arg
[i
][j
] >= 0; ++j
) 
 375         ++nedges
[R_arg
[i
][j
]]; 
 378   for (i 
= 0; i 
< n
; i
++) 
 381         short *sp 
= XCALLOC (short, nedges
[i
] + 1); 
 388   for (i 
= 0; i 
< n
; i
++) 
 390       for (j 
= 0; R_arg
[i
][j
] >= 0; ++j
) 
 392           *end_R
[R_arg
[i
][j
]] = i
; 
 393           ++end_R
[R_arg
[i
][j
]]; 
 399   /* Free the input: it is replaced with the result. */ 
 400   for (i 
= 0; i 
< n
; i
++) 
 406       fputs ("transpose: output\n", stderr
); 
 407       matrix_print (stderr
, new_R
, n
); 
 415 build_relations (void) 
 417   short *edge 
= XCALLOC (short, ngotos 
+ 1); 
 418   short *states 
= XCALLOC (short, ritem_longest_rhs () + 1); 
 421   includes 
= XCALLOC (short *, ngotos
); 
 423   for (i 
= 0; i 
< ngotos
; i
++) 
 426       int state1 
= from_state
[i
]; 
 427       int symbol1 
= state_table
[to_state
[i
]]->accessing_symbol
; 
 430       for (rulep 
= derives
[symbol1
]; *rulep 
> 0; rulep
++) 
 434           int stateno 
= state1
; 
 438           for (rp 
= ritem 
+ rule_table
[*rulep
].rhs
; *rp 
> 0; rp
++) 
 440               shifts 
*sp 
= state_table
[stateno
]->shifts
; 
 442               for (j 
= 0; j 
< sp
->nshifts
; j
++) 
 444                   stateno 
= sp
->shifts
[j
]; 
 445                   if (state_table
[stateno
]->accessing_symbol 
== *rp
) 
 449               states
[length
++] = stateno
; 
 452           if (!state_table
[stateno
]->consistent
) 
 453             add_lookback_edge (stateno
, *rulep
, i
); 
 461               /* JF added rp>=ritem &&   I hope to god its right! */ 
 462               if (rp 
>= ritem 
&& ISVAR (*rp
)) 
 464                   stateno 
= states
[--length
]; 
 465                   edge
[nedges
++] = map_goto (stateno
, *rp
); 
 475           includes
[i
] = XCALLOC (short, nedges 
+ 1); 
 476           for (j 
= 0; j 
< nedges
; j
++) 
 477             includes
[i
][j
] = edge
[j
]; 
 478           includes
[i
][nedges
] = -1; 
 485   includes 
= transpose (includes
, ngotos
); 
 491 compute_FOLLOWS (void) 
 497   for (i 
= 0; i 
< ngotos
; i
++) 
 505 compute_lookaheads (void) 
 510   for (i 
= 0; i 
< state_table
[nstates
]->lookaheadsp
; i
++) 
 511     for (sp 
= lookback
[i
]; sp
; sp 
= sp
->next
) 
 513         int size 
= LA (i 
+ 1) - LA (i
); 
 515         for (j 
= 0; j 
< size
; ++j
) 
 516           LA (i
)[j
] |= F (sp
->value
)[j
]; 
 520   for (i 
= 0; i 
< state_table
[nstates
]->lookaheadsp
; i
++) 
 521     LIST_FREE (shorts
, lookback
[i
]); 
 528 /*--------------------------------------. 
 529 | Initializing the lookaheads members.  | 
 530 `--------------------------------------*/ 
 533 initialize_lookaheads (void) 
 537   for (i 
= 0; i 
< nstates
; i
++) 
 541       reductions 
*rp 
= state_table
[i
]->reductions
; 
 542       shifts 
*sp 
= state_table
[i
]->shifts
; 
 545           && (rp
->nreds 
> 1 || (sp
->nshifts 
&& SHIFT_IS_SHIFT (sp
, 0)))) 
 546         nlookaheads 
+= rp
->nreds
; 
 548         state_table
[i
]->consistent 
= 1; 
 550       for (k 
= 0; k 
< sp
->nshifts
; k
++) 
 551         if (SHIFT_IS_ERROR (sp
, k
)) 
 553             state_table
[i
]->consistent 
= 0; 
 557       state_table
[i
]->nlookaheads 
= nlookaheads
; 
 558       state_table
[i
]->lookaheadsp 
= count
; 
 559       count 
+= nlookaheads
; 
 562   /* Seems to be needed by conflicts.c. */ 
 563   state_table
[nstates
] = STATE_ALLOC (0); 
 564   state_table
[nstates
]->lookaheadsp 
= count
; 
 570   tokensetsize 
= WORDSIZE (ntokens
); 
 572   initialize_lookaheads (); 
 578   compute_lookaheads ();