]>
git.saurik.com Git - bison.git/blob - src/lalr.c
87e91cd5f553e9857ab345cba7c43bb07eae3123
   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.  */ 
  39 /* All the decorated states, indexed by the state number.  */ 
  40 state_t 
**states 
= NULL
; 
  43 short *LAruleno 
= NULL
; 
  48 short *goto_map 
= NULL
; 
  49 short *from_state 
= NULL
; 
  50 short *to_state 
= NULL
; 
  52 /* And for the famous F variable, which name is so descriptive that a 
  53    comment is hardly needed.  <grin>.  */ 
  54 static bitset 
*F 
= NULL
; 
  56 static short **includes
; 
  57 static shorts 
**lookback
; 
  60 /*---------------------------------------------------------------. 
  61 | digraph & traverse.                                            | 
  63 | The following variables are used as common storage between the | 
  65 `---------------------------------------------------------------*/ 
  69 static short *VERTICES
; 
  80   INDEX
[i
] = height 
= top
; 
  83     for (j 
= 0; R
[i
][j
] >= 0; ++j
) 
  85         if (INDEX
[R
[i
][j
]] == 0) 
  88         if (INDEX
[i
] > INDEX
[R
[i
][j
]]) 
  89           INDEX
[i
] = INDEX
[R
[i
][j
]]; 
  91         bitset_or (F
[i
], F
[i
], F
[R
[i
][j
]]); 
  94   if (INDEX
[i
] == height
) 
 103         bitset_copy (F
[j
], F
[i
]); 
 109 digraph (short **relation
) 
 113   infinity 
= ngotos 
+ 2; 
 114   INDEX 
= XCALLOC (short, ngotos 
+ 1); 
 115   VERTICES 
= XCALLOC (short, ngotos 
+ 1); 
 120   for (i 
= 0; i 
< ngotos
; i
++) 
 123   for (i 
= 0; i 
< ngotos
; i
++) 
 124     if (INDEX
[i
] == 0 && R
[i
]) 
 139   /* Avoid having to special case 0.  */ 
 143   LA 
= XCALLOC (bitset
, nLA
); 
 144   for (i 
= 0; i 
< nLA
; ++i
) 
 146       LA
[i
] = bitset_create (ntokens
, BITSET_FIXED
); 
 149   LAruleno 
= XCALLOC (short, nLA
); 
 150   lookback 
= XCALLOC (shorts 
*, nLA
); 
 153   for (i 
= 0; i 
< nstates
; i
++) 
 154     if (!states
[i
]->consistent
) 
 155       for (j 
= 0; j 
< states
[i
]->reductions
->nreds
; j
++) 
 156         *np
++ = states
[i
]->reductions
->rules
[j
]; 
 167   goto_map 
= XCALLOC (short, nvars 
+ 1) - ntokens
; 
 168   temp_map 
= XCALLOC (short, nvars 
+ 1) - ntokens
; 
 171   for (state 
= 0; state 
< nstates
; ++state
) 
 173       shifts 
*sp 
= states
[state
]->shifts
; 
 174       for (i 
= sp
->nshifts 
- 1; i 
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
) 
 176           if (ngotos 
== MAXSHORT
) 
 177             fatal (_("too many gotos (max %d)"), MAXSHORT
); 
 180           goto_map
[SHIFT_SYMBOL (sp
, i
)]++; 
 186     for (i 
= ntokens
; i 
< nsyms
; i
++) 
 192     for (i 
= ntokens
; i 
< nsyms
; i
++) 
 193       goto_map
[i
] = temp_map
[i
]; 
 195     goto_map
[nsyms
] = ngotos
; 
 196     temp_map
[nsyms
] = ngotos
; 
 199   from_state 
= XCALLOC (short, ngotos
); 
 200   to_state 
= XCALLOC (short, ngotos
); 
 202   for (state 
= 0; state 
< nstates
; ++state
) 
 204       shifts 
*sp 
= states
[state
]->shifts
; 
 205       for (i 
= sp
->nshifts 
- 1; i 
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
) 
 207           int k 
= temp_map
[SHIFT_SYMBOL (sp
, i
)]++; 
 208           from_state
[k
] = state
; 
 209           to_state
[k
] = sp
->shifts
[i
]; 
 213   XFREE (temp_map 
+ ntokens
); 
 218 /*----------------------------------------------------------. 
 219 | Map a state/symbol pair into its numeric representation.  | 
 220 `----------------------------------------------------------*/ 
 223 map_goto (int state
, int symbol
) 
 230   low 
