]>
git.saurik.com Git - bison.git/blob - src/lalr.c
6042eca8f8d7e658b4f505c74dc8d93072677a74
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. Warning:
37 there is a state_TABLE in LR0.c, but it is different and static.
39 state_t
**state_table
= NULL
;
50 /* And for the famous F variable, which name is so descriptive that a
51 comment is hardly needed. <grin>. */
52 static unsigned *F
= NULL
;
53 #define F(Rule) (F + (Rule) * tokensetsize)
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
;
78 size_t size
= F (i
+ 1) - F(i
);
81 INDEX
[i
] = height
= top
;
84 for (j
= 0; R
[i
][j
] >= 0; ++j
)
86 if (INDEX
[R
[i
][j
]] == 0)
89 if (INDEX
[i
] > INDEX
[R
[i
][j
]])
90 INDEX
[i
] = INDEX
[R
[i
][j
]];
92 for (k
= 0; k
< size
; ++k
)
93 F (i
)[k
] |= F (R
[i
][j
])[k
];
96 if (INDEX
[i
] == height
)
105 for (k
= 0; k
< size
; ++k
)
112 digraph (short **relation
)
116 infinity
= ngotos
+ 2;
117 INDEX
= XCALLOC (short, ngotos
+ 1);
118 VERTICES
= XCALLOC (short, ngotos
+ 1);
123 for (i
= 0; i
< ngotos
; i
++)
126 for (i
= 0; i
< ngotos
; i
++)
127 if (INDEX
[i
] == 0 && R
[i
])
135 /*--------------------.
136 | Build STATE_TABLE. |
137 `--------------------*/
140 set_state_table (void)
142 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
143 might be used (see conflicts.c). It is too opaque for me to
144 provide a probably less hacky implementation. --akim */
145 state_table
= XCALLOC (state_t
*, nstates
+ 1);
149 for (sp
= first_state
; sp
; sp
= sp
->next
)
150 state_table
[sp
->number
] = sp
;
153 /* Pessimization, but simplification of the code: make sure all the
154 states have a shifts, even if reduced to 0 shifts. */
157 for (i
= 0; i
< nstates
; i
++)
158 if (!state_table
[i
]->shifts
)
159 state_table
[i
]->shifts
= shifts_new (0);
162 /* Initializing the lookaheads members. Please note that it must be
163 performed after having set some of the other members which are
164 used below. Change with extreme caution. */
168 for (i
= 0; i
< nstates
; i
++)
171 reductions
*rp
= state_table
[i
]->reductions
;
172 shifts
*sp
= state_table
[i
]->shifts
;
174 state_table
[i
]->lookaheads
= count
;
177 && (rp
->nreds
> 1 || (sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0))))
180 state_table
[i
]->consistent
= 1;
182 for (k
= 0; k
< sp
->nshifts
; k
++)
183 if (SHIFT_IS_ERROR (sp
, k
))
185 state_table
[i
]->consistent
= 0;
190 /* Seems to be needed by conflicts.c. */
191 state_table
[nstates
] = STATE_ALLOC (0);
192 state_table
[nstates
]->lookaheads
= count
;
205 size_t nLA
= state_table
[nstates
]->lookaheads
;
209 LA
= XCALLOC (unsigned, nLA
* tokensetsize
);
210 LAruleno
= XCALLOC (short, nLA
);
211 lookback
= XCALLOC (shorts
*, nLA
);
214 for (i
= 0; i
< nstates
; i
++)
215 if (!state_table
[i
]->consistent
)
216 if ((rp
= state_table
[i
]->reductions
))
217 for (j
= 0; j
< rp
->nreds
; j
++)
218 *np
++ = rp
->rules
[j
];
232 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
233 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
236 for (state
= 0; state
< nstates
; ++state
)
238 shifts
*sp
= state_table
[state
]->shifts
;
239 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
241 symbol
= state_table
[sp
->shifts
[i
]]->accessing_symbol
;
243 if (ngotos
== MAXSHORT
)
244 fatal (_("too many gotos (max %d)"), MAXSHORT
);
252 for (i
= ntokens
; i
< nsyms
; i
++)
258 for (i
= ntokens
; i
< nsyms
; i
++)
259 goto_map
[i
] = temp_map
[i
];
261 goto_map
[nsyms
] = ngotos
;
262 temp_map
[nsyms
] = ngotos
;
264 from_state
= XCALLOC (short, ngotos
);
265 to_state
= XCALLOC (short, ngotos
);
267 for (state
= 0; state
< nstates
; ++state
)
269 shifts
*sp
= state_table
[state
]->shifts
;
270 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
272 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
274 state2
= sp
->shifts
[i
];
275 symbol
= state_table
[state2
]->accessing_symbol
;
277 k
= temp_map
[symbol
]++;
278 from_state
[k
] = state
;
279 to_state
[k
] = state2
;
284 XFREE (temp_map
+ ntokens
);
289 /*----------------------------------------------------------.
290 | Map a state/symbol pair into its numeric representation. |
291 `----------------------------------------------------------*/
294 map_goto (int state
, int symbol
)
301 low
= goto_map
[symbol
];
302 high
= goto_map
[symbol
+ 1] - 1;
306 middle
= (low
+ high
) / 2;
307 s
= from_state
[middle
];
325 short **reads
= XCALLOC (short *, ngotos
);
326 short *edge
= XCALLOC (short, ngotos
+ 1);
331 F
= XCALLOC (unsigned, ngotos
* tokensetsize
);
333 for (i
= 0; i
< ngotos
; i
++)
335 int stateno
= to_state
[i
];
336 shifts
*sp
= state_table
[stateno
]->shifts
;
339 for (j
= 0; j
< sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, j
); j
++)
341 int symbol
= state_table
[sp
->shifts
[j
]]->accessing_symbol
;
342 SETBIT (F (i
), symbol
);
345 for (; j
< sp
->nshifts
; j
++)
347 int symbol
= state_table
[sp
->shifts
[j
]]->accessing_symbol
;
348 if (nullable
[symbol
])
349 edge
[nedges
++] = map_goto (stateno
, symbol
);
354 reads
[i
] = XCALLOC (short, nedges
+ 1);
355 shortcpy (reads
[i
], edge
, nedges
);
356 reads
[i
][nedges
] = -1;
363 for (i
= 0; i
< ngotos
; i
++)
372 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
379 i
= state_table
[stateno
]->lookaheads
;
380 k
= state_table
[stateno
+ 1]->lookaheads
;
382 while (!found
&& i
< k
)
384 if (LAruleno
[i
] == ruleno
)
392 sp
= XCALLOC (shorts
, 1);
393 sp
->next
= lookback
[i
];
400 matrix_print (FILE *out
, short **matrix
, int n
)
404 for (i
= 0; i
< n
; ++i
)
406 fprintf (out
, "%3d: ", i
);
408 for (j
= 0; matrix
[i
][j
] != -1; ++j
)
409 fprintf (out
, "%3d ", matrix
[i
][j
]);
415 /*-------------------------------------------------------------------.
416 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
417 | replaced with the result. |
419 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
421 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
423 `-------------------------------------------------------------------*/
426 transpose (short **R_arg
, int n
)
429 short **new_R
= XCALLOC (short *, n
);
430 /* END_R[I] -- next entry of NEW_R[I]. */
431 short **end_R
= XCALLOC (short *, n
);
432 /* NEDGES[I] -- total size of NEW_R[I]. */
433 short *nedges
= XCALLOC (short, n
);
438 fputs ("transpose: input\n", stderr
);
439 matrix_print (stderr
, R_arg
, n
);
443 for (i
= 0; i
< n
; i
++)
445 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
446 ++nedges
[R_arg
[i
][j
]];
449 for (i
= 0; i
< n
; i
++)
452 short *sp
= XCALLOC (short, nedges
[i
] + 1);
459 for (i
= 0; i
< n
; i
++)
461 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
463 *end_R
[R_arg
[i
][j
]] = i
;
464 ++end_R
[R_arg
[i
][j
]];
470 /* Free the input: it is replaced with the result. */
471 for (i
= 0; i
< n
; i
++)
477 fputs ("transpose: output\n", stderr
);
478 matrix_print (stderr
, new_R
, n
);
486 build_relations (void)
488 short *edge
= XCALLOC (short, ngotos
+ 1);
489 short *states
= XCALLOC (short, ritem_longest_rhs () + 1);
492 includes
= XCALLOC (short *, ngotos
);
494 for (i
= 0; i
< ngotos
; i
++)
497 int state1
= from_state
[i
];
498 int symbol1
= state_table
[to_state
[i
]]->accessing_symbol
;
501 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
505 int stateno
= state1
;
509 for (rp
= ritem
+ rule_table
[*rulep
].rhs
; *rp
> 0; rp
++)
511 shifts
*sp
= state_table
[stateno
]->shifts
;
513 for (j
= 0; j
< sp
->nshifts
; j
++)
515 stateno
= sp
->shifts
[j
];
516 if (state_table
[stateno
]->accessing_symbol
== *rp
)
520 states
[length
++] = stateno
;
523 if (!state_table
[stateno
]->consistent
)
524 add_lookback_edge (stateno
, *rulep
, i
);
532 /* JF added rp>=ritem && I hope to god its right! */
533 if (rp
>= ritem
&& ISVAR (*rp
))
535 stateno
= states
[--length
];
536 edge
[nedges
++] = map_goto (stateno
, *rp
);
546 includes
[i
] = XCALLOC (short, nedges
+ 1);
547 for (j
= 0; j
< nedges
; j
++)
548 includes
[i
][j
] = edge
[j
];
549 includes
[i
][nedges
] = -1;
556 includes
= transpose (includes
, ngotos
);
562 compute_FOLLOWS (void)
568 for (i
= 0; i
< ngotos
; i
++)
576 compute_lookaheads (void)
581 for (i
= 0; i
< state_table
[nstates
]->lookaheads
; i
++)
582 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
584 int size
= LA (i
+ 1) - LA (i
);
586 for (j
= 0; j
< size
; ++j
)
587 LA (i
)[j
] |= F (sp
->value
)[j
];
591 for (i
= 0; i
< state_table
[nstates
]->lookaheads
; i
++)
592 LIST_FREE (shorts
, lookback
[i
]);
602 tokensetsize
= WORDSIZE (ntokens
);
610 compute_lookaheads ();