]>
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. 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
])
143 size_t nLA
= state_table
[nstates
]->lookaheadsp
;
147 LA
= XCALLOC (unsigned, nLA
* tokensetsize
);
148 LAruleno
= XCALLOC (short, nLA
);
149 lookback
= XCALLOC (shorts
*, nLA
);
152 for (i
= 0; i
< nstates
; i
++)
153 if (!state_table
[i
]->consistent
)
154 if ((rp
= state_table
[i
]->reductions
))
155 for (j
= 0; j
< rp
->nreds
; j
++)
156 *np
++ = rp
->rules
[j
];
170 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
171 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
174 for (state
= 0; state
< nstates
; ++state
)
176 shifts
*sp
= state_table
[state
]->shifts
;
177 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
179 symbol
= state_table
[sp
->shifts
[i
]]->accessing_symbol
;
181 if (ngotos
== MAXSHORT
)
182 fatal (_("too many gotos (max %d)"), MAXSHORT
);
190 for (i
= ntokens
; i
< nsyms
; i
++)
196 for (i
= ntokens
; i
< nsyms
; i
++)
197 goto_map
[i
] = temp_map
[i
];
199 goto_map
[nsyms
] = ngotos
;
200 temp_map
[nsyms
] = ngotos
;
202 from_state
= XCALLOC (short, ngotos
);
203 to_state
= XCALLOC (short, ngotos
);
205 for (state
= 0; state
< nstates
; ++state
)
207 shifts
*sp
= state_table
[state
]->shifts
;
208 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
210 for (i
= sp
->nshifts
- 1; i
>= 0 && SHIFT_IS_GOTO (sp
, i
); --i
)
212 state2
= sp
->shifts
[i
];
213 symbol
= state_table
[state2
]->accessing_symbol
;
215 k
= temp_map
[symbol
]++;
216 from_state
[k
] = state
;
217 to_state
[k
] = state2
;
222 XFREE (temp_map
+ ntokens
);
227 /*----------------------------------------------------------.
228 | Map a state/symbol pair into its numeric representation. |
229 `----------------------------------------------------------*/
232 map_goto (int state
, int symbol
)
239 low
= goto_map
[symbol
];
240 high
= goto_map
[symbol
+ 1] - 1;
244 middle
= (low
+ high
) / 2;
245 s
= from_state
[middle
];
263 short **reads
= XCALLOC (short *, ngotos
);
264 short *edge
= XCALLOC (short, ngotos
+ 1);
269 F
= XCALLOC (unsigned, ngotos
* tokensetsize
);
271 for (i
= 0; i
< ngotos
; i
++)
273 int stateno
= to_state
[i
];
274 shifts
*sp
= state_table
[stateno
]->shifts
;
277 for (j
= 0; j
< sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, j
); j
++)
279 int symbol
= state_table
[sp
->shifts
[j
]]->accessing_symbol
;
280 SETBIT (F (i
), symbol
);
283 for (; j
< sp
->nshifts
; j
++)
285 int symbol
= state_table
[sp
->shifts
[j
]]->accessing_symbol
;
286 if (nullable
[symbol
])
287 edge
[nedges
++] = map_goto (stateno
, symbol
);
292 reads
[i
] = XCALLOC (short, nedges
+ 1);
293 shortcpy (reads
[i
], edge
, nedges
);
294 reads
[i
][nedges
] = -1;
301 for (i
= 0; i
< ngotos
; i
++)
310 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
317 i
= state_table
[stateno
]->lookaheadsp
;
318 k
= state_table
[stateno
+ 1]->lookaheadsp
;
320 while (!found
&& i
< k
)
322 if (LAruleno
[i
] == ruleno
)
330 sp
= XCALLOC (shorts
, 1);
331 sp
->next
= lookback
[i
];
338 matrix_print (FILE *out
, short **matrix
, int n
)
342 for (i
= 0; i
< n
; ++i
)
344 fprintf (out
, "%3d: ", i
);
346 for (j
= 0; matrix
[i
][j
] != -1; ++j
)
347 fprintf (out
, "%3d ", matrix
[i
][j
]);
353 /*-------------------------------------------------------------------.
354 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
355 | replaced with the result. |
357 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
359 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
361 `-------------------------------------------------------------------*/
364 transpose (short **R_arg
, int n
)
367 short **new_R
= XCALLOC (short *, n
);
368 /* END_R[I] -- next entry of NEW_R[I]. */
369 short **end_R
= XCALLOC (short *, n
);
370 /* NEDGES[I] -- total size of NEW_R[I]. */
371 short *nedges
= XCALLOC (short, n
);
376 fputs ("transpose: input\n", stderr
);
377 matrix_print (stderr
, R_arg
, n
);
381 for (i
= 0; i
< n
; i
++)
383 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
384 ++nedges
[R_arg
[i
][j
]];
387 for (i
= 0; i
< n
; i
++)
390 short *sp
= XCALLOC (short, nedges
[i
] + 1);
397 for (i
= 0; i
< n
; i
++)
399 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
401 *end_R
[R_arg
[i
][j
]] = i
;
402 ++end_R
[R_arg
[i
][j
]];
408 /* Free the input: it is replaced with the result. */
409 for (i
= 0; i
< n
; i
++)
415 fputs ("transpose: output\n", stderr
);
416 matrix_print (stderr
, new_R
, n
);
424 build_relations (void)
426 short *edge
= XCALLOC (short, ngotos
+ 1);
427 short *states
= XCALLOC (short, ritem_longest_rhs () + 1);
430 includes
= XCALLOC (short *, ngotos
);
432 for (i
= 0; i
< ngotos
; i
++)
435 int state1
= from_state
[i
];
436 int symbol1
= state_table
[to_state
[i
]]->accessing_symbol
;
439 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
443 int stateno
= state1
;
447 for (rp
= ritem
+ rule_table
[*rulep
].rhs
; *rp
> 0; rp
++)
449 shifts
*sp
= state_table
[stateno
]->shifts
;
451 for (j
= 0; j
< sp
->nshifts
; j
++)
453 stateno
= sp
->shifts
[j
];
454 if (state_table
[stateno
]->accessing_symbol
== *rp
)
458 states
[length
++] = stateno
;
461 if (!state_table
[stateno
]->consistent
)
462 add_lookback_edge (stateno
, *rulep
, i
);
470 /* JF added rp>=ritem && I hope to god its right! */
471 if (rp
>= ritem
&& ISVAR (*rp
))
473 stateno
= states
[--length
];
474 edge
[nedges
++] = map_goto (stateno
, *rp
);
484 includes
[i
] = XCALLOC (short, nedges
+ 1);
485 for (j
= 0; j
< nedges
; j
++)
486 includes
[i
][j
] = edge
[j
];
487 includes
[i
][nedges
] = -1;
494 includes
= transpose (includes
, ngotos
);
500 compute_FOLLOWS (void)
506 for (i
= 0; i
< ngotos
; i
++)
514 compute_lookaheads (void)
519 for (i
= 0; i
< state_table
[nstates
]->lookaheadsp
; i
++)
520 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
522 int size
= LA (i
+ 1) - LA (i
);
524 for (j
= 0; j
< size
; ++j
)
525 LA (i
)[j
] |= F (sp
->value
)[j
];
529 for (i
= 0; i
< state_table
[nstates
]->lookaheadsp
; i
++)
530 LIST_FREE (shorts
, lookback
[i
]);
537 /*--------------------------------------.
538 | Initializing the lookaheads members. |
539 `--------------------------------------*/
542 initialize_lookaheads (void)
546 for (i
= 0; i
< nstates
; i
++)
550 reductions
*rp
= state_table
[i
]->reductions
;
551 shifts
*sp
= state_table
[i
]->shifts
;
554 && (rp
->nreds
> 1 || (sp
->nshifts
&& SHIFT_IS_SHIFT (sp
, 0))))
555 nlookaheads
+= rp
->nreds
;
557 state_table
[i
]->consistent
= 1;
559 for (k
= 0; k
< sp
->nshifts
; k
++)
560 if (SHIFT_IS_ERROR (sp
, k
))
562 state_table
[i
]->consistent
= 0;
566 state_table
[i
]->nlookaheads
= nlookaheads
;
567 state_table
[i
]->lookaheadsp
= count
;
568 count
+= nlookaheads
;
571 /* Seems to be needed by conflicts.c. */
572 state_table
[nstates
] = STATE_ALLOC (0);
573 state_table
[nstates
]->lookaheadsp
= count
;
579 tokensetsize
= WORDSIZE (ntokens
);
581 initialize_lookaheads ();
587 compute_lookaheads ();