= goto_map
[symbol
]; 
 231   high 
= goto_map
[symbol 
+ 1] - 1; 
 235       middle 
= (low 
+ high
) / 2; 
 236       s 
= from_state
[middle
]; 
 254   short **reads 
= XCALLOC (short *, ngotos
); 
 255   short *edge 
= XCALLOC (short, ngotos 
+ 1); 
 260   F 
= XCALLOC (bitset
, ngotos
); 
 261   for (i 
= 0; i 
< ngotos
; ++i
) 
 263       F
[i
] = bitset_create (ntokens
, BITSET_FIXED
); 
 267   for (i 
= 0; i 
< ngotos
; i
++) 
 269       int stateno 
= to_state
[i
]; 
 270       shifts 
*sp 
= states
[stateno
]->shifts
; 
 273       for (j 
= 0; j 
< sp
->nshifts 
&& SHIFT_IS_SHIFT (sp
, j
); j
++) 
 274         bitset_set (F
[i
], SHIFT_SYMBOL (sp
, j
)); 
 276       for (; j 
< sp
->nshifts
; j
++) 
 278           int symbol 
= SHIFT_SYMBOL (sp
, j
); 
 279           if (nullable
[symbol
]) 
 280             edge
[nedges
++] = map_goto (stateno
, symbol
); 
 285           reads
[i
] = XCALLOC (short, nedges 
+ 1); 
 286           shortcpy (reads
[i
], edge
, nedges
); 
 287           reads
[i
][nedges
] = -1; 
 294   for (i 
= 0; i 
< ngotos
; i
++) 
 303 add_lookback_edge (state_t 
*state
, int ruleno
, int gotono
) 
 308   for (i 
= 0; i 
< state
->nlookaheads
; ++i
) 
 309     if (LAruleno
[state
->lookaheadsp 
+ i
] == ruleno
) 
 312   assert (LAruleno
[state
->lookaheadsp 
+ i
] == ruleno
); 
 314   sp 
= XCALLOC (shorts
, 1); 
 315   sp
->next 
= lookback
[state
->lookaheadsp 
+ i
]; 
 317   lookback
[state
->lookaheadsp 
+ i
] = sp
; 
 322 matrix_print (FILE *out
, short **matrix
, int n
) 
 326   for (i 
= 0; i 
< n
; ++i
) 
 328       fprintf (out
, "%3d: ", i
); 
 330         for (j 
= 0; matrix
[i
][j
] != -1; ++j
) 
 331           fprintf (out
, "%3d ", matrix
[i
][j
]); 
 337 /*-------------------------------------------------------------------. 
 338 | Return the transpose of R_ARG, of size N.  Destroy R_ARG, as it is | 
 339 | replaced with the result.                                          | 
 341 | R_ARG[I] is NULL or a -1 terminated list of numbers.               | 
 343 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM | 
 345 `-------------------------------------------------------------------*/ 
 348 transpose (short **R_arg
, int n
) 
 351   short **new_R 
= XCALLOC (short *, n
); 
 352   /* END_R[I] -- next entry of NEW_R[I]. */ 
 353   short **end_R 
= XCALLOC (short *, n
); 
 354   /* NEDGES[I] -- total size of NEW_R[I]. */ 
 355   short *nedges 
= XCALLOC (short, n
); 
 360       fputs ("transpose: input\n", stderr
); 
 361       matrix_print (stderr
, R_arg
, n
); 
 365   for (i 
= 0; i 
< n
; i
++) 
 367       for (j 
= 0; R_arg
[i
][j
] >= 0; ++j
) 
 368         ++nedges
[R_arg
[i
][j
]]; 
 371   for (i 
= 0; i 
< n
; i
++) 
 374         short *sp 
= XCALLOC (short, nedges
[i
] + 1); 
 381   for (i 
= 0; i 
< n
; i
++) 
 383       for (j 
= 0; R_arg
[i
][j
] >= 0; ++j
) 
 385           *end_R
[R_arg
[i
][j
]] = i
; 
 386           ++end_R
[R_arg
[i
][j
]]; 
 392   /* Free the input: it is replaced with the result. */ 
 393   for (i 
= 0; i 
< n
; i
++) 
 399       fputs ("transpose: output\n", stderr
); 
 400       matrix_print (stderr
, new_R
, n
); 
 408 build_relations (void) 
 410   short *edge 
= XCALLOC (short, ngotos 
+ 1); 
 411   short *states1 
= XCALLOC (short, ritem_longest_rhs () + 1); 
 414   includes 
