]>
git.saurik.com Git - bison.git/blob - src/lalr.c
db9abfa3c8c0226474aa92613b6f0817068eb283
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. */
36 /* All the decorated states, indexed by the state number. */
37 state_t
**state_table
= NULL
;
49 /* And for the famous F variable, which name is so descriptive that a
50 comment is hardly needed. <grin>. */
51 static unsigned *F
= NULL
;
52 #define F(Rule) (F + (Rule) * tokensetsize)
54 static short **includes
;
55 static shorts
**lookback
;
58 /*---------------------------------------------------------------.
59 | digraph & traverse. |
61 | The following variables are used as common storage between the |
63 `---------------------------------------------------------------*/
67 static short *VERTICES
;
77 size_t size
= F (i
+ 1) - F(i
);
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 for (k
= 0; k
< size
; ++k
)
92 F (i
)[k
] |= F (R
[i
][j
])[k
];
95 if (INDEX
[i
] == height
)
104 for (k
= 0; k
< size
; ++k
)
111 digraph (short **relation
)
115 infinity
= ngotos
+ 2;
116 INDEX
= XCALLOC (short, ngotos
+ 1);
117 VERTICES
= XCALLOC (short, ngotos
+ 1);
122 for (i
= 0; i
< ngotos
; i
++)
125 for (i
= 0; i
< ngotos
; i
++)
126 if (INDEX
[i
] == 0 && R
[i
])
141 /* Avoid having to special case 0. */
145 LA
= XCALLOC (unsigned, nLA
* tokensetsize
);
146 LAruleno
= XCALLOC (short, nLA
);
147 lookback
= XCALLOC (shorts
*, nLA
);
150 for (i
= 0; i
< nstates
; i
++)
151 if (!state_table
[i
]->consistent
)
152 for (j
= 0; j
< state_table
[i
]->reductions
->nreds
; j
++)
153 *np
++ = state_table
[i
]->reductions
->rules
[j
];
163 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
164 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
167 for (state
= 0; state
< nstates
; ++state
)
169 shifts
*sp
= state_table
[state
]->shifts
;
170 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
172 if (ngotos
== MAXSHORT
)
173 fatal (_("too many gotos (max %d)"), MAXSHORT
);
176 goto_map
[SHIFT_SYMBOL (sp
, i
)]++;
182 for (i
= ntokens
; i
< nsyms
; i
++)
188 for (i
= ntokens
; i
< nsyms
; i
++)
189 goto_map
[i
] = temp_map
[i
];
191 goto_map
[nsyms
] = ngotos
;
192 temp_map
[nsyms
] = ngotos
;
195 from_state
= XCALLOC (short, ngotos
);
196 to_state
= XCALLOC (short, ngotos
);
198 for (state
= 0; state
< nstates
; ++state
)
200 shifts
*sp
= state_table
[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 (int state
, int 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
= XCALLOC (unsigned, ngotos
* tokensetsize
);
258 for (i
= 0; i
< ngotos
; i
++)
260 int stateno
= to_state
[i
];
261 shifts
*sp
= state_table
[stateno
]->shifts
;
264 for (j
= 0; j
< sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, j
); j
++)
265 SETBIT (F (i
), SHIFT_SYMBOL (sp
, j
));
267 for (; j
< sp
->nshifts
; j
++)
269 int symbol
= SHIFT_SYMBOL (sp
, j
);
270 if (nullable
[symbol
])
271 edge
[nedges
++] = map_goto (stateno
, symbol
);
276 reads
[i
] = XCALLOC (short, nedges
+ 1);
277 shortcpy (reads
[i
], edge
, nedges
);
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 (LAruleno
[state
->lookaheadsp
+ i
] == ruleno
)
303 assert (LAruleno
[state
->lookaheadsp
+ i
] == ruleno
);
305 sp
= XCALLOC (shorts
, 1);
306 sp
->next
= lookback
[state
->lookaheadsp
+ i
];
308 lookback
[state
->lookaheadsp
+ 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 short *states
= XCALLOC (short, ritem_longest_rhs () + 1);
405 includes
= XCALLOC (short *, ngotos
);
407 for (i
= 0; i
< ngotos
; i
++)
410 int symbol1
= state_table
[to_state
[i
]]->accessing_symbol
;
413 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
418 state_t
*state
= state_table
[from_state
[i
]];
419 states
[0] = state
->number
;
421 for (rp
= ritem
+ rule_table
[*rulep
].rhs
; *rp
> 0; rp
++)
423 shifts
*sp
= state
->shifts
;
425 for (j
= 0; j
< sp
->nshifts
; j
++)
427 state
= state_table
[sp
->shifts
[j
]];
428 if (state
->accessing_symbol
== *rp
)
432 states
[length
++] = state
->number
;
435 if (!state
->consistent
)
436 add_lookback_edge (state
, *rulep
, i
);
444 /* JF added rp>=ritem && I hope to god its right! */
445 if (rp
>= ritem
&& ISVAR (*rp
))
447 edge
[nedges
++] = map_goto (states
[--length
], *rp
);
457 includes
[i
] = XCALLOC (short, nedges
+ 1);
458 for (j
= 0; j
< nedges
; j
++)
459 includes
[i
][j
] = edge
[j
];
460 includes
[i
][nedges
] = -1;
467 includes
= transpose (includes
, ngotos
);
473 compute_FOLLOWS (void)
479 for (i
= 0; i
< ngotos
; i
++)
487 compute_lookaheads (void)
492 for (i
= 0; i
< nLA
; i
++)
493 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
495 int size
= LA (i
+ 1) - LA (i
);
497 for (j
= 0; j
< size
; ++j
)
498 LA (i
)[j
] |= F (sp
->value
)[j
];
502 for (i
= 0; i
< nLA
; i
++)
503 LIST_FREE (shorts
, lookback
[i
]);
510 /*--------------------------------------.
511 | Initializing the lookaheads members. |
512 `--------------------------------------*/
515 initialize_lookaheads (void)
519 for (i
= 0; i
< nstates
; i
++)
523 reductions
*rp
= state_table
[i
]->reductions
;
524 shifts
*sp
= state_table
[i
]->shifts
;
526 /* We need a lookahead either to distinguish different
527 reductions (i.e., there are two or more), or to distinguish a
528 reduction from a shift. Otherwise, it is straightforward,
529 and the state is `consistent'. */
531 || (rp
->nreds
== 1 && sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0)))
532 nlookaheads
+= rp
->nreds
;
534 state_table
[i
]->consistent
= 1;
536 for (k
= 0; k
< sp
->nshifts
; k
++)
537 if (SHIFT_IS_ERROR (sp
, k
))
539 state_table
[i
]->consistent
= 0;
543 state_table
[i
]->nlookaheads
= nlookaheads
;
544 state_table
[i
]->lookaheadsp
= nLA
;
552 tokensetsize
= WORDSIZE (ntokens
);
554 initialize_lookaheads ();
560 compute_lookaheads ();