]>
git.saurik.com Git - bison.git/blob - src/lalr.c
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 unsigned *F
= NULL
;
55 #define F(Rule) (F + (Rule) * tokensetsize)
57 static short **includes
;
58 static shorts
**lookback
;
61 /*---------------------------------------------------------------.
62 | digraph & traverse. |
64 | The following variables are used as common storage between the |
66 `---------------------------------------------------------------*/
70 static short *VERTICES
;
80 size_t size
= F (i
+ 1) - F(i
);
83 INDEX
[i
] = height
= top
;
86 for (j
= 0; R
[i
][j
] >= 0; ++j
)
88 if (INDEX
[R
[i
][j
]] == 0)
91 if (INDEX
[i
] > INDEX
[R
[i
][j
]])
92 INDEX
[i
] = INDEX
[R
[i
][j
]];
94 for (k
= 0; k
< size
; ++k
)
95 F (i
)[k
] |= F (R
[i
][j
])[k
];
98 if (INDEX
[i
] == height
)
107 for (k
= 0; k
< size
; ++k
)
114 digraph (short **relation
)
118 infinity
= ngotos
+ 2;
119 INDEX
= XCALLOC (short, ngotos
+ 1);
120 VERTICES
= XCALLOC (short, ngotos
+ 1);
125 for (i
= 0; i
< ngotos
; i
++)
128 for (i
= 0; i
< ngotos
; i
++)
129 if (INDEX
[i
] == 0 && R
[i
])
144 /* Avoid having to special case 0. */
148 LA
= XCALLOC (bitset
, nLA
);
149 for (i
= 0; i
< nLA
; ++i
)
151 LA
[i
] = bitset_create (ntokens
, BITSET_FIXED
);
154 LAruleno
= XCALLOC (short, nLA
);
155 lookback
= XCALLOC (shorts
*, nLA
);
158 for (i
= 0; i
< nstates
; i
++)
159 if (!states
[i
]->consistent
)
160 for (j
= 0; j
< states
[i
]->reductions
->nreds
; j
++)
161 *np
++ = states
[i
]->reductions
->rules
[j
];
172 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
173 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
176 for (state
= 0; state
< nstates
; ++state
)
178 shifts
*sp
= states
[state
]->shifts
;
179 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
181 if (ngotos
== MAXSHORT
)
182 fatal (_("too many gotos (max %d)"), MAXSHORT
);
185 goto_map
[SHIFT_SYMBOL (sp
, i
)]++;
191 for (i
= ntokens
; i
< nsyms
; i
++)
197 for (i
= ntokens
; i
< nsyms
; i
++)
198 goto_map
[i
] = temp_map
[i
];
200 goto_map
[nsyms
] = ngotos
;
201 temp_map
[nsyms
] = ngotos
;
204 from_state
= XCALLOC (short, ngotos
);
205 to_state
= XCALLOC (short, ngotos
);
207 for (state
= 0; state
< nstates
; ++state
)
209 shifts
*sp
= states
[state
]->shifts
;
210 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
212 int k
= temp_map
[SHIFT_SYMBOL (sp
, i
)]++;
213 from_state
[k
] = state
;
214 to_state
[k
] = sp
->shifts
[i
];
218 XFREE (temp_map
+ ntokens
);
223 /*----------------------------------------------------------.
224 | Map a state/symbol pair into its numeric representation. |
225 `----------------------------------------------------------*/
228 map_goto (int state
, int symbol
)
235 low
= goto_map
[symbol
];
236 high
= goto_map
[symbol
+ 1] - 1;
240 middle
= (low
+ high
) / 2;
241 s
= from_state
[middle
];
259 short **reads
= XCALLOC (short *, ngotos
);
260 short *edge
= XCALLOC (short, ngotos
+ 1);
265 F
= XCALLOC (unsigned, ngotos
* tokensetsize
);
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 SETBIT (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
)
505 for (j
= 0; j
< ntokens
; ++j
)
506 if (BITISSET (F (sp
->value
), j
))
507 bitset_set (LA
[i
], j
);
511 for (i
= 0; i
< nLA
; i
++)
512 LIST_FREE (shorts
, lookback
[i
]);
519 /*--------------------------------------.
520 | Initializing the lookaheads members. |
521 `--------------------------------------*/
524 initialize_lookaheads (void)
528 for (i
= 0; i
< nstates
; i
++)
532 reductions
*rp
= states
[i
]->reductions
;
533 shifts
*sp
= states
[i
]->shifts
;
535 /* We need a lookahead either to distinguish different
536 reductions (i.e., there are two or more), or to distinguish a
537 reduction from a shift. Otherwise, it is straightforward,
538 and the state is `consistent'. */
540 || (rp
->nreds
== 1 && sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0)))
541 nlookaheads
+= rp
->nreds
;
543 states
[i
]->consistent
= 1;
545 for (k
= 0; k
< sp
->nshifts
; k
++)
546 if (SHIFT_IS_ERROR (sp
, k
))
548 states
[i
]->consistent
= 0;
552 states
[i
]->nlookaheads
= nlookaheads
;
553 states
[i
]->lookaheadsp
= nLA
;
559 /*---------------------------------------.
560 | Output the lookaheads for each state. |
561 `---------------------------------------*/
564 lookaheads_print (FILE *out
)
568 fprintf (out
, "Lookaheads: BEGIN\n");
569 for (i
= 0; i
< nstates
; ++i
)
571 fprintf (out
, "State %d: %d lookaheads\n",
572 i
, states
[i
]->nlookaheads
);
574 for (j
= 0; j
< states
[i
]->nlookaheads
; ++j
)
575 for (k
= 0; k
< ntokens
; ++k
)
576 if (bitset_test (LA
[states
[i
]->lookaheadsp
+ j
], j
))
577 fprintf (out
, " on %d (%s) -> rule %d\n",
579 -LAruleno
[states
[i
]->lookaheadsp
+ j
] - 1);
581 fprintf (out
, "Lookaheads: END\n");
587 tokensetsize
= WORDSIZE (ntokens
);
589 initialize_lookaheads ();
595 compute_lookaheads ();
598 lookaheads_print (stderr
);