]>
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
];
369 nwords
= ngotos
* tokensetsize
;
370 F
= XCALLOC (unsigned, nwords
);
372 reads
= XCALLOC (short *, ngotos
);
373 edge
= XCALLOC (short, ngotos
+ 1);
377 for (i
= 0; i
< ngotos
; i
++)
379 int stateno
= to_state
[i
];
380 shifts
*sp
= state_table
[stateno
].shift_table
;
384 for (j
= 0; j
< sp
->nshifts
; j
++)
386 symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
389 SETBIT (rowp
, symbol
);
392 for (; j
< sp
->nshifts
; j
++)
394 symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
395 if (nullable
[symbol
])
396 edge
[nedges
++] = map_goto (stateno
, symbol
);
401 reads
[i
] = rp
= XCALLOC (short, nedges
+ 1);
403 for (j
= 0; j
< nedges
; j
++)
411 rowp
+= tokensetsize
;
416 for (i
= 0; i
< ngotos
; i
++)
425 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
432 i
= state_table
[stateno
].lookaheads
;
433 k
= state_table
[stateno
+ 1].lookaheads
;
435 while (!found
&& i
< k
)
437 if (LAruleno
[i
] == ruleno
)
445 sp
= XCALLOC (shorts
, 1);
446 sp
->next
= lookback
[i
];
453 transpose (short **R_arg
, int n
)
460 nedges
= XCALLOC (short, n
);
462 for (i
= 0; i
< n
; i
++)
464 short *sp
= R_arg
[i
];
472 new_R
= XCALLOC (short *, n
);
473 temp_R
= XCALLOC (short *, n
);
475 for (i
= 0; i
< n
; i
++)
478 short *sp
= XCALLOC (short, nedges
[i
] + 1);
486 for (i
= 0; i
< n
; i
++)
488 short *sp
= R_arg
[i
];
491 *temp_R
[*sp
++]++ = i
;
501 build_relations (void)
514 short **new_includes
;
516 includes
= XCALLOC (short *, ngotos
);
517 edge
= XCALLOC (short, ngotos
+ 1);
518 states
= XCALLOC (short, maxrhs () + 1);
520 for (i
= 0; i
< ngotos
; i
++)
523 state1
= from_state
[i
];
524 symbol1
= state_table
[to_state
[i
]].accessing_symbol
;
526 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
532 for (rp
= ritem
+ rule_table
[*rulep
].rhs
; *rp
> 0; rp
++)
535 shifts
*sp
= state_table
[stateno
].shift_table
;
537 for (j
= 0; j
< sp
->nshifts
; j
++)
539 stateno
= sp
->shifts
[j
];
540 if (state_table
[stateno
].accessing_symbol
== symbol2
)
544 states
[length
++] = stateno
;
547 if (!state_table
[stateno
].consistent
)
548 add_lookback_edge (stateno
, *rulep
, i
);
556 /* JF added rp>=ritem && I hope to god its right! */
557 if (rp
>= ritem
&& ISVAR (*rp
))
559 stateno
= states
[--length
];
560 edge
[nedges
++] = map_goto (stateno
, *rp
);
569 includes
[i
] = XCALLOC (short, nedges
+ 1);
570 for (j
= 0; j
< nedges
; j
++)
571 includes
[i
][j
] = edge
[j
];
572 includes
[i
][nedges
] = -1;
576 new_includes
= transpose (includes
, ngotos
);
578 for (i
= 0; i
< ngotos
; i
++)
582 includes
= new_includes
;
591 compute_FOLLOWS (void)
597 for (i
= 0; i
< ngotos
; i
++)
605 compute_lookaheads (void)
610 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
611 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
613 unsigned *fp1
= LA (i
);
614 unsigned *fp2
= F (sp
->value
);
615 while (fp1
< LA (i
+ 1))
620 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
621 LIST_FREE (shorts
, lookback
[i
]);
631 tokensetsize
= WORDSIZE (ntokens
);
639 compute_lookaheads ();