]>
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. */
44 goto_number
*goto_map
= NULL
;
45 static goto_number ngotos
= 0;
46 state_number
*from_state
= NULL
;
47 state_number
*to_state
= NULL
;
49 /* Linked list of goto numbers. */
50 typedef struct goto_list
52 struct goto_list
*next
;
57 /* LA is a LR by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
58 LArule[l] is applicable in the appropriate state when the next
59 token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
62 static bitsetv LA
= NULL
;
66 /* And for the famous F variable, which name is so descriptive that a
67 comment is hardly needed. <grin>. */
68 static bitsetv F
= NULL
;
70 static goto_number
**includes
;
71 static goto_list
**lookback
;
80 goto_number
*temp_map
;
82 goto_map
= XCALLOC (goto_number
, nvars
+ 1) - ntokens
;
83 temp_map
= XCALLOC (goto_number
, nvars
+ 1) - ntokens
;
86 for (s
= 0; s
< nstates
; ++s
)
88 transitions
*sp
= states
[s
]->transitions
;
90 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
92 if (ngotos
>= GOTO_NUMBER_MAXIMUM
)
95 goto_map
[TRANSITION_SYMBOL (sp
, i
)]++;
102 for (i
= ntokens
; i
< nsyms
; i
++)
108 for (i
= ntokens
; i
< nsyms
; i
++)
109 goto_map
[i
] = temp_map
[i
];
111 goto_map
[nsyms
] = ngotos
;
112 temp_map
[nsyms
] = ngotos
;
115 from_state
= XCALLOC (state_number
, ngotos
);
116 to_state
= XCALLOC (state_number
, ngotos
);
118 for (s
= 0; s
< nstates
; ++s
)
120 transitions
*sp
= states
[s
]->transitions
;
122 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
124 int k
= temp_map
[TRANSITION_SYMBOL (sp
, i
)]++;
126 to_state
[k
] = sp
->states
[i
]->number
;
130 XFREE (temp_map
+ ntokens
);
135 /*----------------------------------------------------------.
136 | Map a state/symbol pair into its numeric representation. |
137 `----------------------------------------------------------*/
140 map_goto (state_number s0
, symbol_number sym
)
148 high
= goto_map
[sym
+ 1] - 1;
154 middle
= (low
+ high
) / 2;
155 s
= from_state
[middle
];
169 goto_number
**reads
= XCALLOC (goto_number
*, ngotos
);
170 goto_number
*edge
= XCALLOC (goto_number
, ngotos
+ 1);
175 F
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
177 for (i
= 0; i
< ngotos
; i
++)
179 state_number stateno
= to_state
[i
];
180 transitions
*sp
= states
[stateno
]->transitions
;
183 FOR_EACH_SHIFT (sp
, j
)
184 bitset_set (F
[i
], TRANSITION_SYMBOL (sp
, j
));
186 for (; j
< sp
->num
; j
++)
188 symbol_number sym
= TRANSITION_SYMBOL (sp
, j
);
190 edge
[nedges
++] = map_goto (stateno
, sym
);
195 reads
[i
] = XCALLOC (goto_number
, nedges
+ 1);
196 memcpy (reads
[i
], edge
, nedges
* sizeof (edge
[0]));
197 reads
[i
][nedges
] = -1;
202 relation_digraph (reads
, ngotos
, &F
);
204 for (i
= 0; i
< ngotos
; i
++)
213 add_lookback_edge (state
*s
, rule
*r
, int gotono
)
215 int ri
= state_reduction_find (s
, r
);
216 goto_list
*sp
= XCALLOC (goto_list
, 1);
217 sp
->next
= lookback
[(s
->reductions
->lookaheads
- LA
) + ri
];
219 lookback
[(s
->reductions
->lookaheads
- LA
) + ri
] = sp
;
225 build_relations (void)
227 goto_number
*edge
= XCALLOC (goto_number
, ngotos
+ 1);
228 state_number
*states1
= XCALLOC (state_number
, ritem_longest_rhs () + 1);
231 includes
= XCALLOC (goto_number
*, ngotos
);
233 for (i
= 0; i
< ngotos
; i
++)
236 symbol_number symbol1
= states
[to_state
[i
]]->accessing_symbol
;
239 for (rulep
= derives
[symbol1
]; *rulep
; rulep
++)
244 state
*s
= states
[from_state
[i
]];
245 states1
[0] = s
->number
;
247 for (rp
= (*rulep
)->rhs
; *rp
>= 0; rp
++)
249 s
= transitions_to (s
->transitions
,
250 item_number_as_symbol_number (*rp
));
251 states1
[length
++] = s
->number
;
255 add_lookback_edge (s
, *rulep
, i
);
263 /* JF added rp>=ritem && I hope to god its right! */
264 if (rp
>= ritem
&& ISVAR (*rp
))
266 /* Downcasting from item_number to symbol_number. */
267 edge
[nedges
++] = map_goto (states1
[--length
],
268 item_number_as_symbol_number (*rp
));
278 includes
[i
] = XCALLOC (goto_number
, nedges
+ 1);
279 for (j
= 0; j
< nedges
; j
++)
280 includes
[i
][j
] = edge
[j
];
281 includes
[i
][nedges
] = -1;
288 relation_transpose (&includes
, ngotos
);
294 compute_FOLLOWS (void)
298 relation_digraph (includes
, ngotos
, &F
);
300 for (i
= 0; i
< ngotos
; i
++)
308 compute_lookaheads (void)
313 for (i
= 0; i
< nLA
; i
++)
314 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
315 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
318 for (i
= 0; i
< nLA
; i
++)
319 LIST_FREE (goto_list
, lookback
[i
]);
326 /*-----------------------------------------------------------.
327 | Count the number of lookaheads required for S (NLOOKAHEADS |
329 `-----------------------------------------------------------*/
332 state_lookaheads_count (state
*s
)
336 reductions
*rp
= s
->reductions
;
337 transitions
*sp
= s
->transitions
;
339 /* We need a lookahead either to distinguish different
340 reductions (i.e., there are two or more), or to distinguish a
341 reduction from a shift. Otherwise, it is straightforward,
342 and the state is `consistent'. */
344 || (rp
->num
== 1 && sp
->num
&&
345 !TRANSITION_IS_DISABLED (sp
, 0) && TRANSITION_IS_SHIFT (sp
, 0)))
346 nlookaheads
+= rp
->num
;
350 for (k
= 0; k
< sp
->num
; k
++)
351 if (!TRANSITION_IS_DISABLED (sp
, k
) && TRANSITION_IS_ERROR (sp
, k
))
361 /*----------------------------------------------.
362 | Compute LA, NLA, and the lookaheads members. |
363 `----------------------------------------------*/
371 /* Compute the total number of reductions requiring a lookahead. */
373 for (i
= 0; i
< nstates
; i
++)
374 nLA
+= state_lookaheads_count (states
[i
]);
375 /* Avoid having to special case 0. */
379 pLA
= LA
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
380 lookback
= XCALLOC (goto_list
*, nLA
);
382 /* Initialize the members LOOKAHEADS for each state which reductions
383 require lookaheads. */
384 for (i
= 0; i
< nstates
; i
++)
386 int count
= state_lookaheads_count (states
[i
]);
389 states
[i
]->reductions
->lookaheads
= pLA
;
396 /*---------------------------------------.
397 | Output the lookaheads for each state. |
398 `---------------------------------------*/
401 lookaheads_print (FILE *out
)
405 fprintf (out
, "Lookaheads: BEGIN\n");
406 for (i
= 0; i
< nstates
; ++i
)
408 reductions
*reds
= states
[i
]->reductions
;
409 bitset_iterator iter
;
412 if (reds
->lookaheads
)
413 for (k
= 0; k
< reds
->num
; ++k
)
414 if (reds
->lookaheads
[k
])
417 fprintf (out
, "State %d: %d lookaheads\n",
420 if (reds
->lookaheads
)
421 for (j
= 0; j
< reds
->num
; ++j
)
422 BITSET_FOR_EACH (iter
, reds
->lookaheads
[j
], k
, 0)
424 fprintf (out
, " on %d (%s) -> rule %d\n",
426 reds
->rules
[j
]->number
);
429 fprintf (out
, "Lookaheads: END\n");
440 compute_lookaheads ();
442 if (trace_flag
& trace_sets
)
443 lookaheads_print (stderr
);
451 for (s
= 0; s
< nstates
; ++s
)
452 states
[s
]->reductions
->lookaheads
= NULL
;