]>
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 named 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
;
79 INDEX
[i
] = height
= top
;
87 while ((j
= *rp
++) >= 0)
92 if (INDEX
[i
] > INDEX
[j
])
103 if (INDEX
[i
] == height
)
124 digraph (short **relation
)
128 infinity
= ngotos
+ 2;
129 INDEX
= XCALLOC (short, ngotos
+ 1);
130 VERTICES
= XCALLOC (short, ngotos
+ 1);
135 for (i
= 0; i
< ngotos
; i
++)
138 for (i
= 0; i
< ngotos
; i
++)
139 if (INDEX
[i
] == 0 && R
[i
])
147 /*--------------------.
148 | Build STATE_TABLE. |
149 `--------------------*/
152 set_state_table (void)
154 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
155 might be used (see conflicts.c). It is too opaque for me to
156 provide a probably less hacky implementation. --akim */
157 state_table
= XCALLOC (state_t
, nstates
+ 1);
161 for (sp
= first_state
; sp
; sp
= sp
->next
)
163 state_table
[sp
->number
].state
= sp
;
164 state_table
[sp
->number
].accessing_symbol
= sp
->accessing_symbol
;
170 for (sp
= first_shift
; sp
; sp
= sp
->next
)
171 state_table
[sp
->number
].shift_table
= sp
;
176 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
177 state_table
[rp
->number
].reduction_table
= rp
;
180 /* Initializing the lookaheads members. Please note that it must be
181 performed after having set some of the other members which are
182 used below. Change with extreme caution. */
186 for (i
= 0; i
< nstates
; i
++)
189 reductions
*rp
= state_table
[i
].reduction_table
;
190 shifts
*sp
= state_table
[i
].shift_table
;
192 state_table
[i
].lookaheads
= count
;
196 || (sp
&& !ISVAR (state_table
[sp
->shifts
[0]].accessing_symbol
))))
199 state_table
[i
].consistent
= 1;
202 for (k
= 0; k
< sp
->nshifts
; k
++)
203 if (state_table
[sp
->shifts
[k
]].accessing_symbol
204 == error_token_number
)
206 state_table
[i
].consistent
= 0;
210 state_table
[nstates
].lookaheads
= count
;
215 /* Return the size of the longest ride hand side of the rules. */
225 for (itemp
= ritem
; *itemp
; itemp
++)
251 size_t nLA
= state_table
[nstates
].lookaheads
;
255 LA
= XCALLOC (unsigned, nLA
* tokensetsize
);
256 LAruleno
= XCALLOC (short, nLA
);
257 lookback
= XCALLOC (shorts
*, nLA
);
260 for (i
= 0; i
< nstates
; i
++)
261 if (!state_table
[i
].consistent
)
262 if ((rp
= state_table
[i
].reduction_table
))
263 for (j
= 0; j
< rp
->nreds
; j
++)
264 *np
++ = rp
->rules
[j
];
279 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
280 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
283 for (sp
= first_shift
; sp
; sp
= sp
->next
)
285 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
287 symbol
= state_table
[sp
->shifts
[i
]].accessing_symbol
;
289 if (ISTOKEN (symbol
))
292 if (ngotos
== MAXSHORT
)
293 fatal (_("too many gotos (max %d)"), MAXSHORT
);
301 for (i
= ntokens
; i
< nsyms
; i
++)
307 for (i
= ntokens
; i
< nsyms
; i
++)
308 goto_map
[i
] = temp_map
[i
];
310 goto_map
[nsyms
] = ngotos
;
311 temp_map
[nsyms
] = ngotos
;
313 from_state
= XCALLOC (short, ngotos
);
314 to_state
= XCALLOC (short, ngotos
);
316 for (sp
= first_shift
; sp
; sp
= sp
->next
)
319 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
321 state2
= sp
->shifts
[i
];
322 symbol
= state_table
[state2
].accessing_symbol
;
324 if (ISTOKEN (symbol
))
327 k
= temp_map
[symbol
]++;
328 from_state
[k
] = state1
;
329 to_state
[k
] = state2
;
333 XFREE (temp_map
+ ntokens
);
338 /*----------------------------------------------------------.
339 | Map a state/symbol pair into its numeric representation. |
340 `----------------------------------------------------------*/
343 map_goto (int state
, int symbol
)
350 low
= goto_map
[symbol
];
351 high
= goto_map
[symbol
+ 1] - 1;
355 middle
= (low
+ high
) / 2;
356 s
= from_state
[middle
];
387 nwords
= ngotos
* tokensetsize
;
388 F
= XCALLOC (unsigned, nwords
);
390 reads
= XCALLOC (short *, ngotos
);
391 edge
= XCALLOC (short, ngotos
+ 1);
395 for (i
= 0; i
< ngotos
; i
++)
397 stateno
= to_state
[i
];
398 sp
= state_table
[stateno
].shift_table
;
404 for (j
= 0; j
< k
; j
++)
406 symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
409 SETBIT (rowp
, symbol
);
414 symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
415 if (nullable
[symbol
])
416 edge
[nedges
++] = map_goto (stateno
, symbol
);
421 reads
[i
] = rp
= XCALLOC (short, nedges
+ 1);
423 for (j
= 0; j
< nedges
; j
++)
431 rowp
+= tokensetsize
;
436 for (i
= 0; i
< ngotos
; i
++)
445 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
452 i
= state_table
[stateno
].lookaheads
;
453 k
= state_table
[stateno
+ 1].lookaheads
;
455 while (!found
&& i
< k
)
457 if (LAruleno
[i
] == ruleno
)
465 sp
= XCALLOC (shorts
, 1);
466 sp
->next
= lookback
[i
];
473 transpose (short **R_arg
, int n
)
482 nedges
= XCALLOC (short, n
);
484 for (i
= 0; i
< n
; i
++)
494 new_R
= XCALLOC (short *, n
);
495 temp_R
= XCALLOC (short *, n
);
497 for (i
= 0; i
< n
; i
++)
502 sp
= XCALLOC (short, k
+ 1);
511 for (i
= 0; i
< n
; i
++)
517 *temp_R
[*sp
++]++ = i
;
528 build_relations (void)
546 short **new_includes
;
548 includes
= XCALLOC (short *, ngotos
);
549 edge
= XCALLOC (short, ngotos
+ 1);
550 states
= XCALLOC (short, maxrhs () + 1);
552 for (i
= 0; i
< ngotos
; i
++)
555 state1
= from_state
[i
];
556 symbol1
= state_table
[to_state
[i
]].accessing_symbol
;
558 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
564 for (rp
= ritem
+ rule_table
[*rulep
].rhs
; *rp
> 0; rp
++)
567 sp
= state_table
[stateno
].shift_table
;
570 for (j
= 0; j
< k
; j
++)
572 stateno
= sp
->shifts
[j
];
573 if (state_table
[stateno
].accessing_symbol
== symbol2
)
577 states
[length
++] = stateno
;
580 if (!state_table
[stateno
].consistent
)
581 add_lookback_edge (stateno
, *rulep
, i
);
589 /* JF added rp>=ritem && I hope to god its right! */
590 if (rp
>= ritem
&& ISVAR (*rp
))
592 stateno
= states
[--length
];
593 edge
[nedges
++] = map_goto (stateno
, *rp
);
602 includes
[i
] = shortp
= XCALLOC (short, nedges
+ 1);
603 for (j
= 0; j
< nedges
; j
++)
609 new_includes
= transpose (includes
, ngotos
);
611 for (i
= 0; i
< ngotos
; i
++)
617 includes
= new_includes
;
626 compute_FOLLOWS (void)
632 for (i
= 0; i
< ngotos
; i
++)
640 compute_lookaheads (void)
645 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
646 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
648 unsigned *fp1
= LA (i
);
649 unsigned *fp2
= F (sp
->value
);
650 while (fp1
< LA (i
+ 1))
655 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
656 LIST_FREE (shorts
, lookback
[i
]);
666 tokensetsize
= WORDSIZE (ntokens
);
674 compute_lookaheads ();