= XCALLOC (short *, ngotos
); 
 416   for (i 
= 0; i 
< ngotos
; i
++) 
 419       int symbol1 
= states
[to_state
[i
]]->accessing_symbol
; 
 422       for (rulep 
= derives
[symbol1
]; *rulep 
> 0; rulep
++) 
 427           state_t 
*state 
= states
[from_state
[i
]]; 
 428           states1
[0] = state
->number
; 
 430           for (rp 
= &ritem
[rules
[*rulep
].rhs
]; *rp 
>= 0; rp
++) 
 432               shifts 
*sp 
= state
->shifts
; 
 434               for (j 
= 0; j 
< sp
->nshifts
; j
++) 
 436                   state 
= states
[sp
->shifts
[j
]]; 
 437                   if (state
->accessing_symbol 
== *rp
) 
 441               states1
[length
++] = state
->number
; 
 444           if (!state
->consistent
) 
 445             add_lookback_edge (state
, *rulep
, i
); 
 453               /* JF added rp>=ritem &&   I hope to god its right! */ 
 454               if (rp 
>= ritem 
&& ISVAR (*rp
)) 
 456                   edge
[nedges
++] = map_goto (states1
[--length
], *rp
); 
 466           includes
[i
] = XCALLOC (short, nedges 
+ 1); 
 467           for (j 
= 0; j 
< nedges
; j
++) 
 468             includes
[i
][j
] = edge
[j
]; 
 469           includes
[i
][nedges
] = -1; 
 476   includes 
= transpose (includes
, ngotos
); 
 482 compute_FOLLOWS (void) 
 488   for (i 
= 0; i 
< ngotos
; i
++) 
 496 compute_lookaheads (void) 
 501   for (i 
= 0; i 
< nLA
; i
++) 
 502     for (sp 
= lookback
[i
]; sp
; sp 
= sp
->next
) 
 503       bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]); 
 506   for (i 
= 0; i 
< nLA
; i
++) 
 507     LIST_FREE (shorts
, lookback
[i
]); 
 510   for (i 
= 0; i 
< (unsigned) ngotos
; ++i
) 
 516 /*--------------------------------------. 
 517 | Initializing the lookaheads members.  | 
 518 `--------------------------------------*/ 
 521 initialize_lookaheads (void) 
 525   for (i 
= 0; i 
< nstates
; i
++) 
 529       reductions 
*rp 
= states
[i
]->reductions
; 
 530       shifts 
*sp 
= states
[i
]->shifts
; 
 532       /* We need a lookahead either to distinguish different 
 533          reductions (i.e., there are two or more), or to distinguish a 
 534          reduction from a shift.  Otherwise, it is straightforward, 
 535          and the state is `consistent'.  */ 
 537           || (rp
->nreds 
== 1 && sp
->nshifts 
&& SHIFT_IS_SHIFT (sp
, 0))) 
 538         nlookaheads 
+= rp
->nreds
; 
 540         states
[i
]->consistent 
= 1; 
 542       for (k 
= 0; k 
< sp
->nshifts
; k
++) 
 543         if (SHIFT_IS_ERROR (sp
, k
)) 
 545             states
[i
]->consistent 
= 0; 
 549       states
[i
]->nlookaheads 
= nlookaheads
; 
 550       states
[i
]->lookaheadsp 
= nLA
; 
 556 /*---------------------------------------. 
 557 | Output the lookaheads for each state.  | 
 558 `---------------------------------------*/ 
 561 lookaheads_print (FILE *out
) 
 565   fprintf (out
, "Lookaheads: BEGIN\n"); 
 566   for (i 
= 0; i 
< nstates
; ++i
) 
 568       fprintf (out
, "State %d: %d lookaheads\n", 
 569                i
, states
[i
]->nlookaheads
); 
 571       for (j 
= 0; j 
< states
[i
]->nlookaheads
; ++j
) 
 572         for (k 
= 0; k 
< ntokens
; ++k
) 
 573           if (bitset_test (LA
[states
[i
]->lookaheadsp 
+ j
], j
)) 
 574             fprintf (out
, "   on %d (%s) -> rule %d\n", 
 576                      -LAruleno
[states
[i
]->lookaheadsp 
+ j
] - 1); 
 578   fprintf (out
, "Lookaheads: END\n"); 
 584   tokensetsize 
= WORDSIZE (ntokens
); 
 586   initialize_lookaheads (); 
 592   compute_lookaheads (); 
 595     lookaheads_print (stderr
);