]>
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. */
43 rule_t
**LArule
= NULL
;
48 short *goto_map
= NULL
;
49 state_number_t
*from_state
= NULL
;
50 state_number_t
*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
;
69 /* Avoid having to special case 0. */
73 LA
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
74 LArule
= XCALLOC (rule_t
*, nLA
);
75 lookback
= XCALLOC (shorts
*, nLA
);
78 for (i
= 0; i
< nstates
; i
++)
79 if (!states
[i
]->consistent
)
80 for (j
= 0; j
< states
[i
]->reductions
->num
; j
++)
81 *np
++ = &rules
[states
[i
]->reductions
->rules
[j
]];
91 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
92 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
95 for (state
= 0; state
< nstates
; ++state
)
97 transitions_t
*sp
= states
[state
]->shifts
;
99 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
101 if (ngotos
== SHRT_MAX
)
102 fatal (_("too many gotos (max %d)"), SHRT_MAX
);
105 goto_map
[TRANSITION_SYMBOL (sp
, i
)]++;
112 for (i
= ntokens
; i
< nsyms
; i
++)
118 for (i
= ntokens
; i
< nsyms
; i
++)
119 goto_map
[i
] = temp_map
[i
];
121 goto_map
[nsyms
] = ngotos
;
122 temp_map
[nsyms
] = ngotos
;
125 from_state
= XCALLOC (state_number_t
, ngotos
);
126 to_state
= XCALLOC (state_number_t
, ngotos
);
128 for (state
= 0; state
< nstates
; ++state
)
130 transitions_t
*sp
= states
[state
]->shifts
;
132 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
134 int k
= temp_map
[TRANSITION_SYMBOL (sp
, i
)]++;
135 from_state
[k
] = state
;
136 to_state
[k
] = sp
->states
[i
];
140 XFREE (temp_map
+ ntokens
);
145 /*----------------------------------------------------------.
146 | Map a state/symbol pair into its numeric representation. |
147 `----------------------------------------------------------*/
150 map_goto (state_number_t state
, symbol_number_t symbol
)
157 low
= goto_map
[symbol
];
158 high
= goto_map
[symbol
+ 1] - 1;
162 middle
= (low
+ high
) / 2;
163 s
= from_state
[middle
];
181 short **reads
= XCALLOC (short *, ngotos
);
182 short *edge
= XCALLOC (short, ngotos
+ 1);
187 F
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
189 for (i
= 0; i
< ngotos
; i
++)
191 state_number_t stateno
= to_state
[i
];
192 transitions_t
*sp
= states
[stateno
]->shifts
;
195 for (j
= 0; j
< sp
->num
&& TRANSITION_IS_SHIFT (sp
, j
); j
++)
196 bitset_set (F
[i
], TRANSITION_SYMBOL (sp
, j
));
198 for (; j
< sp
->num
; j
++)
200 symbol_number_t symbol
= TRANSITION_SYMBOL (sp
, j
);
201 if (nullable
[symbol
])
202 edge
[nedges
++] = map_goto (stateno
, symbol
);
207 reads
[i
] = XCALLOC (short, nedges
+ 1);
208 memcpy (reads
[i
], edge
, nedges
* sizeof (edge
[0]));
209 reads
[i
][nedges
] = -1;
214 relation_digraph (reads
, ngotos
, &F
);
216 for (i
= 0; i
< ngotos
; i
++)
225 add_lookback_edge (state_t
*state
, rule_number_t ruleno
, int gotono
)
230 for (i
= 0; i
< state
->nlookaheads
; ++i
)
231 if (state
->lookaheads_rule
[i
]->number
== ruleno
)
234 assert (state
->lookaheads_rule
[i
]->number
== ruleno
);
236 sp
= XCALLOC (shorts
, 1);
237 sp
->next
= lookback
[(state
->lookaheads
- LA
) + i
];
239 lookback
[(state
->lookaheads
- LA
) + i
] = sp
;
245 build_relations (void)
247 short *edge
= XCALLOC (short, ngotos
+ 1);
248 state_number_t
*states1
= XCALLOC (state_number_t
, ritem_longest_rhs () + 1);
251 includes
= XCALLOC (short *, ngotos
);
253 for (i
= 0; i
< ngotos
; i
++)
256 symbol_number_t symbol1
= states
[to_state
[i
]]->accessing_symbol
;
257 rule_number_t
*rulep
;
259 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
264 state_t
*state
= states
[from_state
[i
]];
265 states1
[0] = state
->number
;
267 for (rp
= rules
[*rulep
].rhs
; *rp
>= 0; rp
++)
269 state
= transitions_to (state
->shifts
,
270 item_number_as_symbol_number (*rp
));
271 states1
[length
++] = state
->number
;
274 if (!state
->consistent
)
275 add_lookback_edge (state
, *rulep
, i
);
283 /* JF added rp>=ritem && I hope to god its right! */
284 if (rp
>= ritem
&& ISVAR (*rp
))
286 /* Downcasting from item_number_t to symbol_number_t. */
287 edge
[nedges
++] = map_goto (states1
[--length
],
288 item_number_as_symbol_number (*rp
));
298 includes
[i
] = XCALLOC (short, nedges
+ 1);
299 for (j
= 0; j
< nedges
; j
++)
300 includes
[i
][j
] = edge
[j
];
301 includes
[i
][nedges
] = -1;
308 relation_transpose (&includes
, ngotos
);
314 compute_FOLLOWS (void)
318 relation_digraph (includes
, ngotos
, &F
);
320 for (i
= 0; i
< ngotos
; i
++)
328 compute_lookaheads (void)
333 for (i
= 0; i
< nLA
; i
++)
334 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
335 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
338 for (i
= 0; i
< nLA
; i
++)
339 LIST_FREE (shorts
, lookback
[i
]);
346 /*-------------------------------------------------------------.
347 | Count the number of lookaheads required for each state |
348 | (NLOOKAHEADS member). Compute the total number of LA, NLA. |
349 `-------------------------------------------------------------*/
352 states_lookaheads_count (void)
358 for (i
= 0; i
< nstates
; i
++)
362 reductions_t
*rp
= states
[i
]->reductions
;
363 transitions_t
*sp
= states
[i
]->shifts
;
365 /* We need a lookahead either to distinguish different
366 reductions (i.e., there are two or more), or to distinguish a
367 reduction from a shift. Otherwise, it is straightforward,
368 and the state is `consistent'. */
370 || (rp
->num
== 1 && sp
->num
&& TRANSITION_IS_SHIFT (sp
, 0)))
371 nlookaheads
+= rp
->num
;
373 states
[i
]->consistent
= 1;
375 for (k
= 0; k
< sp
->num
; k
++)
376 if (TRANSITION_IS_ERROR (sp
, k
))
378 states
[i
]->consistent
= 0;
382 states
[i
]->nlookaheads
= nlookaheads
;
388 /*--------------------------------------.
389 | Initializing the lookaheads members. |
390 `--------------------------------------*/
393 states_lookaheads_initialize (void)
397 rule_t
**pLArule
= LArule
;
399 /* Initialize the members LOOKAHEADS and LOOKAHEADS_RULE for each
401 for (i
= 0; i
< nstates
; i
++)
403 states
[i
]->lookaheads
= pLA
;
404 states
[i
]->lookaheads_rule
= pLArule
;
405 pLA
+= states
[i
]->nlookaheads
;
406 pLArule
+= states
[i
]->nlookaheads
;
411 /*---------------------------------------.
412 | Output the lookaheads for each state. |
413 `---------------------------------------*/
416 lookaheads_print (FILE *out
)
420 fprintf (out
, "Lookaheads: BEGIN\n");
421 for (i
= 0; i
< nstates
; ++i
)
423 fprintf (out
, "State %d: %d lookaheads\n",
424 i
, states
[i
]->nlookaheads
);
426 for (j
= 0; j
< states
[i
]->nlookaheads
; ++j
)
427 BITSET_EXECUTE (states
[i
]->lookaheads
[j
], 0, k
,
429 fprintf (out
, " on %d (%s) -> rule %d\n",
431 states
[i
]->lookaheads_rule
[j
]->number
- 1);
434 fprintf (out
, "Lookaheads: END\n");
440 states_lookaheads_count ();
442 states_lookaheads_initialize ();
447 compute_lookaheads ();
450 lookaheads_print (stderr
);