]>
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. */
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
;
197 /*------------------------------------------.
198 | Return the size of the longest rule RHS. |
199 `------------------------------------------*/
210 for (itemp
= ritem
; *itemp
; itemp
++)
236 size_t nLA
= state_table
[nstates
].lookaheads
;
240 LA
= XCALLOC (unsigned, nLA
* tokensetsize
);
241 LAruleno
= XCALLOC (short, nLA
);
242 lookback
= XCALLOC (shorts
*, nLA
);
245 for (i
= 0; i
< nstates
; i
++)
246 if (!state_table
[i
].consistent
)
247 if ((rp
= state_table
[i
].reduction_table
))
248 for (j
= 0; j
< rp
->nreds
; j
++)
249 *np
++ = rp
->rules
[j
];
264 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
265 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
268 for (sp
= first_shift
; sp
; sp
= sp
->next
)
270 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
272 symbol
= state_table
[sp
->shifts
[i
]].accessing_symbol
;
274 if (ISTOKEN (symbol
))
277 if (ngotos
== MAXSHORT
)
278 fatal (_("too many gotos (max %d)"), MAXSHORT
);
286 for (i
= ntokens
; i
< nsyms
; i
++)
292 for (i
= ntokens
; i
< nsyms
; i
++)
293 goto_map
[i
] = temp_map
[i
];
295 goto_map
[nsyms
] = ngotos
;
296 temp_map
[nsyms
] = ngotos
;
298 from_state
= XCALLOC (short, ngotos
);
299 to_state
= XCALLOC (short, ngotos
);
301 for (sp
= first_shift
; sp
; sp
= sp
->next
)
304 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
306 state2
= sp
->shifts
[i
];
307 symbol
= state_table
[state2
].accessing_symbol
;
309 if (ISTOKEN (symbol
))
312 k
= temp_map
[symbol
]++;
313 from_state
[k
] = state1
;
314 to_state
[k
] = state2
;
318 XFREE (temp_map
+ ntokens
);
323 /*----------------------------------------------------------.
324 | Map a state/symbol pair into its numeric representation. |
325 `----------------------------------------------------------*/
328 map_goto (int state
, int symbol
)
335 low
= goto_map
[symbol
];
336 high
= goto_map
[symbol
+ 1] - 1;
340 middle
= (low
+ high
) / 2;
341 s
= from_state
[middle
];
359 short **reads
= XCALLOC (short *, ngotos
);
360 short *edge
= XCALLOC (short, ngotos
+ 1);
365 F
= XCALLOC (unsigned, ngotos
* tokensetsize
);
367 for (i
= 0; i
< ngotos
; i
++)
369 int stateno
= to_state
[i
];
370 shifts
*sp
= state_table
[stateno
].shift_table
;
375 for (j
= 0; j
< sp
->nshifts
; j
++)
377 int symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
380 SETBIT (F
+ i
* tokensetsize
, symbol
);
383 for (; j
< sp
->nshifts
; j
++)
385 int symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
386 if (nullable
[symbol
])
387 edge
[nedges
++] = map_goto (stateno
, symbol
);
392 reads
[i
] = XCALLOC (short, nedges
+ 1);
393 shortcpy (reads
[i
], edge
, nedges
);
394 reads
[i
][nedges
] = -1;
402 for (i
= 0; i
< ngotos
; i
++)
411 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
418 i
= state_table
[stateno
].lookaheads
;
419 k
= state_table
[stateno
+ 1].lookaheads
;
421 while (!found
&& i
< k
)
423 if (LAruleno
[i
] == ruleno
)
431 sp
= XCALLOC (shorts
, 1);
432 sp
->next
= lookback
[i
];
438 /*-------------------------------------------------------------------.
439 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
440 | replaced with the result. |
441 `-------------------------------------------------------------------*/
444 transpose (short **R_arg
, int n
)
451 nedges
= XCALLOC (short, n
);
453 for (i
= 0; i
< n
; i
++)
455 short *sp
= R_arg
[i
];
463 new_R
= XCALLOC (short *, n
);
464 temp_R
= XCALLOC (short *, n
);
466 for (i
= 0; i
< n
; i
++)
469 short *sp
= XCALLOC (short, nedges
[i
] + 1);
477 for (i
= 0; i
< n
; i
++)
479 short *sp
= R_arg
[i
];
482 *temp_R
[*sp
++]++ = i
;
487 /* Free the input: it is replaced with the result. */
488 for (i
= 0; i
< n
; i
++)
497 build_relations (void)
499 short *edge
= XCALLOC (short, ngotos
+ 1);
500 short *states
= XCALLOC (short, maxrhs () + 1);
503 includes
= XCALLOC (short *, ngotos
);
505 for (i
= 0; i
< ngotos
; i
++)
508 int state1
= from_state
[i
];
509 int symbol1
= state_table
[to_state
[i
]].accessing_symbol
;
512 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
516 int stateno
= state1
;
520 for (rp
= ritem
+ rule_table
[*rulep
].rhs
; *rp
> 0; rp
++)
522 shifts
*sp
= state_table
[stateno
].shift_table
;
524 for (j
= 0; j
< sp
->nshifts
; j
++)
526 stateno
= sp
->shifts
[j
];
527 if (state_table
[stateno
].accessing_symbol
== *rp
)
531 states
[length
++] = stateno
;
534 if (!state_table
[stateno
].consistent
)
535 add_lookback_edge (stateno
, *rulep
, i
);
543 /* JF added rp>=ritem && I hope to god its right! */
544 if (rp
>= ritem
&& ISVAR (*rp
))
546 stateno
= states
[--length
];
547 edge
[nedges
++] = map_goto (stateno
, *rp
);
557 includes
[i
] = XCALLOC (short, nedges
+ 1);
558 for (j
= 0; j
< nedges
; j
++)
559 includes
[i
][j
] = edge
[j
];
560 includes
[i
][nedges
] = -1;
567 includes
= transpose (includes
, ngotos
);
573 compute_FOLLOWS (void)
579 for (i
= 0; i
< ngotos
; i
++)
587 compute_lookaheads (void)
592 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
593 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
595 int size
= LA (i
+ 1) - LA (i
);
597 for (j
= 0; j
< size
; ++j
)
598 LA (i
)[j
] |= F (sp
->value
)[j
];
602 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
603 LIST_FREE (shorts
, lookback
[i
]);
613 tokensetsize
= WORDSIZE (ntokens
);
621 compute_lookaheads ();