]>
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 goto_number_t
*goto_map
= NULL
;
43 static goto_number_t ngotos
= 0;
44 state_number_t
*from_state
= NULL
;
45 state_number_t
*to_state
= NULL
;
47 /* Linked list of goto numbers. */
48 typedef struct goto_list_s
50 struct goto_list_s
*next
;
55 /* LA is a LR by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
56 LArule[l] is applicable in the appropriate state when the next
57 token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
60 static bitsetv LA
= NULL
;
64 /* And for the famous F variable, which name is so descriptive that a
65 comment is hardly needed. <grin>. */
66 static bitsetv F
= NULL
;
68 static goto_number_t
**includes
;
69 static goto_list_t
**lookback
;
78 goto_number_t
*temp_map
;
80 goto_map
= XCALLOC (goto_number_t
, nvars
+ 1) - ntokens
;
81 temp_map
= XCALLOC (goto_number_t
, nvars
+ 1) - ntokens
;
84 for (state
= 0; state
< nstates
; ++state
)
86 transitions_t
*sp
= states
[state
]->transitions
;
88 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
90 if (ngotos
>= GOTO_NUMBER_MAX
)
93 goto_map
[TRANSITION_SYMBOL (sp
, i
)]++;
100 for (i
= ntokens
; i
< nsyms
; i
++)
106 for (i
= ntokens
; i
< nsyms
; i
++)
107 goto_map
[i
] = temp_map
[i
];
109 goto_map
[nsyms
] = ngotos
;
110 temp_map
[nsyms
] = ngotos
;
113 from_state
= XCALLOC (state_number_t
, ngotos
);
114 to_state
= XCALLOC (state_number_t
, ngotos
);
116 for (state
= 0; state
< nstates
; ++state
)
118 transitions_t
*sp
= states
[state
]->transitions
;
120 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
122 int k
= temp_map
[TRANSITION_SYMBOL (sp
, i
)]++;
123 from_state
[k
] = state
;
124 to_state
[k
] = sp
->states
[i
]->number
;
128 XFREE (temp_map
+ ntokens
);
133 /*----------------------------------------------------------.
134 | Map a state/symbol pair into its numeric representation. |
135 `----------------------------------------------------------*/
138 map_goto (state_number_t state
, symbol_number_t symbol
)
145 low
= goto_map
[symbol
];
146 high
= goto_map
[symbol
+ 1] - 1;
152 middle
= (low
+ high
) / 2;
153 s
= from_state
[middle
];
167 goto_number_t
**reads
= XCALLOC (goto_number_t
*, ngotos
);
168 goto_number_t
*edge
= XCALLOC (goto_number_t
, ngotos
+ 1);
173 F
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
175 for (i
= 0; i
< ngotos
; i
++)
177 state_number_t stateno
= to_state
[i
];
178 transitions_t
*sp
= states
[stateno
]->transitions
;
181 FOR_EACH_SHIFT (sp
, j
)
182 bitset_set (F
[i
], TRANSITION_SYMBOL (sp
, j
));
184 for (; j
< sp
->num
; j
++)
186 symbol_number_t symbol
= TRANSITION_SYMBOL (sp
, j
);
187 if (nullable
[symbol
])
188 edge
[nedges
++] = map_goto (stateno
, symbol
);
193 reads
[i
] = XCALLOC (goto_number_t
, nedges
+ 1);
194 memcpy (reads
[i
], edge
, nedges
* sizeof (edge
[0]));
195 reads
[i
][nedges
] = -1;
200 relation_digraph (reads
, ngotos
, &F
);
202 for (i
= 0; i
< ngotos
; i
++)
211 add_lookback_edge (state_t
*state
, rule_t
*rule
, int gotono
)
213 int r
= state_reduction_find (state
, rule
);
214 goto_list_t
*sp
= XCALLOC (goto_list_t
, 1);
215 sp
->next
= lookback
[(state
->reductions
->lookaheads
- LA
) + r
];
217 lookback
[(state
->reductions
->lookaheads
- LA
) + r
] = sp
;
223 build_relations (void)
225 goto_number_t
*edge
= XCALLOC (goto_number_t
, ngotos
+ 1);
226 state_number_t
*states1
= XCALLOC (state_number_t
, ritem_longest_rhs () + 1);
229 includes
= XCALLOC (goto_number_t
*, ngotos
);
231 for (i
= 0; i
< ngotos
; i
++)
234 symbol_number_t symbol1
= states
[to_state
[i
]]->accessing_symbol
;
237 for (rulep
= derives
[symbol1
]; *rulep
; rulep
++)
242 state_t
*state
= states
[from_state
[i
]];
243 states1
[0] = state
->number
;
245 for (rp
= (*rulep
)->rhs
; *rp
>= 0; rp
++)
247 state
= transitions_to (state
->transitions
,
248 item_number_as_symbol_number (*rp
));
249 states1
[length
++] = state
->number
;
252 if (!state
->consistent
)
253 add_lookback_edge (state
, *rulep
, i
);
261 /* JF added rp>=ritem && I hope to god its right! */
262 if (rp
>= ritem
&& ISVAR (*rp
))
264 /* Downcasting from item_number_t to symbol_number_t. */
265 edge
[nedges
++] = map_goto (states1
[--length
],
266 item_number_as_symbol_number (*rp
));
276 includes
[i
] = XCALLOC (goto_number_t
, nedges
+ 1);
277 for (j
= 0; j
< nedges
; j
++)
278 includes
[i
][j
] = edge
[j
];
279 includes
[i
][nedges
] = -1;
286 relation_transpose (&includes
, ngotos
);
292 compute_FOLLOWS (void)
296 relation_digraph (includes
, ngotos
, &F
);
298 for (i
= 0; i
< ngotos
; i
++)
306 compute_lookaheads (void)
311 for (i
= 0; i
< nLA
; i
++)
312 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
313 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
316 for (i
= 0; i
< nLA
; i
++)
317 LIST_FREE (goto_list_t
, lookback
[i
]);
324 /*---------------------------------------------------------------.
325 | Count the number of lookaheads required for STATE (NLOOKAHEADS |
327 `---------------------------------------------------------------*/
330 state_lookaheads_count (state_t
*state
)
334 reductions_t
*rp
= state
->reductions
;
335 transitions_t
*sp
= state
->transitions
;
337 /* We need a lookahead either to distinguish different
338 reductions (i.e., there are two or more), or to distinguish a
339 reduction from a shift. Otherwise, it is straightforward,
340 and the state is `consistent'. */
342 || (rp
->num
== 1 && sp
->num
&&
343 !TRANSITION_IS_DISABLED (sp
, 0) && TRANSITION_IS_SHIFT (sp
, 0)))
344 nlookaheads
+= rp
->num
;
346 state
->consistent
= 1;
348 for (k
= 0; k
< sp
->num
; k
++)
349 if (!TRANSITION_IS_DISABLED (sp
, k
) && TRANSITION_IS_ERROR (sp
, k
))
351 state
->consistent
= 0;
359 /*----------------------------------------------.
360 | Compute LA, NLA, and the lookaheads members. |
361 `----------------------------------------------*/
369 /* Compute the total number of reductions requiring a lookahead. */
371 for (i
= 0; i
< nstates
; i
++)
372 nLA
+= state_lookaheads_count (states
[i
]);
373 /* Avoid having to special case 0. */
377 pLA
= LA
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
378 lookback
= XCALLOC (goto_list_t
*, nLA
);
380 /* Initialize the members LOOKAHEADS for each state which reductions
381 require lookaheads. */
382 for (i
= 0; i
< nstates
; i
++)
384 int count
= state_lookaheads_count (states
[i
]);
387 states
[i
]->reductions
->lookaheads
= pLA
;
394 /*---------------------------------------.
395 | Output the lookaheads for each state. |
396 `---------------------------------------*/
399 lookaheads_print (FILE *out
)
403 fprintf (out
, "Lookaheads: BEGIN\n");
404 for (i
= 0; i
< nstates
; ++i
)
406 reductions_t
*reds
= states
[i
]->reductions
;
407 bitset_iterator iter
;
410 if (reds
->lookaheads
)
411 for (k
= 0; k
< reds
->num
; ++k
)
412 if (reds
->lookaheads
[k
])
415 fprintf (out
, "State %d: %d lookaheads\n",
418 if (reds
->lookaheads
)
419 for (j
= 0; j
< reds
->num
; ++j
)
420 BITSET_FOR_EACH (iter
, reds
->lookaheads
[j
], k
, 0)
422 fprintf (out
, " on %d (%s) -> rule %d\n",
424 reds
->rules
[j
]->number
);
427 fprintf (out
, "Lookaheads: END\n");
438 compute_lookaheads ();
440 if (trace_flag
& trace_sets
)
441 lookaheads_print (stderr
);
449 for (s
= 0; s
< nstates
; ++s
)
450 states
[s
]->reductions
->lookaheads
= NULL
;