]>
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 /* All the decorated states, indexed by the state number. */
43 state_t
**states
= NULL
;
45 rule_t
**LArule
= NULL
;
50 short *goto_map
= NULL
;
51 short *from_state
= NULL
;
52 short *to_state
= NULL
;
54 /* And for the famous F variable, which name is so descriptive that a
55 comment is hardly needed. <grin>. */
56 static bitsetv F
= NULL
;
58 static short **includes
;
59 static shorts
**lookback
;
62 /*---------------------------------------------------------------.
63 | digraph & traverse. |
65 | The following variables are used as common storage between the |
67 `---------------------------------------------------------------*/
71 static short *VERTICES
;
82 INDEX
[i
] = height
= top
;
85 for (j
= 0; R
[i
][j
] >= 0; ++j
)
87 if (INDEX
[R
[i
][j
]] == 0)
90 if (INDEX
[i
] > INDEX
[R
[i
][j
]])
91 INDEX
[i
] = INDEX
[R
[i
][j
]];
93 bitset_or (F
[i
], F
[i
], F
[R
[i
][j
]]);
96 if (INDEX
[i
] == height
)
105 bitset_copy (F
[j
], F
[i
]);
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
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
146 LArule
= XCALLOC (rule_t
*, nLA
);
147 lookback
= XCALLOC (shorts
*, nLA
);
150 for (i
= 0; i
< nstates
; i
++)
151 if (!states
[i
]->consistent
)
152 for (j
= 0; j
< states
[i
]->reductions
->nreds
; j
++)
153 *np
++ = &rules
[states
[i
]->reductions
->rules
[j
]];
164 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
165 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
168 for (state
= 0; state
< nstates
; ++state
)
170 shifts
*sp
= states
[state
]->shifts
;
171 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
173 if (ngotos
== SHRT_MAX
)
174 fatal (_("too many gotos (max %d)"), SHRT_MAX
);
177 goto_map
[SHIFT_SYMBOL (sp
, i
)]++;
183 for (i
= ntokens
; i
< nsyms
; i
++)
189 for (i
= ntokens
; i
< nsyms
; i
++)
190 goto_map
[i
] = temp_map
[i
];
192 goto_map
[nsyms
] = ngotos
;
193 temp_map
[nsyms
] = ngotos
;
196 from_state
= XCALLOC (short, ngotos
);
197 to_state
= XCALLOC (short, ngotos
);
199 for (state
= 0; state
< nstates
; ++state
)
201 shifts
*sp
= states
[state
]->shifts
;
202 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
204 int k
= temp_map
[SHIFT_SYMBOL (sp
, i
)]++;
205 from_state
[k
] = state
;
206 to_state
[k
] = sp
->shifts
[i
];
210 XFREE (temp_map
+ ntokens
);
215 /*----------------------------------------------------------.
216 | Map a state/symbol pair into its numeric representation. |
217 `----------------------------------------------------------*/
220 map_goto (int state
, token_number_t symbol
)
227 low
= goto_map
[symbol
];
228 high
= goto_map
[symbol
+ 1] - 1;
232 middle
= (low
+ high
) / 2;
233 s
= from_state
[middle
];
251 short **reads
= XCALLOC (short *, ngotos
);
252 short *edge
= XCALLOC (short, ngotos
+ 1);
257 F
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
259 for (i
= 0; i
< ngotos
; i
++)
261 int stateno
= to_state
[i
];
262 shifts
*sp
= states
[stateno
]->shifts
;
265 for (j
= 0; j
< sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, j
); j
++)
266 bitset_set (F
[i
], SHIFT_SYMBOL (sp
, j
));
268 for (; j
< sp
->nshifts
; j
++)
270 token_number_t symbol
= SHIFT_SYMBOL (sp
, j
);
271 if (nullable
[symbol
])
272 edge
[nedges
++] = map_goto (stateno
, symbol
);
277 reads
[i
] = XCALLOC (short, nedges
+ 1);
278 memcpy (reads
[i
], edge
, nedges
* sizeof (edge
[0]));
279 reads
[i
][nedges
] = -1;
286 for (i
= 0; i
< ngotos
; i
++)
295 add_lookback_edge (state_t
*state
, int ruleno
, int gotono
)
300 for (i
= 0; i
< state
->nlookaheads
; ++i
)
301 if (LArule
[state
->lookaheadsp
+ i
]->number
== ruleno
)
304 assert (LArule
[state
->lookaheadsp
+ i
]->number
== ruleno
);
306 sp
= XCALLOC (shorts
, 1);
307 sp
->next
= lookback
[state
->lookaheadsp
+ i
];
309 lookback
[state
->lookaheadsp
+ i
] = sp
;
314 matrix_print (FILE *out
, short **matrix
, int n
)
318 for (i
= 0; i
< n
; ++i
)
320 fprintf (out
, "%3d: ", i
);
322 for (j
= 0; matrix
[i
][j
] != -1; ++j
)
323 fprintf (out
, "%3d ", matrix
[i
][j
]);
329 /*-------------------------------------------------------------------.
330 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
331 | replaced with the result. |
333 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
335 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
337 `-------------------------------------------------------------------*/
340 transpose (short **R_arg
, int n
)
343 short **new_R
= XCALLOC (short *, n
);
344 /* END_R[I] -- next entry of NEW_R[I]. */
345 short **end_R
= XCALLOC (short *, n
);
346 /* NEDGES[I] -- total size of NEW_R[I]. */
347 short *nedges
= XCALLOC (short, n
);
352 fputs ("transpose: input\n", stderr
);
353 matrix_print (stderr
, R_arg
, n
);
357 for (i
= 0; i
< n
; i
++)
359 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
360 ++nedges
[R_arg
[i
][j
]];
363 for (i
= 0; i
< n
; i
++)
366 short *sp
= XCALLOC (short, nedges
[i
] + 1);
373 for (i
= 0; i
< n
; i
++)
375 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
377 *end_R
[R_arg
[i
][j
]] = i
;
378 ++end_R
[R_arg
[i
][j
]];
384 /* Free the input: it is replaced with the result. */
385 for (i
= 0; i
< n
; i
++)
391 fputs ("transpose: output\n", stderr
);
392 matrix_print (stderr
, new_R
, n
);
400 build_relations (void)
402 short *edge
= XCALLOC (short, ngotos
+ 1);
403 short *states1
= XCALLOC (short, ritem_longest_rhs () + 1);
406 includes
= XCALLOC (short *, ngotos
);
408 for (i
= 0; i
< ngotos
; i
++)
411 token_number_t symbol1
= states
[to_state
[i
]]->accessing_symbol
;
414 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
419 state_t
*state
= states
[from_state
[i
]];
420 states1
[0] = state
->number
;
422 for (rp
= rules
[*rulep
].rhs
; *rp
>= 0; rp
++)
424 shifts
*sp
= state
->shifts
;
426 for (j
= 0; j
< sp
->nshifts
; j
++)
428 state
= states
[sp
->shifts
[j
]];
429 if (state
->accessing_symbol
430 == item_number_as_token_number (*rp
))
434 states1
[length
++] = state
->number
;
437 if (!state
->consistent
)
438 add_lookback_edge (state
, *rulep
, i
);
446 /* JF added rp>=ritem && I hope to god its right! */
447 if (rp
>= ritem
&& ISVAR (*rp
))
449 /* Downcasting from item_number_t to token_number_t. */
450 edge
[nedges
++] = map_goto (states1
[--length
],
451 item_number_as_token_number (*rp
));
461 includes
[i
] = XCALLOC (short, nedges
+ 1);
462 for (j
= 0; j
< nedges
; j
++)
463 includes
[i
][j
] = edge
[j
];
464 includes
[i
][nedges
] = -1;
471 includes
= transpose (includes
, ngotos
);
477 compute_FOLLOWS (void)
483 for (i
= 0; i
< ngotos
; i
++)
491 compute_lookaheads (void)
496 for (i
= 0; i
< nLA
; i
++)
497 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
498 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
501 for (i
= 0; i
< nLA
; i
++)
502 LIST_FREE (shorts
, lookback
[i
]);
509 /*--------------------------------------.
510 | Initializing the lookaheads members. |
511 `--------------------------------------*/
514 initialize_lookaheads (void)
518 for (i
= 0; i
< nstates
; i
++)
522 reductions
*rp
= states
[i
]->reductions
;
523 shifts
*sp
= states
[i
]->shifts
;
525 /* We need a lookahead either to distinguish different
526 reductions (i.e., there are two or more), or to distinguish a
527 reduction from a shift. Otherwise, it is straightforward,
528 and the state is `consistent'. */
530 || (rp
->nreds
== 1 && sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0)))
531 nlookaheads
+= rp
->nreds
;
533 states
[i
]->consistent
= 1;
535 for (k
= 0; k
< sp
->nshifts
; k
++)
536 if (SHIFT_IS_ERROR (sp
, k
))
538 states
[i
]->consistent
= 0;
542 states
[i
]->nlookaheads
= nlookaheads
;
543 states
[i
]->lookaheadsp
= nLA
;
549 /*---------------------------------------.
550 | Output the lookaheads for each state. |
551 `---------------------------------------*/
554 lookaheads_print (FILE *out
)
558 fprintf (out
, "Lookaheads: BEGIN\n");
559 for (i
= 0; i
< nstates
; ++i
)
561 fprintf (out
, "State %d: %d lookaheads\n",
562 i
, states
[i
]->nlookaheads
);
564 for (j
= 0; j
< states
[i
]->nlookaheads
; ++j
)
565 for (k
= 0; k
< ntokens
; ++k
)
566 if (bitset_test (LA
[states
[i
]->lookaheadsp
+ j
], k
))
567 fprintf (out
, " on %d (%s) -> rule %d\n",
568 k
, quotearg_style (escape_quoting_style
, symbols
[k
]->tag
),
569 LArule
[states
[i
]->lookaheadsp
+ j
]->number
- 1);
571 fprintf (out
, "Lookaheads: END\n");
577 initialize_lookaheads ();
583 compute_lookaheads ();
586 lookaheads_print (stderr
);