]>
git.saurik.com Git - bison.git/blob - src/lalr.c
674a121a86ea482fdcf65906e22bbe44fb7f35e0
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. */
35 /* All the decorated states, indexed by the state number. Warning:
36 there is a state_TABLE in LR0.c, but it is different and static.
38 state_t
*state_table
= NULL
;
48 extern void berror
PARAMS ((const char *));
53 /* And for the famous F variable, which name is so descriptive that a
54 comment is hardly needed. <grin>. */
55 static unsigned *F
= NULL
;
56 #define F(Rule) (F + (Rule) * tokensetsize)
58 static short **includes
;
59 static shorts
**lookback
;
62 static short *VERTICES
;
72 size_t size
= F (i
+ 1) - F(i
);
75 INDEX
[i
] = height
= top
;
78 for (j
= 0; R
[i
][j
] >= 0; ++j
)
80 if (INDEX
[R
[i
][j
]] == 0)
83 if (INDEX
[i
] > INDEX
[R
[i
][j
]])
84 INDEX
[i
] = INDEX
[R
[i
][j
]];
86 for (k
= 0; k
< size
; ++k
)
87 F (i
)[k
] |= F (R
[i
][j
])[k
];
90 if (INDEX
[i
] == height
)
99 for (k
= 0; k
< size
; ++k
)
106 digraph (short **relation
)
110 infinity
= ngotos
+ 2;
111 INDEX
= XCALLOC (short, ngotos
+ 1);
112 VERTICES
= XCALLOC (short, ngotos
+ 1);
117 for (i
= 0; i
< ngotos
; i
++)
120 for (i
= 0; i
< ngotos
; i
++)
121 if (INDEX
[i
] == 0 && R
[i
])
129 /*--------------------.
130 | Build STATE_TABLE. |
131 `--------------------*/
134 set_state_table (void)
136 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
137 might be used (see conflicts.c). It is too opaque for me to
138 provide a probably less hacky implementation. --akim */
139 state_table
= XCALLOC (state_t
, nstates
+ 1);
143 for (sp
= first_state
; sp
; sp
= sp
->next
)
145 state_table
[sp
->number
].state
= sp
;
146 state_table
[sp
->number
].accessing_symbol
= sp
->accessing_symbol
;
152 for (sp
= first_shift
; sp
; sp
= sp
->next
)
153 state_table
[sp
->number
].shift_table
= sp
;
158 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
159 state_table
[rp
->number
].reduction_table
= rp
;
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
].reduction_table
;
172 shifts
*sp
= state_table
[i
].shift_table
;
174 state_table
[i
].lookaheads
= count
;
178 || (sp
&& !ISVAR (state_table
[sp
->shifts
[0]].accessing_symbol
))))
181 state_table
[i
].consistent
= 1;
184 for (k
= 0; k
< sp
->nshifts
; k
++)
185 if (state_table
[sp
->shifts
[k
]].accessing_symbol
186 == error_token_number
)
188 state_table
[i
].consistent
= 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
].reduction_table
))
217 for (j
= 0; j
< rp
->nreds
; j
++)
218 *np
++ = rp
->rules
[j
];
233 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
234 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
237 for (sp
= first_shift
; sp
; sp
= sp
->next
)
239 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
241 symbol
= state_table
[sp
->shifts
[i
]].accessing_symbol
;
243 if (ISTOKEN (symbol
))
246 if (ngotos
== MAXSHORT
)
247 fatal (_("too many gotos (max %d)"), MAXSHORT
);
255 for (i
= ntokens
; i
< nsyms
; i
++)
261 for (i
= ntokens
; i
< nsyms
; i
++)
262 goto_map
[i
] = temp_map
[i
];
264 goto_map
[nsyms
] = ngotos
;
265 temp_map
[nsyms
] = ngotos
;
267 from_state
= XCALLOC (short, ngotos
);
268 to_state
= XCALLOC (short, ngotos
);
270 for (sp
= first_shift
; sp
; sp
= sp
->next
)
273 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
275 state2
= sp
->shifts
[i
];
276 symbol
= state_table
[state2
].accessing_symbol
;
278 if (ISTOKEN (symbol
))
281 k
= temp_map
[symbol
]++;
282 from_state
[k
] = state1
;
283 to_state
[k
] = state2
;
287 XFREE (temp_map
+ ntokens
);
292 /*----------------------------------------------------------.
293 | Map a state/symbol pair into its numeric representation. |
294 `----------------------------------------------------------*/
297 map_goto (int state
, int symbol
)
304 low
= goto_map
[symbol
];
305 high
= goto_map
[symbol
+ 1] - 1;
309 middle
= (low
+ high
) / 2;
310 s
= from_state
[middle
];
328 short **reads
= XCALLOC (short *, ngotos
);
329 short *edge
= XCALLOC (short, ngotos
+ 1);
334 F
= XCALLOC (unsigned, ngotos
* tokensetsize
);
336 for (i
= 0; i
< ngotos
; i
++)
338 int stateno
= to_state
[i
];
339 shifts
*sp
= state_table
[stateno
].shift_table
;
344 for (j
= 0; j
< sp
->nshifts
; j
++)
346 int symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
349 SETBIT (F
+ i
* tokensetsize
, symbol
);
352 for (; j
< sp
->nshifts
; j
++)
354 int symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
355 if (nullable
[symbol
])
356 edge
[nedges
++] = map_goto (stateno
, symbol
);
361 reads
[i
] = XCALLOC (short, nedges
+ 1);
362 shortcpy (reads
[i
], edge
, nedges
);
363 reads
[i
][nedges
] = -1;
371 for (i
= 0; i
< ngotos
; i
++)
380 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
387 i
= state_table
[stateno
].lookaheads
;
388 k
= state_table
[stateno
+ 1].lookaheads
;
390 while (!found
&& i
< k
)
392 if (LAruleno
[i
] == ruleno
)
400 sp
= XCALLOC (shorts
, 1);
401 sp
->next
= lookback
[i
];
407 /*-------------------------------------------------------------------.
408 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
409 | replaced with the result. |
410 `-------------------------------------------------------------------*/
413 transpose (short **R_arg
, int n
)
420 nedges
= XCALLOC (short, n
);
422 for (i
= 0; i
< n
; i
++)
424 short *sp
= R_arg
[i
];
432 new_R
= XCALLOC (short *, n
);
433 temp_R
= XCALLOC (short *, n
);
435 for (i
= 0; i
< n
; i
++)
438 short *sp
= XCALLOC (short, nedges
[i
] + 1);
446 for (i
= 0; i
< n
; i
++)
448 short *sp
= R_arg
[i
];
451 *temp_R
[*sp
++]++ = i
;
456 /* Free the input: it is replaced with the result. */
457 for (i
= 0; i
< n
; i
++)
466 build_relations (void)
468 short *edge
= XCALLOC (short, ngotos
+ 1);
469 short *states
= XCALLOC (short, ritem_longest_rhs () + 1);
472 includes
= XCALLOC (short *, ngotos
);
474 for (i
= 0; i
< ngotos
; i
++)
477 int state1
= from_state
[i
];
478 int symbol1
= state_table
[to_state
[i
]].accessing_symbol
;
481 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
485 int stateno
= state1
;
489 for (rp
= ritem
+ rule_table
[*rulep
].rhs
; *rp
> 0; rp
++)
491 shifts
*sp
= state_table
[stateno
].shift_table
;
493 for (j
= 0; j
< sp
->nshifts
; j
++)
495 stateno
= sp
->shifts
[j
];
496 if (state_table
[stateno
].accessing_symbol
== *rp
)
500 states
[length
++] = stateno
;
503 if (!state_table
[stateno
].consistent
)
504 add_lookback_edge (stateno
, *rulep
, i
);
512 /* JF added rp>=ritem && I hope to god its right! */
513 if (rp
>= ritem
&& ISVAR (*rp
))
515 stateno
= states
[--length
];
516 edge
[nedges
++] = map_goto (stateno
, *rp
);
526 includes
[i
] = XCALLOC (short, nedges
+ 1);
527 for (j
= 0; j
< nedges
; j
++)
528 includes
[i
][j
] = edge
[j
];
529 includes
[i
][nedges
] = -1;
536 includes
= transpose (includes
, ngotos
);
542 compute_FOLLOWS (void)
548 for (i
= 0; i
< ngotos
; i
++)
556 compute_lookaheads (void)
561 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
562 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
564 int size
= LA (i
+ 1) - LA (i
);
566 for (j
= 0; j
< size
; ++j
)
567 LA (i
)[j
] |= F (sp
->value
)[j
];
571 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
572 LIST_FREE (shorts
, lookback
[i
]);
582 tokensetsize
= WORDSIZE (ntokens
);
590 compute_lookaheads ();