]>
git.saurik.com Git - bison.git/blob - src/lalr.c
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
, rule_number_t 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
;
411 rule_number_t
*rulep
;
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 state
= shifts_to (state
->shifts
,
424 item_number_as_symbol_number (*rp
));
425 states1
[length
++] = state
->number
;
428 if (!state
->consistent
)
429 add_lookback_edge (state
, *rulep
, i
);
437 /* JF added rp>=ritem && I hope to god its right! */
438 if (rp
>= ritem
&& ISVAR (*rp
))
440 /* Downcasting from item_number_t to symbol_number_t. */
441 edge
[nedges
++] = map_goto (states1
[--length
],
442 item_number_as_symbol_number (*rp
));
452 includes
[i
] = XCALLOC (short, nedges
+ 1);
453 for (j
= 0; j
< nedges
; j
++)
454 includes
[i
][j
] = edge
[j
];
455 includes
[i
][nedges
] = -1;
462 includes
= transpose (includes
, ngotos
);
468 compute_FOLLOWS (void)
474 for (i
= 0; i
< ngotos
; i
++)
482 compute_lookaheads (void)
487 for (i
= 0; i
< nLA
; i
++)
488 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
489 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
492 for (i
= 0; i
< nLA
; i
++)
493 LIST_FREE (shorts
, lookback
[i
]);
500 /*-------------------------------------------------------------.
501 | Count the number of lookaheads required for each state |
502 | (NLOOKAHEADS member). Compute the total number of LA, NLA. |
503 `-------------------------------------------------------------*/
506 states_lookaheads_count (void)
512 for (i
= 0; i
< nstates
; i
++)
516 reductions_t
*rp
= states
[i
]->reductions
;
517 shifts_t
*sp
= states
[i
]->shifts
;
519 /* We need a lookahead either to distinguish different
520 reductions (i.e., there are two or more), or to distinguish a
521 reduction from a shift. Otherwise, it is straightforward,
522 and the state is `consistent'. */
524 || (rp
->nreds
== 1 && sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0)))
525 nlookaheads
+= rp
->nreds
;
527 states
[i
]->consistent
= 1;
529 for (k
= 0; k
< sp
->nshifts
; k
++)
530 if (SHIFT_IS_ERROR (sp
, k
))
532 states
[i
]->consistent
= 0;
536 states
[i
]->nlookaheads
= nlookaheads
;
542 /*--------------------------------------.
543 | Initializing the lookaheads members. |
544 `--------------------------------------*/
547 states_lookaheads_initialize (void)
551 rule_t
**pLArule
= LArule
;
553 /* Initialize the members LOOKAHEADS and LOOKAHEADS_RULE for each
555 for (i
= 0; i
< nstates
; i
++)
557 states
[i
]->lookaheads
= pLA
;
558 states
[i
]->lookaheads_rule
= pLArule
;
559 pLA
+= states
[i
]->nlookaheads
;
560 pLArule
+= states
[i
]->nlookaheads
;
565 /*---------------------------------------.
566 | Output the lookaheads for each state. |
567 `---------------------------------------*/
570 lookaheads_print (FILE *out
)
574 fprintf (out
, "Lookaheads: BEGIN\n");
575 for (i
= 0; i
< nstates
; ++i
)
577 fprintf (out
, "State %d: %d lookaheads\n",
578 i
, states
[i
]->nlookaheads
);
580 for (j
= 0; j
< states
[i
]->nlookaheads
; ++j
)
581 for (k
= 0; k
< ntokens
; ++k
)
582 if (bitset_test (states
[i
]->lookaheads
[j
], k
))
583 fprintf (out
, " on %d (%s) -> rule %d\n",
584 k
, symbol_tag_get (symbols
[k
]),
585 states
[i
]->lookaheads_rule
[j
]->number
- 1);
587 fprintf (out
, "Lookaheads: END\n");
593 states_lookaheads_count ();
595 states_lookaheads_initialize ();
600 compute_lookaheads ();
603 lookaheads_print (stderr
);