]>
git.saurik.com Git - bison.git/blob - src/lalr.c
84f32894578b89dd547e1de05779a5eb6ab4ca52
1 /* Compute look-ahead criteria for bison,
2 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* Compute how to make the finite state machine deterministic; find
24 which rules need lookahead in each state, and which lookahead
25 tokens they accept. */
42 rule_t
**LArule
= NULL
;
47 short *goto_map
= NULL
;
48 state_number_t
*from_state
= NULL
;
49 state_number_t
*to_state
= NULL
;
51 /* And for the famous F variable, which name is so descriptive that a
52 comment is hardly needed. <grin>. */
53 static bitsetv F
= NULL
;
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
;
79 INDEX
[i
] = height
= top
;
82 for (j
= 0; R
[i
][j
] >= 0; ++j
)
84 if (INDEX
[R
[i
][j
]] == 0)
87 if (INDEX
[i
] > INDEX
[R
[i
][j
]])
88 INDEX
[i
] = INDEX
[R
[i
][j
]];
90 bitset_or (F
[i
], F
[i
], F
[R
[i
][j
]]);
93 if (INDEX
[i
] == height
)
102 bitset_copy (F
[j
], F
[i
]);
108 digraph (short **relation
)
112 infinity
= ngotos
+ 2;
113 INDEX
= XCALLOC (short, ngotos
+ 1);
114 VERTICES
= XCALLOC (short, ngotos
+ 1);
119 for (i
= 0; i
< ngotos
; i
++)
122 for (i
= 0; i
< ngotos
; i
++)
123 if (INDEX
[i
] == 0 && R
[i
])
138 /* Avoid having to special case 0. */
142 LA
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
143 LArule
= XCALLOC (rule_t
*, nLA
);
144 lookback
= XCALLOC (shorts
*, nLA
);
147 for (i
= 0; i
< nstates
; i
++)
148 if (!states
[i
]->consistent
)
149 for (j
= 0; j
< states
[i
]->reductions
->nreds
; j
++)
150 *np
++ = &rules
[states
[i
]->reductions
->rules
[j
]];
157 state_number_t state
;
160 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
161 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
164 for (state
= 0; state
< nstates
; ++state
)
166 shifts_t
*sp
= states
[state
]->shifts
;
168 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
170 if (ngotos
== SHRT_MAX
)
171 fatal (_("too many gotos (max %d)"), SHRT_MAX
);
174 goto_map
[SHIFT_SYMBOL (sp
, i
)]++;
181 for (i
= ntokens
; i
< nsyms
; i
++)
187 for (i
= ntokens
; i
< nsyms
; i
++)
188 goto_map
[i
] = temp_map
[i
];
190 goto_map
[nsyms
] = ngotos
;
191 temp_map
[nsyms
] = ngotos
;
194 from_state
= XCALLOC (state_number_t
, ngotos
);
195 to_state
= XCALLOC (state_number_t
, ngotos
);
197 for (state
= 0; state
< nstates
; ++state
)
199 shifts_t
*sp
= states
[state
]->shifts
;
201 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
203 int k
= temp_map
[SHIFT_SYMBOL (sp
, i
)]++;
204 from_state
[k
] = state
;
205 to_state
[k
] = sp
->shifts
[i
];
209 XFREE (temp_map
+ ntokens
);
214 /*----------------------------------------------------------.
215 | Map a state/symbol pair into its numeric representation. |
216 `----------------------------------------------------------*/
219 map_goto (state_number_t state
, symbol_number_t symbol
)
226 low
= goto_map
[symbol
];
227 high
= goto_map
[symbol
+ 1] - 1;
231 middle
= (low
+ high
) / 2;
232 s
= from_state
[middle
];
250 short **reads
= XCALLOC (short *, ngotos
);
251 short *edge
= XCALLOC (short, ngotos
+ 1);
256 F
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
258 for (i
= 0; i
< ngotos
; i
++)
260 state_number_t stateno
= to_state
[i
];
261 shifts_t
*sp
= states
[stateno
]->shifts
;
264 for (j
= 0; j
< sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, j
); j
++)
265 bitset_set (F
[i
], SHIFT_SYMBOL (sp
, j
));
267 for (; j
< sp
->nshifts
; j
++)
269 symbol_number_t symbol
= SHIFT_SYMBOL (sp
, j
);
270 if (nullable
[symbol
])
271 edge
[nedges
++] = map_goto (stateno
, symbol
);
276 reads
[i
] = XCALLOC (short, nedges
+ 1);
277 memcpy (reads
[i
], edge
, nedges
* sizeof (edge
[0]));
278 reads
[i
][nedges
] = -1;
285 for (i
= 0; i
< ngotos
; i
++)
294 add_lookback_edge (state_t
*state
, int ruleno
, int gotono
)
299 for (i
= 0; i
< state
->nlookaheads
; ++i
)
300 if (state
->lookaheads_rule
[i
]->number
== ruleno
)
303 assert (state
->lookaheads_rule
[i
]->number
== ruleno
);
305 sp
= XCALLOC (shorts
, 1);
306 sp
->next
= lookback
[(state
->lookaheads
- LA
) + i
];
308 lookback
[(state
->lookaheads
- LA
) + i
] = sp
;
313 matrix_print (FILE *out
, short **matrix
, int n
)
317 for (i
= 0; i
< n
; ++i
)
319 fprintf (out
, "%3d: ", i
);
321 for (j
= 0; matrix
[i
][j
] != -1; ++j
)
322 fprintf (out
, "%3d ", matrix
[i
][j
]);
328 /*-------------------------------------------------------------------.
329 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
330 | replaced with the result. |
332 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
334 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
336 `-------------------------------------------------------------------*/
339 transpose (short **R_arg
, int n
)
342 short **new_R
= XCALLOC (short *, n
);
343 /* END_R[I] -- next entry of NEW_R[I]. */
344 short **end_R
= XCALLOC (short *, n
);
345 /* NEDGES[I] -- total size of NEW_R[I]. */
346 short *nedges
= XCALLOC (short, n
);
351 fputs ("transpose: input\n", stderr
);
352 matrix_print (stderr
, R_arg
, n
);
356 for (i
= 0; i
< n
; i
++)
358 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
359 ++nedges
[R_arg
[i
][j
]];
362 for (i
= 0; i
< n
; i
++)
365 short *sp
= XCALLOC (short, nedges
[i
] + 1);
372 for (i
= 0; i
< n
; i
++)
374 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
376 *end_R
[R_arg
[i
][j
]] = i
;
377 ++end_R
[R_arg
[i
][j
]];
383 /* Free the input: it is replaced with the result. */
384 for (i
= 0; i
< n
; i
++)
390 fputs ("transpose: output\n", stderr
);
391 matrix_print (stderr
, new_R
, n
);
399 build_relations (void)
401 short *edge
= XCALLOC (short, ngotos
+ 1);
402 state_number_t
*states1
= XCALLOC (state_number_t
, ritem_longest_rhs () + 1);
405 includes
= XCALLOC (short *, ngotos
);
407 for (i
= 0; i
< ngotos
; i
++)
410 symbol_number_t symbol1
= states
[to_state
[i
]]->accessing_symbol
;
413 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
418 state_t
*state
= states
[from_state
[i
]];
419 states1
[0] = state
->number
;
421 for (rp
= rules
[*rulep
].rhs
; *rp
>= 0; rp
++)
423 shifts_t
*sp
= state
->shifts
;
425 for (j
= 0; j
< sp
->nshifts
; j
++)
427 state
= states
[sp
->shifts
[j
]];
428 if (state
->accessing_symbol
429 == item_number_as_symbol_number (*rp
))
433 states1
[length
++] = state
->number
;
436 if (!state
->consistent
)
437 add_lookback_edge (state
, *rulep
, i
);
445 /* JF added rp>=ritem && I hope to god its right! */
446 if (rp
>= ritem
&& ISVAR (*rp
))
448 /* Downcasting from item_number_t to symbol_number_t. */
449 edge
[nedges
++] = map_goto (states1
[--length
],
450 item_number_as_symbol_number (*rp
));
460 includes
[i
] = XCALLOC (short, nedges
+ 1);
461 for (j
= 0; j
< nedges
; j
++)
462 includes
[i
][j
] = edge
[j
];
463 includes
[i
][nedges
] = -1;
470 includes
= transpose (includes
, ngotos
);
476 compute_FOLLOWS (void)
482 for (i
= 0; i
< ngotos
; i
++)
490 compute_lookaheads (void)
495 for (i
= 0; i
< nLA
; i
++)
496 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
497 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
500 for (i
= 0; i
< nLA
; i
++)
501 LIST_FREE (shorts
, lookback
[i
]);
508 /*-------------------------------------------------------------.
509 | Count the number of lookaheads required for each state |
510 | (NLOOKAHEADS member). Compute the total number of LA, NLA. |
511 `-------------------------------------------------------------*/
514 states_lookaheads_count (void)
520 for (i
= 0; i
< nstates
; i
++)
524 reductions
*rp
= states
[i
]->reductions
;
525 shifts_t
*sp
= states
[i
]->shifts
;
527 /* We need a lookahead either to distinguish different
528 reductions (i.e., there are two or more), or to distinguish a
529 reduction from a shift. Otherwise, it is straightforward,
530 and the state is `consistent'. */
532 || (rp
->nreds
== 1 && sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0)))
533 nlookaheads
+= rp
->nreds
;
535 states
[i
]->consistent
= 1;
537 for (k
= 0; k
< sp
->nshifts
; k
++)
538 if (SHIFT_IS_ERROR (sp
, k
))
540 states
[i
]->consistent
= 0;
544 states
[i
]->nlookaheads
= nlookaheads
;
550 /*--------------------------------------.
551 | Initializing the lookaheads members. |
552 `--------------------------------------*/
555 states_lookaheads_initialize (void)
559 rule_t
**pLArule
= LArule
;
561 /* Initialize the members LOOKAHEADS and LOOKAHEADS_RULE for each
563 for (i
= 0; i
< nstates
; i
++)
565 states
[i
]->lookaheads
= pLA
;
566 states
[i
]->lookaheads_rule
= pLArule
;
567 pLA
+= states
[i
]->nlookaheads
;
568 pLArule
+= states
[i
]->nlookaheads
;
573 /*---------------------------------------.
574 | Output the lookaheads for each state. |
575 `---------------------------------------*/
578 lookaheads_print (FILE *out
)
582 fprintf (out
, "Lookaheads: BEGIN\n");
583 for (i
= 0; i
< nstates
; ++i
)
585 fprintf (out
, "State %d: %d lookaheads\n",
586 i
, states
[i
]->nlookaheads
);
588 for (j
= 0; j
< states
[i
]->nlookaheads
; ++j
)
589 for (k
= 0; k
< ntokens
; ++k
)
590 if (bitset_test (states
[i
]->lookaheads
[j
], k
))
591 fprintf (out
, " on %d (%s) -> rule %d\n",
592 k
, symbol_tag_get (symbols
[k
]),
593 states
[i
]->lookaheads_rule
[j
]->number
- 1);
595 fprintf (out
, "Lookaheads: END\n");
601 states_lookaheads_count ();
603 states_lookaheads_initialize ();
608 compute_lookaheads ();
611 lookaheads_print (stderr
);