]>
git.saurik.com Git - bison.git/blob - src/lalr.c
eb226f4483321439f1ac816df6bd4b6914f85432
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
;
49 extern void berror
PARAMS ((const char *));
54 /* And for the famous F variable, which name is so descriptive that a
55 comment is hardly needed. <grin>. */
56 static unsigned *F
= NULL
;
57 #define F(Rule) (F + (Rule) * tokensetsize)
59 static short **includes
;
60 static shorts
**lookback
;
63 static short *VERTICES
;
73 size_t size
= F (i
+ 1) - F(i
);
76 INDEX
[i
] = height
= top
;
79 for (j
= 0; R
[i
][j
] >= 0; ++j
)
81 if (INDEX
[R
[i
][j
]] == 0)
84 if (INDEX
[i
] > INDEX
[R
[i
][j
]])
85 INDEX
[i
] = INDEX
[R
[i
][j
]];
87 for (k
= 0; k
< size
; ++k
)
88 F (i
)[k
] |= F (R
[i
][j
])[k
];
91 if (INDEX
[i
] == height
)
100 for (k
= 0; k
< size
; ++k
)
107 digraph (short **relation
)
111 infinity
= ngotos
+ 2;
112 INDEX
= XCALLOC (short, ngotos
+ 1);
113 VERTICES
= XCALLOC (short, ngotos
+ 1);
118 for (i
= 0; i
< ngotos
; i
++)
121 for (i
= 0; i
< ngotos
; i
++)
122 if (INDEX
[i
] == 0 && R
[i
])
130 /*--------------------.
131 | Build STATE_TABLE. |
132 `--------------------*/
135 set_state_table (void)
137 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
138 might be used (see conflicts.c). It is too opaque for me to
139 provide a probably less hacky implementation. --akim */
140 state_table
= XCALLOC (state_t
, nstates
+ 1);
144 for (sp
= first_state
; sp
; sp
= sp
->next
)
146 state_table
[sp
->number
].state
= sp
;
147 state_table
[sp
->number
].accessing_symbol
= sp
->accessing_symbol
;
153 for (sp
= first_shift
; sp
; sp
= sp
->next
)
154 state_table
[sp
->number
].shift_table
= sp
;
159 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
160 state_table
[rp
->number
].reduction_table
= rp
;
163 /* Initializing the lookaheads members. Please note that it must be
164 performed after having set some of the other members which are
165 used below. Change with extreme caution. */
169 for (i
= 0; i
< nstates
; i
++)
172 reductions
*rp
= state_table
[i
].reduction_table
;
173 shifts
*sp
= state_table
[i
].shift_table
;
175 state_table
[i
].lookaheads
= count
;
179 || (sp
&& !ISVAR (state_table
[sp
->shifts
[0]].accessing_symbol
))))
182 state_table
[i
].consistent
= 1;
185 for (k
= 0; k
< sp
->nshifts
; k
++)
186 if (state_table
[sp
->shifts
[k
]].accessing_symbol
187 == error_token_number
)
189 state_table
[i
].consistent
= 0;
193 state_table
[nstates
].lookaheads
= count
;
206 size_t nLA
= state_table
[nstates
].lookaheads
;
210 LA
= XCALLOC (unsigned, nLA
* tokensetsize
);
211 LAruleno
= XCALLOC (short, nLA
);
212 lookback
= XCALLOC (shorts
*, nLA
);
215 for (i
= 0; i
< nstates
; i
++)
216 if (!state_table
[i
].consistent
)
217 if ((rp
= state_table
[i
].reduction_table
))
218 for (j
= 0; j
< rp
->nreds
; j
++)
219 *np
++ = rp
->rules
[j
];
234 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
235 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
238 for (sp
= first_shift
; sp
; sp
= sp
->next
)
240 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
242 symbol
= state_table
[sp
->shifts
[i
]].accessing_symbol
;
244 if (ISTOKEN (symbol
))
247 if (ngotos
== MAXSHORT
)
248 fatal (_("too many gotos (max %d)"), MAXSHORT
);
256 for (i
= ntokens
; i
< nsyms
; i
++)
262 for (i
= ntokens
; i
< nsyms
; i
++)
263 goto_map
[i
] = temp_map
[i
];
265 goto_map
[nsyms
] = ngotos
;
266 temp_map
[nsyms
] = ngotos
;
268 from_state
= XCALLOC (short, ngotos
);
269 to_state
= XCALLOC (short, ngotos
);
271 for (sp
= first_shift
; sp
; sp
= sp
->next
)
274 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
276 state2
= sp
->shifts
[i
];
277 symbol
= state_table
[state2
].accessing_symbol
;
279 if (ISTOKEN (symbol
))
282 k
= temp_map
[symbol
]++;
283 from_state
[k
] = state1
;
284 to_state
[k
] = state2
;
288 XFREE (temp_map
+ ntokens
);
293 /*----------------------------------------------------------.
294 | Map a state/symbol pair into its numeric representation. |
295 `----------------------------------------------------------*/
298 map_goto (int state
, int symbol
)
305 low
= goto_map
[symbol
];
306 high
= goto_map
[symbol
+ 1] - 1;
310 middle
= (low
+ high
) / 2;
311 s
= from_state
[middle
];
329 short **reads
= XCALLOC (short *, ngotos
);
330 short *edge
= XCALLOC (short, ngotos
+ 1);
335 F
= XCALLOC (unsigned, ngotos
* tokensetsize
);
337 for (i
= 0; i
< ngotos
; i
++)
339 int stateno
= to_state
[i
];
340 shifts
*sp
= state_table
[stateno
].shift_table
;
345 for (j
= 0; j
< sp
->nshifts
; j
++)
347 int symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
350 SETBIT (F
+ i
* tokensetsize
, symbol
);
353 for (; j
< sp
->nshifts
; j
++)
355 int symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
356 if (nullable
[symbol
])
357 edge
[nedges
++] = map_goto (stateno
, symbol
);
362 reads
[i
] = XCALLOC (short, nedges
+ 1);
363 shortcpy (reads
[i
], edge
, nedges
);
364 reads
[i
][nedges
] = -1;
372 for (i
= 0; i
< ngotos
; i
++)
381 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
388 i
= state_table
[stateno
].lookaheads
;
389 k
= state_table
[stateno
+ 1].lookaheads
;
391 while (!found
&& i
< k
)
393 if (LAruleno
[i
] == ruleno
)
401 sp
= XCALLOC (shorts
, 1);
402 sp
->next
= lookback
[i
];
409 matrix_print (FILE *out
, short **matrix
, int n
)
413 for (i
= 0; i
< n
; ++i
)
415 fprintf (out
, "%3d: ", i
);
417 for (j
= 0; matrix
[i
][j
] != -1; ++j
)
418 fprintf (out
, "%3d ", matrix
[i
][j
]);
424 /*-------------------------------------------------------------------.
425 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
426 | replaced with the result. |
428 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
430 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
432 `-------------------------------------------------------------------*/
435 transpose (short **R_arg
, int n
)
438 short **new_R
= XCALLOC (short *, n
);
439 /* END_R[I] -- next entry of NEW_R[I]. */
440 short **end_R
= XCALLOC (short *, n
);
441 /* NEDGES[I] -- total size of NEW_R[I]. */
442 short *nedges
= XCALLOC (short, n
);
447 fputs ("transpose: input\n", stderr
);
448 matrix_print (stderr
, R_arg
, n
);
452 for (i
= 0; i
< n
; i
++)
454 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
455 ++nedges
[R_arg
[i
][j
]];
458 for (i
= 0; i
< n
; i
++)
461 short *sp
= XCALLOC (short, nedges
[i
] + 1);
468 for (i
= 0; i
< n
; i
++)
470 for (j
= 0; R_arg
[i
][j
] >= 0; ++j
)
472 *end_R
[R_arg
[i
][j
]] = i
;
473 ++end_R
[R_arg
[i
][j
]];
479 /* Free the input: it is replaced with the result. */
480 for (i
= 0; i
< n
; i
++)
486 fputs ("transpose: output\n", stderr
);
487 matrix_print (stderr
, new_R
, n
);
495 build_relations (void)
497 short *edge
= XCALLOC (short, ngotos
+ 1);
498 short *states
= XCALLOC (short, ritem_longest_rhs () + 1);
501 includes
= XCALLOC (short *, ngotos
);
503 for (i
= 0; i
< ngotos
; i
++)
506 int state1
= from_state
[i
];
507 int symbol1
= state_table
[to_state
[i
]].accessing_symbol
;
510 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
514 int stateno
= state1
;
518 for (rp
= ritem
+ rule_table
[*rulep
].rhs
; *rp
> 0; rp
++)
520 shifts
*sp
= state_table
[stateno
].shift_table
;
522 for (j
= 0; j
< sp
->nshifts
; j
++)
524 stateno
= sp
->shifts
[j
];
525 if (state_table
[stateno
].accessing_symbol
== *rp
)
529 states
[length
++] = stateno
;
532 if (!state_table
[stateno
].consistent
)
533 add_lookback_edge (stateno
, *rulep
, i
);
541 /* JF added rp>=ritem && I hope to god its right! */
542 if (rp
>= ritem
&& ISVAR (*rp
))
544 stateno
= states
[--length
];
545 edge
[nedges
++] = map_goto (stateno
, *rp
);
555 includes
[i
] = XCALLOC (short, nedges
+ 1);
556 for (j
= 0; j
< nedges
; j
++)
557 includes
[i
][j
] = edge
[j
];
558 includes
[i
][nedges
] = -1;
565 includes
= transpose (includes
, ngotos
);
571 compute_FOLLOWS (void)
577 for (i
= 0; i
< ngotos
; i
++)
585 compute_lookaheads (void)
590 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
591 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
593 int size
= LA (i
+ 1) - LA (i
);
595 for (j
= 0; j
< size
; ++j
)
596 LA (i
)[j
] |= F (sp
->value
)[j
];
600 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
601 LIST_FREE (shorts
, lookback
[i
]);
611 tokensetsize
= WORDSIZE (ntokens
);
619 compute_lookaheads ();