]>
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 /* LARULE is a vector which records the rules that need lookahead in
56 various states. The elements of LARULE that apply to state S are
57 those from LOOKAHEADS[S] through LOOKAHEADS[S+1]-1.
59 If LR is the length of LArule, then a number from 0 to LR-1 can
60 specify both a rule and a state where the rule might be applied.
63 static rule_t
**LArule
= NULL
;
65 /* LA is a LR by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
66 LArule[l] is applicable in the appropriate state when the next
67 token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
70 static bitsetv LA
= NULL
;
74 /* And for the famous F variable, which name is so descriptive that a
75 comment is hardly needed. <grin>. */
76 static bitsetv F
= NULL
;
78 static goto_number_t
**includes
;
79 static goto_list_t
**lookback
;
91 /* Avoid having to special case 0. */
95 LA
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
96 LArule
= XCALLOC (rule_t
*, nLA
);
97 lookback
= XCALLOC (goto_list_t
*, nLA
);
100 for (i
= 0; i
< nstates
; i
++)
101 if (!states
[i
]->consistent
)
102 for (j
= 0; j
< states
[i
]->reductions
->num
; j
++)
103 *np
++ = states
[i
]->reductions
->rules
[j
];
110 state_number_t state
;
111 goto_number_t
*temp_map
;
113 goto_map
= XCALLOC (goto_number_t
, nvars
+ 1) - ntokens
;
114 temp_map
= XCALLOC (goto_number_t
, nvars
+ 1) - ntokens
;
117 for (state
= 0; state
< nstates
; ++state
)
119 transitions_t
*sp
= states
[state
]->transitions
;
121 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
123 if (ngotos
== GOTO_NUMBER_MAX
)
124 fatal (_("too many gotos (max %d)"), GOTO_NUMBER_MAX
);
127 goto_map
[TRANSITION_SYMBOL (sp
, i
)]++;
134 for (i
= ntokens
; i
< nsyms
; i
++)
140 for (i
= ntokens
; i
< nsyms
; i
++)
141 goto_map
[i
] = temp_map
[i
];
143 goto_map
[nsyms
] = ngotos
;
144 temp_map
[nsyms
] = ngotos
;
147 from_state
= XCALLOC (state_number_t
, ngotos
);
148 to_state
= XCALLOC (state_number_t
, ngotos
);
150 for (state
= 0; state
< nstates
; ++state
)
152 transitions_t
*sp
= states
[state
]->transitions
;
154 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
156 int k
= temp_map
[TRANSITION_SYMBOL (sp
, i
)]++;
157 from_state
[k
] = state
;
158 to_state
[k
] = sp
->states
[i
]->number
;
162 XFREE (temp_map
+ ntokens
);
167 /*----------------------------------------------------------.
168 | Map a state/symbol pair into its numeric representation. |
169 `----------------------------------------------------------*/
172 map_goto (state_number_t state
, symbol_number_t symbol
)
179 low
= goto_map
[symbol
];
180 high
= goto_map
[symbol
+ 1] - 1;
184 middle
= (low
+ high
) / 2;
185 s
= from_state
[middle
];
203 goto_number_t
**reads
= XCALLOC (goto_number_t
*, ngotos
);
204 goto_number_t
*edge
= XCALLOC (goto_number_t
, ngotos
+ 1);
209 F
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
211 for (i
= 0; i
< ngotos
; i
++)
213 state_number_t stateno
= to_state
[i
];
214 transitions_t
*sp
= states
[stateno
]->transitions
;
217 FOR_EACH_SHIFT (sp
, j
)
218 bitset_set (F
[i
], TRANSITION_SYMBOL (sp
, j
));
220 for (; j
< sp
->num
; j
++)
222 symbol_number_t symbol
= TRANSITION_SYMBOL (sp
, j
);
223 if (nullable
[symbol
])
224 edge
[nedges
++] = map_goto (stateno
, symbol
);
229 reads
[i
] = XCALLOC (goto_number_t
, nedges
+ 1);
230 memcpy (reads
[i
], edge
, nedges
* sizeof (edge
[0]));
231 reads
[i
][nedges
] = -1;
236 relation_digraph (reads
, ngotos
, &F
);
238 for (i
= 0; i
< ngotos
; i
++)
247 add_lookback_edge (state_t
*state
, rule_t
*rule
, int gotono
)
252 for (i
= 0; i
< state
->nlookaheads
; ++i
)
253 if (state
->lookaheads_rule
[i
] == rule
)
256 assert (state
->lookaheads_rule
[i
] == rule
);
258 sp
= XCALLOC (goto_list_t
, 1);
259 sp
->next
= lookback
[(state
->lookaheads
- LA
) + i
];
261 lookback
[(state
->lookaheads
- LA
) + i
] = sp
;
267 build_relations (void)
269 goto_number_t
*edge
= XCALLOC (goto_number_t
, ngotos
+ 1);
270 state_number_t
*states1
= XCALLOC (state_number_t
, ritem_longest_rhs () + 1);
273 includes
= XCALLOC (goto_number_t
*, ngotos
);
275 for (i
= 0; i
< ngotos
; i
++)
278 symbol_number_t symbol1
= states
[to_state
[i
]]->accessing_symbol
;
281 for (rulep
= derives
[symbol1
]; *rulep
; rulep
++)
286 state_t
*state
= states
[from_state
[i
]];
287 states1
[0] = state
->number
;
289 for (rp
= (*rulep
)->rhs
; *rp
>= 0; rp
++)
291 state
= transitions_to (state
->transitions
,
292 item_number_as_symbol_number (*rp
));
293 states1
[length
++] = state
->number
;
296 if (!state
->consistent
)
297 add_lookback_edge (state
, *rulep
, i
);
305 /* JF added rp>=ritem && I hope to god its right! */
306 if (rp
>= ritem
&& ISVAR (*rp
))
308 /* Downcasting from item_number_t to symbol_number_t. */
309 edge
[nedges
++] = map_goto (states1
[--length
],
310 item_number_as_symbol_number (*rp
));
320 includes
[i
] = XCALLOC (goto_number_t
, nedges
+ 1);
321 for (j
= 0; j
< nedges
; j
++)
322 includes
[i
][j
] = edge
[j
];
323 includes
[i
][nedges
] = -1;
330 relation_transpose (&includes
, ngotos
);
336 compute_FOLLOWS (void)
340 relation_digraph (includes
, ngotos
, &F
);
342 for (i
= 0; i
< ngotos
; i
++)
350 compute_lookaheads (void)
355 for (i
= 0; i
< nLA
; i
++)
356 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
357 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
360 for (i
= 0; i
< nLA
; i
++)
361 LIST_FREE (goto_list_t
, lookback
[i
]);
368 /*-------------------------------------------------------------.
369 | Count the number of lookaheads required for each state |
370 | (NLOOKAHEADS member). Compute the total number of LA, NLA. |
371 `-------------------------------------------------------------*/
374 states_lookaheads_count (void)
380 for (i
= 0; i
< nstates
; i
++)
384 reductions_t
*rp
= states
[i
]->reductions
;
385 transitions_t
*sp
= states
[i
]->transitions
;
387 /* We need a lookahead either to distinguish different
388 reductions (i.e., there are two or more), or to distinguish a
389 reduction from a shift. Otherwise, it is straightforward,
390 and the state is `consistent'. */
392 || (rp
->num
== 1 && sp
->num
&&
393 !TRANSITION_IS_DISABLED (sp
, 0) && TRANSITION_IS_SHIFT (sp
, 0)))
394 nlookaheads
+= rp
->num
;
396 states
[i
]->consistent
= 1;
398 for (k
= 0; k
< sp
->num
; k
++)
399 if (!TRANSITION_IS_DISABLED (sp
, k
) && TRANSITION_IS_ERROR (sp
, k
))
401 states
[i
]->consistent
= 0;
405 states
[i
]->nlookaheads
= nlookaheads
;
411 /*--------------------------------------.
412 | Initializing the lookaheads members. |
413 `--------------------------------------*/
416 states_lookaheads_initialize (void)
420 rule_t
**pLArule
= LArule
;
422 /* Initialize the members LOOKAHEADS and LOOKAHEADS_RULE for each
424 for (i
= 0; i
< nstates
; i
++)
426 states
[i
]->lookaheads
= pLA
;
427 states
[i
]->lookaheads_rule
= pLArule
;
428 pLA
+= states
[i
]->nlookaheads
;
429 pLArule
+= states
[i
]->nlookaheads
;
434 /*---------------------------------------.
435 | Output the lookaheads for each state. |
436 `---------------------------------------*/
439 lookaheads_print (FILE *out
)
443 fprintf (out
, "Lookaheads: BEGIN\n");
444 for (i
= 0; i
< nstates
; ++i
)
446 bitset_iterator iter
;
448 fprintf (out
, "State %d: %d lookaheads\n",
449 i
, states
[i
]->nlookaheads
);
451 for (j
= 0; j
< states
[i
]->nlookaheads
; ++j
)
452 BITSET_FOR_EACH (iter
, states
[i
]->lookaheads
[j
], k
, 0)
454 fprintf (out
, " on %d (%s) -> rule %d\n",
456 states
[i
]->lookaheads_rule
[j
]->number
);
459 fprintf (out
, "Lookaheads: END\n");
465 states_lookaheads_count ();
467 states_lookaheads_initialize ();
472 compute_lookaheads ();
474 if (trace_flag
& trace_sets
)
475 lookaheads_print (stderr
);
483 for (s
= 0; s
< nstates
; ++s
)
485 states
[s
]->lookaheads
= NULL
;
486 states
[s
]->lookaheads_rule
= NULL
;