]>
git.saurik.com Git - bison.git/blob - src/lalr.c
5b2761c3da87b0c7d776ac7a397e5aa56d4ebf76
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
;
42 short *LAruleno
= NULL
;
47 short *goto_map
= NULL
;
48 short *from_state
= NULL
;
49 short *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 bitset
*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
= XCALLOC (bitset
, nLA
);
143 for (i
= 0; i
< nLA
; ++i
)
145 LA
[i
] = bitset_create (ntokens
, BITSET_FIXED
);
148 LAruleno
= XCALLOC (short, nLA
);
149 lookback
= XCALLOC (shorts
*, nLA
);
152 for (i
= 0; i
< nstates
; i
++)
153 if (!states
[i
]->consistent
)
154 for (j
= 0; j
< states
[i
]->reductions
->nreds
; j
++)
155 *np
++ = states
[i
]->reductions
->rules
[j
];
166 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
167 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
170 for (state
= 0; state
< nstates
; ++state
)
172 shifts
*sp
= states
[state
]->shifts
;
173 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
175 if (ngotos
== MAXSHORT
)
176 fatal (_("too many gotos (max %d)"), MAXSHORT
);
179 goto_map
[SHIFT_SYMBOL (sp
, i
)]++;
185 for (i
= ntokens
; i
< nsyms
; i
++)
191 for (i
= ntokens
; i
< nsyms
; i
++)
192 goto_map
[i
] = temp_map
[i
];
194 goto_map
[nsyms
] = ngotos
;
195 temp_map
[nsyms
] = ngotos
;
198 from_state
= XCALLOC (short, ngotos
);
199 to_state
= XCALLOC (short, ngotos
);
201 for (state
= 0; state
< nstates
; ++state
)
203 shifts
*sp
= states
[state
]->shifts
;
204 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
206 int k
= temp_map
[SHIFT_SYMBOL (sp
, i
)]++;
207 from_state
[k
] = state
;
208 to_state
[k
] = sp
->shifts
[i
];
212 XFREE (temp_map
+ ntokens
);
217 /*----------------------------------------------------------.
218 | Map a state/symbol pair into its numeric representation. |
219 `----------------------------------------------------------*/
222 map_goto (int state
, int symbol
)
229 low
= goto_map
[symbol
];
230 high
= goto_map
[symbol
+ 1] - 1;
234 middle
= (low
+ high
) / 2;
235 s
= from_state
[middle
];
253 short **reads
= XCALLOC (short *, ngotos
);
254 short *edge
= XCALLOC (short, ngotos
+ 1);
259 F
= XCALLOC (bitset
, ngotos
);
260 for (i
= 0; i
< ngotos
; ++i
)
262 F
[i
] = bitset_create (ntokens
, BITSET_FIXED
);
266 for (i
= 0; i
< ngotos
; i
++)
268 int stateno
= to_state
[i
];
269 shifts
*sp
= states
[stateno
]->shifts
;
272 for (j
= 0; j
< sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, j
); j
++)
273 bitset_set (F
[i
], SHIFT_SYMBOL (sp
, j
));
275 for (; j
< sp
->nshifts
; j
++)
277 int symbol
= SHIFT_SYMBOL (sp
, j
);
278 if (nullable
[symbol
])
279 edge
[nedges
++] = map_goto (stateno
, symbol
);
284 reads
[i
] = XCALLOC (short, nedges
+ 1);
285 shortcpy (reads
[i
], edge
, nedges
);
286 reads
[i
][nedges
] = -1;
293 for (i
= 0; i
< ngotos
; i
++)
302 add_lookback_edge (state_t
*state
, int ruleno
, int gotono
)
307 for (i
= 0; i
< state
->nlookaheads
; ++i
)
308 if (LAruleno
[state
->lookaheadsp
+ i
] == ruleno
)
311 assert (LAruleno
[state
->lookaheadsp
+ i
] == ruleno
);
313 sp
= XCALLOC (shorts
, 1);
314 sp
->next
= lookback
[state
->lookaheadsp
+ i
];
316 lookback
[state
->lookaheadsp
+ i
] = sp
;
321 matrix_print (FILE *out
, short **matrix
, int n
)
325 for (i
= 0; i
< n
; ++i
)
327 fprintf (out
, "%3d: ", i
);
329 for (j
= 0; matrix
[i
][j
] != -1; ++j
)
330 fprintf (out
, "%3d ", matrix
[i
][j
]);
336 /*-------------------------------------------------------------------.
337 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
338 | replaced with the result. |
340 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
342 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
344 `-------------------------------------------------------------------*/
347 transpose (short **R_arg
, int n
)
350 short **new_R
= XCALLOC (short *, n
);
351 /* END_R[I] -- next entry of NEW_R[I]. */
352 short **end_R
= XCALLOC (short *, n
);
353 /* NEDGES[I] -- total size of NEW_R[I]. */
354 short *nedges
= XCALLOC (short, n
);
359 fputs ("transpose: input\n", stderr
);
360 matrix_print (stderr
, R_arg
, n
);
364 for (i
= 0; i
< n
; i
++)
366 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
367 ++nedges
[R_arg
[i
][j
]];
370 for (i
= 0; i
< n
; i
++)
373 short *sp
= XCALLOC (short, nedges
[i
] + 1);
380 for (i
= 0; i
< n
; i
++)
382 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
384 *end_R
[R_arg
[i
][j
]] = i
;
385 ++end_R
[R_arg
[i
][j
]];
391 /* Free the input: it is replaced with the result. */
392 for (i
= 0; i
< n
; i
++)
398 fputs ("transpose: output\n", stderr
);
399 matrix_print (stderr
, new_R
, n
);
407 build_relations (void)
409 short *edge
= XCALLOC (short, ngotos
+ 1);
410 short *states1
= XCALLOC (short, ritem_longest_rhs () + 1);
413 includes
= XCALLOC (short *, ngotos
);
415 for (i
= 0; i
< ngotos
; i
++)
418 int symbol1
= states
[to_state
[i
]]->accessing_symbol
;
421 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
426 state_t
*state
= states
[from_state
[i
]];
427 states1
[0] = state
->number
;
429 for (rp
= &ritem
[rules
[*rulep
].rhs
]; *rp
>= 0; rp
++)
431 shifts
*sp
= state
->shifts
;
433 for (j
= 0; j
< sp
->nshifts
; j
++)
435 state
= states
[sp
->shifts
[j
]];
436 if (state
->accessing_symbol
== *rp
)
440 states1
[length
++] = state
->number
;
443 if (!state
->consistent
)
444 add_lookback_edge (state
, *rulep
, i
);
452 /* JF added rp>=ritem && I hope to god its right! */
453 if (rp
>= ritem
&& ISVAR (*rp
))
455 edge
[nedges
++] = map_goto (states1
[--length
], *rp
);
465 includes
[i
] = XCALLOC (short, nedges
+ 1);
466 for (j
= 0; j
< nedges
; j
++)
467 includes
[i
][j
] = edge
[j
];
468 includes
[i
][nedges
] = -1;
475 includes
= transpose (includes
, ngotos
);
481 compute_FOLLOWS (void)
487 for (i
= 0; i
< ngotos
; i
++)
495 compute_lookaheads (void)
500 for (i
= 0; i
< nLA
; i
++)
501 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
502 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
505 for (i
= 0; i
< nLA
; i
++)
506 LIST_FREE (shorts
, lookback
[i
]);
509 for (i
= 0; i
< (unsigned) ngotos
; ++i
)
515 /*--------------------------------------.
516 | Initializing the lookaheads members. |
517 `--------------------------------------*/
520 initialize_lookaheads (void)
524 for (i
= 0; i
< nstates
; i
++)
528 reductions
*rp
= states
[i
]->reductions
;
529 shifts
*sp
= states
[i
]->shifts
;
531 /* We need a lookahead either to distinguish different
532 reductions (i.e., there are two or more), or to distinguish a
533 reduction from a shift. Otherwise, it is straightforward,
534 and the state is `consistent'. */
536 || (rp
->nreds
== 1 && sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0)))
537 nlookaheads
+= rp
->nreds
;
539 states
[i
]->consistent
= 1;
541 for (k
= 0; k
< sp
->nshifts
; k
++)
542 if (SHIFT_IS_ERROR (sp
, k
))
544 states
[i
]->consistent
= 0;
548 states
[i
]->nlookaheads
= nlookaheads
;
549 states
[i
]->lookaheadsp
= nLA
;
555 /*---------------------------------------.
556 | Output the lookaheads for each state. |
557 `---------------------------------------*/
560 lookaheads_print (FILE *out
)
564 fprintf (out
, "Lookaheads: BEGIN\n");
565 for (i
= 0; i
< nstates
; ++i
)
567 fprintf (out
, "State %d: %d lookaheads\n",
568 i
, states
[i
]->nlookaheads
);
570 for (j
= 0; j
< states
[i
]->nlookaheads
; ++j
)
571 for (k
= 0; k
< ntokens
; ++k
)
572 if (bitset_test (LA
[states
[i
]->lookaheadsp
+ j
], j
))
573 fprintf (out
, " on %d (%s) -> rule %d\n",
575 -LAruleno
[states
[i
]->lookaheadsp
+ j
] - 1);
577 fprintf (out
, "Lookaheads: END\n");
583 initialize_lookaheads ();
589 compute_lookaheads ();
592 lookaheads_print (stderr
);