]>
git.saurik.com Git - bison.git/blob - src/lalr.c
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. */
36 /* All the decorated states, indexed by the state number. */
37 state_t
**state_table
= NULL
;
49 /* And for the famous F variable, which name is so descriptive that a
50 comment is hardly needed. <grin>. */
51 static unsigned *F
= NULL
;
52 #define F(Rule) (F + (Rule) * tokensetsize)
54 static short **includes
;
55 static shorts
**lookback
;
58 /*---------------------------------------------------------------.
59 | digraph & traverse. |
61 | The following variables are used as common storage between the |
63 `---------------------------------------------------------------*/
67 static short *VERTICES
;
77 size_t size
= F (i
+ 1) - F(i
);
80 INDEX
[i
] = height
= top
;
83 for (j
= 0; R
[i
][j
] >= 0; ++j
)
85 if (INDEX
[R
[i
][j
]] == 0)
88 if (INDEX
[i
] > INDEX
[R
[i
][j
]])
89 INDEX
[i
] = INDEX
[R
[i
][j
]];
91 for (k
= 0; k
< size
; ++k
)
92 F (i
)[k
] |= F (R
[i
][j
])[k
];
95 if (INDEX
[i
] == height
)
104 for (k
= 0; k
< size
; ++k
)
111 digraph (short **relation
)
115 infinity
= ngotos
+ 2;
116 INDEX
= XCALLOC (short, ngotos
+ 1);
117 VERTICES
= XCALLOC (short, ngotos
+ 1);
122 for (i
= 0; i
< ngotos
; i
++)
125 for (i
= 0; i
< ngotos
; i
++)
126 if (INDEX
[i
] == 0 && R
[i
])
142 /* Avoid having to special case 0. */
146 LA
= XCALLOC (unsigned, nLA
* tokensetsize
);
147 LAruleno
= XCALLOC (short, nLA
);
148 lookback
= XCALLOC (shorts
*, nLA
);
151 for (i
= 0; i
< nstates
; i
++)
152 if (!state_table
[i
]->consistent
)
153 if ((rp
= state_table
[i
]->reductions
))
154 for (j
= 0; j
< rp
->nreds
; j
++)
155 *np
++ = rp
->rules
[j
];
169 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
170 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
173 for (state
= 0; state
< nstates
; ++state
)
175 shifts
*sp
= state_table
[state
]->shifts
;
176 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
178 symbol
= state_table
[sp
->shifts
[i
]]->accessing_symbol
;
180 if (ngotos
== MAXSHORT
)
181 fatal (_("too many gotos (max %d)"), MAXSHORT
);
189 for (i
= ntokens
; i
< nsyms
; i
++)
195 for (i
= ntokens
; i
< nsyms
; i
++)
196 goto_map
[i
] = temp_map
[i
];
198 goto_map
[nsyms
] = ngotos
;
199 temp_map
[nsyms
] = ngotos
;
201 from_state
= XCALLOC (short, ngotos
);
202 to_state
= XCALLOC (short, ngotos
);
204 for (state
= 0; state
< nstates
; ++state
)
206 shifts
*sp
= state_table
[state
]->shifts
;
207 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
209 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
211 state2
= sp
->shifts
[i
];
212 symbol
= state_table
[state2
]->accessing_symbol
;
214 k
= temp_map
[symbol
]++;
215 from_state
[k
] = state
;
216 to_state
[k
] = state2
;
221 XFREE (temp_map
+ ntokens
);
226 /*----------------------------------------------------------.
227 | Map a state/symbol pair into its numeric representation. |
228 `----------------------------------------------------------*/
231 map_goto (int state
, int symbol
)
238 low
= goto_map
[symbol
];
239 high
= goto_map
[symbol
+ 1] - 1;
243 middle
= (low
+ high
) / 2;
244 s
= from_state
[middle
];
262 short **reads
= XCALLOC (short *, ngotos
);
263 short *edge
= XCALLOC (short, ngotos
+ 1);
268 F
= XCALLOC (unsigned, ngotos
* tokensetsize
);
270 for (i
= 0; i
< ngotos
; i
++)
272 int stateno
= to_state
[i
];
273 shifts
*sp
= state_table
[stateno
]->shifts
;
276 for (j
= 0; j
< sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, j
); j
++)
278 int symbol
= state_table
[sp
->shifts
[j
]]->accessing_symbol
;
279 SETBIT (F (i
), symbol
);
282 for (; j
< sp
->nshifts
; j
++)
284 int symbol
= state_table
[sp
->shifts
[j
]]->accessing_symbol
;
285 if (nullable
[symbol
])
286 edge
[nedges
++] = map_goto (stateno
, symbol
);
291 reads
[i
] = XCALLOC (short, nedges
+ 1);
292 shortcpy (reads
[i
], edge
, nedges
);
293 reads
[i
][nedges
] = -1;
300 for (i
= 0; i
< ngotos
; i
++)
309 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
314 for (i
= 0; i
< state_table
[stateno
]->nlookaheads
; ++i
)
315 if (LAruleno
[state_table
[stateno
]->lookaheadsp
+ i
] == ruleno
)
318 assert (LAruleno
[state_table
[stateno
]->lookaheadsp
+ i
] == ruleno
);
320 sp
= XCALLOC (shorts
, 1);
321 sp
->next
= lookback
[state_table
[stateno
]->lookaheadsp
+ i
];
323 lookback
[state_table
[stateno
]->lookaheadsp
+ i
] = sp
;
328 matrix_print (FILE *out
, short **matrix
, int n
)
332 for (i
= 0; i
< n
; ++i
)
334 fprintf (out
, "%3d: ", i
);
336 for (j
= 0; matrix
[i
][j
] != -1; ++j
)
337 fprintf (out
, "%3d ", matrix
[i
][j
]);
343 /*-------------------------------------------------------------------.
344 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
345 | replaced with the result. |
347 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
349 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
351 `-------------------------------------------------------------------*/
354 transpose (short **R_arg
, int n
)
357 short **new_R
= XCALLOC (short *, n
);
358 /* END_R[I] -- next entry of NEW_R[I]. */
359 short **end_R
= XCALLOC (short *, n
);
360 /* NEDGES[I] -- total size of NEW_R[I]. */
361 short *nedges
= XCALLOC (short, n
);
366 fputs ("transpose: input\n", stderr
);
367 matrix_print (stderr
, R_arg
, n
);
371 for (i
= 0; i
< n
; i
++)
373 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
374 ++nedges
[R_arg
[i
][j
]];
377 for (i
= 0; i
< n
; i
++)
380 short *sp
= XCALLOC (short, nedges
[i
] + 1);
387 for (i
= 0; i
< n
; i
++)
389 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
391 *end_R
[R_arg
[i
][j
]] = i
;
392 ++end_R
[R_arg
[i
][j
]];
398 /* Free the input: it is replaced with the result. */
399 for (i
= 0; i
< n
; i
++)
405 fputs ("transpose: output\n", stderr
);
406 matrix_print (stderr
, new_R
, n
);
414 build_relations (void)
416 short *edge
= XCALLOC (short, ngotos
+ 1);
417 short *states
= XCALLOC (short, ritem_longest_rhs () + 1);
420 includes
= XCALLOC (short *, ngotos
);
422 for (i
= 0; i
< ngotos
; i
++)
425 int state1
= from_state
[i
];
426 int symbol1
= state_table
[to_state
[i
]]->accessing_symbol
;
429 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
433 int stateno
= state1
;
437 for (rp
= ritem
+ rule_table
[*rulep
].rhs
; *rp
> 0; rp
++)
439 shifts
*sp
= state_table
[stateno
]->shifts
;
441 for (j
= 0; j
< sp
->nshifts
; j
++)
443 stateno
= sp
->shifts
[j
];
444 if (state_table
[stateno
]->accessing_symbol
== *rp
)
448 states
[length
++] = stateno
;
451 if (!state_table
[stateno
]->consistent
)
452 add_lookback_edge (stateno
, *rulep
, i
);
460 /* JF added rp>=ritem && I hope to god its right! */
461 if (rp
>= ritem
&& ISVAR (*rp
))
463 stateno
= states
[--length
];
464 edge
[nedges
++] = map_goto (stateno
, *rp
);
474 includes
[i
] = XCALLOC (short, nedges
+ 1);
475 for (j
= 0; j
< nedges
; j
++)
476 includes
[i
][j
] = edge
[j
];
477 includes
[i
][nedges
] = -1;
484 includes
= transpose (includes
, ngotos
);
490 compute_FOLLOWS (void)
496 for (i
= 0; i
< ngotos
; i
++)
504 compute_lookaheads (void)
509 for (i
= 0; i
< nLA
; i
++)
510 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
512 int size
= LA (i
+ 1) - LA (i
);
514 for (j
= 0; j
< size
; ++j
)
515 LA (i
)[j
] |= F (sp
->value
)[j
];
519 for (i
= 0; i
< nLA
; i
++)
520 LIST_FREE (shorts
, lookback
[i
]);
527 /*--------------------------------------.
528 | Initializing the lookaheads members. |
529 `--------------------------------------*/
532 initialize_lookaheads (void)
536 for (i
= 0; i
< nstates
; i
++)
540 reductions
*rp
= state_table
[i
]->reductions
;
541 shifts
*sp
= state_table
[i
]->shifts
;
544 && (rp
->nreds
> 1 || (sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0))))
545 nlookaheads
+= rp
->nreds
;
547 state_table
[i
]->consistent
= 1;
549 for (k
= 0; k
< sp
->nshifts
; k
++)
550 if (SHIFT_IS_ERROR (sp
, k
))
552 state_table
[i
]->consistent
= 0;
556 state_table
[i
]->nlookaheads
= nlookaheads
;
557 state_table
[i
]->lookaheadsp
= nLA
;
565 tokensetsize
= WORDSIZE (ntokens
);
567 initialize_lookaheads ();
573 compute_lookaheads ();