]>
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. */
40 /* All the decorated states, indexed by the state number. */
41 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 bitsetv 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
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
144 LAruleno
= XCALLOC (short, nLA
);
145 lookback
= XCALLOC (shorts
*, nLA
);
148 for (i
= 0; i
< nstates
; i
++)
149 if (!states
[i
]->consistent
)
150 for (j
= 0; j
< states
[i
]->reductions
->nreds
; j
++)
151 *np
++ = states
[i
]->reductions
->rules
[j
];
162 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
163 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
166 for (state
= 0; state
< nstates
; ++state
)
168 shifts
*sp
= states
[state
]->shifts
;
169 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
171 if (ngotos
== MAXSHORT
)
172 fatal (_("too many gotos (max %d)"), MAXSHORT
);
175 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 (short, ngotos
);
195 to_state
= XCALLOC (short, ngotos
);
197 for (state
= 0; state
< nstates
; ++state
)
199 shifts
*sp
= states
[state
]->shifts
;
200 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
202 int k
= temp_map
[SHIFT_SYMBOL (sp
, i
)]++;
203 from_state
[k
] = state
;
204 to_state
[k
] = sp
->shifts
[i
];
208 XFREE (temp_map
+ ntokens
);
213 /*----------------------------------------------------------.
214 | Map a state/symbol pair into its numeric representation. |
215 `----------------------------------------------------------*/
218 map_goto (int state
, int symbol
)
225 low
= goto_map
[symbol
];
226 high
= goto_map
[symbol
+ 1] - 1;
230 middle
= (low
+ high
) / 2;
231 s
= from_state
[middle
];
249 short **reads
= XCALLOC (short *, ngotos
);
250 short *edge
= XCALLOC (short, ngotos
+ 1);
255 F
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
257 for (i
= 0; i
< ngotos
; i
++)
259 int stateno
= to_state
[i
];
260 shifts
*sp
= states
[stateno
]->shifts
;
263 for (j
= 0; j
< sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, j
); j
++)
264 bitset_set (F
[i
], SHIFT_SYMBOL (sp
, j
));
266 for (; j
< sp
->nshifts
; j
++)
268 int symbol
= SHIFT_SYMBOL (sp
, j
);
269 if (nullable
[symbol
])
270 edge
[nedges
++] = map_goto (stateno
, symbol
);
275 reads
[i
] = XCALLOC (short, nedges
+ 1);
276 shortcpy (reads
[i
], edge
, nedges
);
277 reads
[i
][nedges
] = -1;
284 for (i
= 0; i
< ngotos
; i
++)
293 add_lookback_edge (state_t
*state
, int ruleno
, int gotono
)
298 for (i
= 0; i
< state
->nlookaheads
; ++i
)
299 if (LAruleno
[state
->lookaheadsp
+ i
] == ruleno
)
302 assert (LAruleno
[state
->lookaheadsp
+ i
] == ruleno
);
304 sp
= XCALLOC (shorts
, 1);
305 sp
->next
= lookback
[state
->lookaheadsp
+ i
];
307 lookback
[state
->lookaheadsp
+ i
] = sp
;
312 matrix_print (FILE *out
, short **matrix
, int n
)
316 for (i
= 0; i
< n
; ++i
)
318 fprintf (out
, "%3d: ", i
);
320 for (j
= 0; matrix
[i
][j
] != -1; ++j
)
321 fprintf (out
, "%3d ", matrix
[i
][j
]);
327 /*-------------------------------------------------------------------.
328 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
329 | replaced with the result. |
331 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
333 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
335 `-------------------------------------------------------------------*/
338 transpose (short **R_arg
, int n
)
341 short **new_R
= XCALLOC (short *, n
);
342 /* END_R[I] -- next entry of NEW_R[I]. */
343 short **end_R
= XCALLOC (short *, n
);
344 /* NEDGES[I] -- total size of NEW_R[I]. */
345 short *nedges
= XCALLOC (short, n
);
350 fputs ("transpose: input\n", stderr
);
351 matrix_print (stderr
, R_arg
, n
);
355 for (i
= 0; i
< n
; i
++)
357 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
358 ++nedges
[R_arg
[i
][j
]];
361 for (i
= 0; i
< n
; i
++)
364 short *sp
= XCALLOC (short, nedges
[i
] + 1);
371 for (i
= 0; i
< n
; i
++)
373 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
375 *end_R
[R_arg
[i
][j
]] = i
;
376 ++end_R
[R_arg
[i
][j
]];
382 /* Free the input: it is replaced with the result. */
383 for (i
= 0; i
< n
; i
++)
389 fputs ("transpose: output\n", stderr
);
390 matrix_print (stderr
, new_R
, n
);
398 build_relations (void)
400 short *edge
= XCALLOC (short, ngotos
+ 1);
401 short *states1
= XCALLOC (short, ritem_longest_rhs () + 1);
404 includes
= XCALLOC (short *, ngotos
);
406 for (i
= 0; i
< ngotos
; i
++)
409 int symbol1
= states
[to_state
[i
]]->accessing_symbol
;
412 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
417 state_t
*state
= states
[from_state
[i
]];
418 states1
[0] = state
->number
;
420 for (rp
= rules
[*rulep
].rhs
; *rp
>= 0; rp
++)
422 shifts
*sp
= state
->shifts
;
424 for (j
= 0; j
< sp
->nshifts
; j
++)
426 state
= states
[sp
->shifts
[j
]];
427 if (state
->accessing_symbol
== *rp
)
431 states1
[length
++] = state
->number
;
434 if (!state
->consistent
)
435 add_lookback_edge (state
, *rulep
, i
);
443 /* JF added rp>=ritem && I hope to god its right! */
444 if (rp
>= ritem
&& ISVAR (*rp
))
446 edge
[nedges
++] = map_goto (states1
[--length
], *rp
);
456 includes
[i
] = XCALLOC (short, nedges
+ 1);
457 for (j
= 0; j
< nedges
; j
++)
458 includes
[i
][j
] = edge
[j
];
459 includes
[i
][nedges
] = -1;
466 includes
= transpose (includes
, ngotos
);
472 compute_FOLLOWS (void)
478 for (i
= 0; i
< ngotos
; i
++)
486 compute_lookaheads (void)
491 for (i
= 0; i
< nLA
; i
++)
492 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
493 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
496 for (i
= 0; i
< nLA
; i
++)
497 LIST_FREE (shorts
, lookback
[i
]);
504 /*--------------------------------------.
505 | Initializing the lookaheads members. |
506 `--------------------------------------*/
509 initialize_lookaheads (void)
513 for (i
= 0; i
< nstates
; i
++)
517 reductions
*rp
= states
[i
]->reductions
;
518 shifts
*sp
= states
[i
]->shifts
;
520 /* We need a lookahead either to distinguish different
521 reductions (i.e., there are two or more), or to distinguish a
522 reduction from a shift. Otherwise, it is straightforward,
523 and the state is `consistent'. */
525 || (rp
->nreds
== 1 && sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0)))
526 nlookaheads
+= rp
->nreds
;
528 states
[i
]->consistent
= 1;
530 for (k
= 0; k
< sp
->nshifts
; k
++)
531 if (SHIFT_IS_ERROR (sp
, k
))
533 states
[i
]->consistent
= 0;
537 states
[i
]->nlookaheads
= nlookaheads
;
538 states
[i
]->lookaheadsp
= nLA
;
544 /*---------------------------------------.
545 | Output the lookaheads for each state. |
546 `---------------------------------------*/
549 lookaheads_print (FILE *out
)
553 fprintf (out
, "Lookaheads: BEGIN\n");
554 for (i
= 0; i
< nstates
; ++i
)
556 fprintf (out
, "State %d: %d lookaheads\n",
557 i
, states
[i
]->nlookaheads
);
559 for (j
= 0; j
< states
[i
]->nlookaheads
; ++j
)
560 for (k
= 0; k
< ntokens
; ++k
)
561 if (bitset_test (LA
[states
[i
]->lookaheadsp
+ j
], j
))
562 fprintf (out
, " on %d (%s) -> rule %d\n",
564 -LAruleno
[states
[i
]->lookaheadsp
+ j
] - 1);
566 fprintf (out
, "Lookaheads: END\n");
572 initialize_lookaheads ();
578 compute_lookaheads ();
581 lookaheads_print (stderr
);