]>
git.saurik.com Git - bison.git/blob - src/lalr.c
9dda7f05647331a319e6d8ff7a9b24d7ac7d42a5
1 /* Compute look-ahead criteria for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* Compute how to make the finite state machine deterministic; find
23 which rules need lookahead in each state, and which lookahead
24 tokens they accept. */
39 /* All the decorated states, indexed by the state number. */
40 state_t
**states
= NULL
;
42 short *LAruleno
= NULL
;
47 short *goto_map
= NULL
;
48 short *from_state
= NULL
;
49 short *to_state
= NULL
;
51 /* And for the famous F variable, which name is so descriptive that a
52 comment is hardly needed. <grin>. */
53 static bitset
*F
= NULL
;
55 static short **includes
;
56 static shorts
**lookback
;
59 /*---------------------------------------------------------------.
60 | digraph & traverse. |
62 | The following variables are used as common storage between the |
64 `---------------------------------------------------------------*/
68 static short *VERTICES
;
79 INDEX
[i
] = height
= top
;
82 for (j
= 0; R
[i
][j
] >= 0; ++j
)
84 if (INDEX
[R
[i
][j
]] == 0)
87 if (INDEX
[i
] > INDEX
[R
[i
][j
]])
88 INDEX
[i
] = INDEX
[R
[i
][j
]];
90 bitset_or (F
[i
], F
[i
], F
[R
[i
][j
]]);
93 if (INDEX
[i
] == height
)
102 bitset_copy (F
[j
], F
[i
]);
108 digraph (short **relation
)
112 infinity
= ngotos
+ 2;
113 INDEX
= XCALLOC (short, ngotos
+ 1);
114 VERTICES
= XCALLOC (short, ngotos
+ 1);
119 for (i
= 0; i
< ngotos
; i
++)
122 for (i
= 0; i
< ngotos
; i
++)
123 if (INDEX
[i
] == 0 && R
[i
])
138 /* Avoid having to special case 0. */
142 LA
= XCALLOC (bitset
, nLA
);
143 for (i
= 0; i
< nLA
; ++i
)
144 LA
[i
] = bitset_create (ntokens
, BITSET_FIXED
);
145 LAruleno
= XCALLOC (short, nLA
);
146 lookback
= XCALLOC (shorts
*, nLA
);
149 for (i
= 0; i
< nstates
; i
++)
150 if (!states
[i
]->consistent
)
151 for (j
= 0; j
< states
[i
]->reductions
->nreds
; j
++)
152 *np
++ = states
[i
]->reductions
->rules
[j
];
163 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
164 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
167 for (state
= 0; state
< nstates
; ++state
)
169 shifts
*sp
= states
[state
]->shifts
;
170 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
172 if (ngotos
== MAXSHORT
)
173 fatal (_("too many gotos (max %d)"), MAXSHORT
);
176 goto_map
[SHIFT_SYMBOL (sp
, i
)]++;
182 for (i
= ntokens
; i
< nsyms
; i
++)
188 for (i
= ntokens
; i
< nsyms
; i
++)
189 goto_map
[i
] = temp_map
[i
];
191 goto_map
[nsyms
] = ngotos
;
192 temp_map
[nsyms
] = ngotos
;
195 from_state
= XCALLOC (short, ngotos
);
196 to_state
= XCALLOC (short, ngotos
);
198 for (state
= 0; state
< nstates
; ++state
)
200 shifts
*sp
= states
[state
]->shifts
;
201 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
203 int k
= temp_map
[SHIFT_SYMBOL (sp
, i
)]++;
204 from_state
[k
] = state
;
205 to_state
[k
] = sp
->shifts
[i
];
209 XFREE (temp_map
+ ntokens
);
214 /*----------------------------------------------------------.
215 | Map a state/symbol pair into its numeric representation. |
216 `----------------------------------------------------------*/
219 map_goto (int state
, int symbol
)
226 low
= goto_map
[symbol
];
227 high
= goto_map
[symbol
+ 1] - 1;
231 middle
= (low
+ high
) / 2;
232 s
= from_state
[middle
];
250 short **reads
= XCALLOC (short *, ngotos
);
251 short *edge
= XCALLOC (short, ngotos
+ 1);
256 F
= XCALLOC (bitset
, ngotos
);
257 for (i
= 0; i
< ngotos
; ++i
)
258 F
[i
] = bitset_create (ntokens
, BITSET_FIXED
);
260 for (i
= 0; i
< ngotos
; i
++)
262 int stateno
= to_state
[i
];
263 shifts
*sp
= states
[stateno
]->shifts
;
266 for (j
= 0; j
< sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, j
); j
++)
267 bitset_set (F
[i
], SHIFT_SYMBOL (sp
, j
));
269 for (; j
< sp
->nshifts
; j
++)
271 int symbol
= SHIFT_SYMBOL (sp
, j
);
272 if (nullable
[symbol
])
273 edge
[nedges
++] = map_goto (stateno
, symbol
);
278 reads
[i
] = XCALLOC (short, nedges
+ 1);
279 shortcpy (reads
[i
], edge
, nedges
);
280 reads
[i
][nedges
] = -1;
287 for (i
= 0; i
< ngotos
; i
++)
296 add_lookback_edge (state_t
*state
, int ruleno
, int gotono
)
301 for (i
= 0; i
< state
->nlookaheads
; ++i
)
302 if (LAruleno
[state
->lookaheadsp
+ i
] == ruleno
)
305 assert (LAruleno
[state
->lookaheadsp
+ i
] == ruleno
);
307 sp
= XCALLOC (shorts
, 1);
308 sp
->next
= lookback
[state
->lookaheadsp
+ i
];
310 lookback
[state
->lookaheadsp
+ i
] = sp
;
315 matrix_print (FILE *out
, short **matrix
, int n
)
319 for (i
= 0; i
< n
; ++i
)
321 fprintf (out
, "%3d: ", i
);
323 for (j
= 0; matrix
[i
][j
] != -1; ++j
)
324 fprintf (out
, "%3d ", matrix
[i
][j
]);
330 /*-------------------------------------------------------------------.
331 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
332 | replaced with the result. |
334 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
336 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
338 `-------------------------------------------------------------------*/
341 transpose (short **R_arg
, int n
)
344 short **new_R
= XCALLOC (short *, n
);
345 /* END_R[I] -- next entry of NEW_R[I]. */
346 short **end_R
= XCALLOC (short *, n
);
347 /* NEDGES[I] -- total size of NEW_R[I]. */
348 short *nedges
= XCALLOC (short, n
);
353 fputs ("transpose: input\n", stderr
);
354 matrix_print (stderr
, R_arg
, n
);
358 for (i
= 0; i
< n
; i
++)
360 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
361 ++nedges
[R_arg
[i
][j
]];
364 for (i
= 0; i
< n
; i
++)
367 short *sp
= XCALLOC (short, nedges
[i
] + 1);
374 for (i
= 0; i
< n
; i
++)
376 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
378 *end_R
[R_arg
[i
][j
]] = i
;
379 ++end_R
[R_arg
[i
][j
]];
385 /* Free the input: it is replaced with the result. */
386 for (i
= 0; i
< n
; i
++)
392 fputs ("transpose: output\n", stderr
);
393 matrix_print (stderr
, new_R
, n
);
401 build_relations (void)
403 short *edge
= XCALLOC (short, ngotos
+ 1);
404 short *states1
= XCALLOC (short, ritem_longest_rhs () + 1);
407 includes
= XCALLOC (short *, ngotos
);
409 for (i
= 0; i
< ngotos
; i
++)
412 int symbol1
= states
[to_state
[i
]]->accessing_symbol
;
415 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
420 state_t
*state
= states
[from_state
[i
]];
421 states1
[0] = state
->number
;
423 for (rp
= &ritem
[rules
[*rulep
].rhs
]; *rp
>= 0; rp
++)
425 shifts
*sp
= state
->shifts
;
427 for (j
= 0; j
< sp
->nshifts
; j
++)
429 state
= states
[sp
->shifts
[j
]];
430 if (state
->accessing_symbol
== *rp
)
434 states1
[length
++] = state
->number
;
437 if (!state
->consistent
)
438 add_lookback_edge (state
, *rulep
, i
);
446 /* JF added rp>=ritem && I hope to god its right! */
447 if (rp
>= ritem
&& ISVAR (*rp
))
449 edge
[nedges
++] = map_goto (states1
[--length
], *rp
);
459 includes
[i
] = XCALLOC (short, nedges
+ 1);
460 for (j
= 0; j
< nedges
; j
++)
461 includes
[i
][j
] = edge
[j
];
462 includes
[i
][nedges
] = -1;
469 includes
= transpose (includes
, ngotos
);
475 compute_FOLLOWS (void)
481 for (i
= 0; i
< ngotos
; i
++)
489 compute_lookaheads (void)
494 for (i
= 0; i
< nLA
; i
++)
495 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
496 bitset_or (LA
[i
], LA
[i
], F
[sp
->value
]);
499 for (i
= 0; i
< nLA
; i
++)
500 LIST_FREE (shorts
, lookback
[i
]);
503 for (i
= 0; i
< (unsigned) ngotos
; ++i
)
509 /*--------------------------------------.
510 | Initializing the lookaheads members. |
511 `--------------------------------------*/
514 initialize_lookaheads (void)
518 for (i
= 0; i
< nstates
; i
++)
522 reductions
*rp
= states
[i
]->reductions
;
523 shifts
*sp
= states
[i
]->shifts
;
525 /* We need a lookahead either to distinguish different
526 reductions (i.e., there are two or more), or to distinguish a
527 reduction from a shift. Otherwise, it is straightforward,
528 and the state is `consistent'. */
530 || (rp
->nreds
== 1 && sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0)))
531 nlookaheads
+= rp
->nreds
;
533 states
[i
]->consistent
= 1;
535 for (k
= 0; k
< sp
->nshifts
; k
++)
536 if (SHIFT_IS_ERROR (sp
, k
))
538 states
[i
]->consistent
= 0;
542 states
[i
]->nlookaheads
= nlookaheads
;
543 states
[i
]->lookaheadsp
= nLA
;
549 /*---------------------------------------.
550 | Output the lookaheads for each state. |
551 `---------------------------------------*/
554 lookaheads_print (FILE *out
)
558 fprintf (out
, "Lookaheads: BEGIN\n");
559 for (i
= 0; i
< nstates
; ++i
)
561 fprintf (out
, "State %d: %d lookaheads\n",
562 i
, states
[i
]->nlookaheads
);
564 for (j
= 0; j
< states
[i
]->nlookaheads
; ++j
)
565 for (k
= 0; k
< ntokens
; ++k
)
566 if (bitset_test (LA
[states
[i
]->lookaheadsp
+ j
], j
))
567 fprintf (out
, " on %d (%s) -> rule %d\n",
569 -LAruleno
[states
[i
]->lookaheadsp
+ j
] - 1);
571 fprintf (out
, "Lookaheads: END\n");
577 initialize_lookaheads ();
583 compute_lookaheads ();
586 lookaheads_print (stderr
);