]>
git.saurik.com Git - bison.git/blob - src/lalr.c
1 /* Compute look-ahead criteria for Bison.
3 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002, 2003, 2004
4 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* Compute how to make the finite state machine deterministic; find
25 which rules need look-ahead in each state, and which look-ahead
26 tokens they accept. */
45 goto_number
*goto_map
;
46 static goto_number ngotos
;
47 state_number
*from_state
;
48 state_number
*to_state
;
50 /* Linked list of goto numbers. */
51 typedef struct goto_list
53 struct goto_list
*next
;
58 /* LA is a LR by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
59 LArule[l] is applicable in the appropriate state when the next
60 token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
63 static bitsetv LA
= NULL
;
67 /* And for the famous F variable, which name is so descriptive that a
68 comment is hardly needed. <grin>. */
69 static bitsetv F
= NULL
;
71 static goto_number
**includes
;
72 static goto_list
**lookback
;
81 goto_number
*temp_map
;
83 CALLOC (goto_map
, nvars
+ 1);
84 CALLOC (temp_map
, nvars
+ 1);
87 for (s
= 0; s
< nstates
; ++s
)
89 transitions
*sp
= states
[s
]->transitions
;
91 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
95 /* Abort if (ngotos + 1) would overflow. */
96 if (ngotos
== GOTO_NUMBER_MAXIMUM
)
99 goto_map
[TRANSITION_SYMBOL (sp
, i
) - ntokens
]++;
106 for (i
= ntokens
; i
< nsyms
; i
++)
108 temp_map
[i
- ntokens
] = k
;
109 k
+= goto_map
[i
- ntokens
];
112 for (i
= ntokens
; i
< nsyms
; i
++)
113 goto_map
[i
- ntokens
] = temp_map
[i
- ntokens
];
115 goto_map
[nsyms
- ntokens
] = ngotos
;
116 temp_map
[nsyms
- ntokens
] = ngotos
;
119 CALLOC (from_state
, ngotos
);
120 CALLOC (to_state
, ngotos
);
122 for (s
= 0; s
< nstates
; ++s
)
124 transitions
*sp
= states
[s
]->transitions
;
126 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
128 goto_number k
= temp_map
[TRANSITION_SYMBOL (sp
, i
) - ntokens
]++;
130 to_state
[k
] = sp
->states
[i
]->number
;
139 /*----------------------------------------------------------.
140 | Map a state/symbol pair into its numeric representation. |
141 `----------------------------------------------------------*/
144 map_goto (state_number s0
, symbol_number sym
)
151 low
= goto_map
[sym
- ntokens
];
152 high
= goto_map
[sym
- ntokens
+ 1] - 1;
158 middle
= (low
+ high
) / 2;
159 s
= from_state
[middle
];
173 goto_number
**reads
= CALLOC (reads
, ngotos
);
174 goto_number
*edge
= CALLOC (edge
, ngotos
+ 1);
175 goto_number nedges
= 0;
179 F
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
181 for (i
= 0; i
< ngotos
; i
++)
183 state_number stateno
= to_state
[i
];
184 transitions
*sp
= states
[stateno
]->transitions
;
187 FOR_EACH_SHIFT (sp
, j
)
188 bitset_set (F
[i
], TRANSITION_SYMBOL (sp
, j
));
190 for (; j
< sp
->num
; j
++)
192 symbol_number sym
= TRANSITION_SYMBOL (sp
, j
);
193 if (nullable
[sym
- ntokens
])
194 edge
[nedges
++] = map_goto (stateno
, sym
);
199 CALLOC (reads
[i
], nedges
+ 1);
200 memcpy (reads
[i
], edge
, nedges
* sizeof (edge
[0]));
201 reads
[i
][nedges
] = END_NODE
;
206 relation_digraph (reads
, ngotos
, &F
);
208 for (i
= 0; i
< ngotos
; i
++)
217 add_lookback_edge (state
*s
, rule
*r
, goto_number gotono
)
219 int ri
= state_reduction_find (s
, r
);
220 goto_list
*sp
= MALLOC (sp
, 1);
221 sp
->next
= lookback
[(s
->reductions
->look_ahead_tokens
- LA
) + ri
];
223 lookback
[(s
->reductions
->look_ahead_tokens
- LA
) + ri
] = sp
;
229 build_relations (void)
231 goto_number
*edge
= CALLOC (edge
, ngotos
+ 1);
232 state_number
*states1
= CALLOC (states1
, ritem_longest_rhs () + 1);
235 CALLOC (includes
, ngotos
);
237 for (i
= 0; i
< ngotos
; i
++)
240 symbol_number symbol1
= states
[to_state
[i
]]->accessing_symbol
;
243 for (rulep
= derives
[symbol1
- ntokens
]; *rulep
; rulep
++)
248 state
*s
= states
[from_state
[i
]];
249 states1
[0] = s
->number
;
251 for (rp
= (*rulep
)->rhs
; *rp
>= 0; rp
++)
253 s
= transitions_to (s
->transitions
,
254 item_number_as_symbol_number (*rp
));
255 states1
[length
++] = s
->number
;
259 add_lookback_edge (s
, *rulep
, i
);
267 /* JF added rp>=ritem && I hope to god its right! */
268 if (rp
>= ritem
&& ISVAR (*rp
))
270 /* Downcasting from item_number to symbol_number. */
271 edge
[nedges
++] = map_goto (states1
[--length
],
272 item_number_as_symbol_number (*rp
));
273 if (nullable
[*rp
- ntokens
])
282 CALLOC (includes
[i
], nedges
+ 1);
283 for (j
= 0; j
< nedges
; j
++)
284 includes
[i
][j
] = edge
[j
];
285 includes
[i
][nedges
] = END_NODE
;
292 relation_transpose (&includes
, ngotos
);
298 compute_FOLLOWS (void)
302 relation_digraph (includes
, ngotos
, &F
);
304 for (i
= 0; i
< ngotos
; i
++)
312 compute_look_ahead_tokens (void)
317 for (i
= 0; i
< nLA
; i
++)
318 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
319 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
322 for (i
= 0; i
< nLA
; i
++)
323 LIST_FREE (goto_list
, lookback
[i
]);
330 /*-----------------------------------------------------.
331 | Count the number of look-ahead tokens required for S |
332 | (N_LOOK_AHEAD_TOKENS member). |
333 `-----------------------------------------------------*/
336 state_look_ahead_tokens_count (state
*s
)
339 int n_look_ahead_tokens
= 0;
340 reductions
*rp
= s
->reductions
;
341 transitions
*sp
= s
->transitions
;
343 /* We need a look-ahead either to distinguish different
344 reductions (i.e., there are two or more), or to distinguish a
345 reduction from a shift. Otherwise, it is straightforward,
346 and the state is `consistent'. */
348 || (rp
->num
== 1 && sp
->num
&&
349 !TRANSITION_IS_DISABLED (sp
, 0) && TRANSITION_IS_SHIFT (sp
, 0)))
350 n_look_ahead_tokens
+= rp
->num
;
354 for (k
= 0; k
< sp
->num
; k
++)
355 if (!TRANSITION_IS_DISABLED (sp
, k
) && TRANSITION_IS_ERROR (sp
, k
))
361 return n_look_ahead_tokens
;
365 /*-----------------------------------------------------.
366 | Compute LA, NLA, and the look_ahead_tokens members. |
367 `-----------------------------------------------------*/
375 /* Compute the total number of reductions requiring a look-ahead. */
377 for (i
= 0; i
< nstates
; i
++)
378 nLA
+= state_look_ahead_tokens_count (states
[i
]);
379 /* Avoid having to special case 0. */
383 pLA
= LA
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
384 CALLOC (lookback
, nLA
);
386 /* Initialize the members LOOK_AHEAD_TOKENS for each state whose reductions
387 require look-ahead tokens. */
388 for (i
= 0; i
< nstates
; i
++)
390 int count
= state_look_ahead_tokens_count (states
[i
]);
393 states
[i
]->reductions
->look_ahead_tokens
= pLA
;
400 /*----------------------------------------------.
401 | Output the look-ahead tokens for each state. |
402 `----------------------------------------------*/
405 look_ahead_tokens_print (FILE *out
)
409 fprintf (out
, "Look-ahead tokens: BEGIN\n");
410 for (i
= 0; i
< nstates
; ++i
)
412 reductions
*reds
= states
[i
]->reductions
;
413 bitset_iterator iter
;
414 int n_look_ahead_tokens
= 0;
416 if (reds
->look_ahead_tokens
)
417 for (k
= 0; k
< reds
->num
; ++k
)
418 if (reds
->look_ahead_tokens
[k
])
419 ++n_look_ahead_tokens
;
421 fprintf (out
, "State %d: %d look-ahead tokens\n",
422 i
, n_look_ahead_tokens
);
424 if (reds
->look_ahead_tokens
)
425 for (j
= 0; j
< reds
->num
; ++j
)
426 BITSET_FOR_EACH (iter
, reds
->look_ahead_tokens
[j
], k
, 0)
428 fprintf (out
, " on %d (%s) -> rule %d\n",
430 reds
->rules
[j
]->number
);
433 fprintf (out
, "Look-ahead tokens: END\n");
444 compute_look_ahead_tokens ();
446 if (trace_flag
& trace_sets
)
447 look_ahead_tokens_print (stderr
);
455 for (s
= 0; s
< nstates
; ++s
)
456 states
[s
]->reductions
->look_ahead_tokens
= NULL
;