]>
git.saurik.com Git - bison.git/blob - src/lalr.c
1 /* Compute look-ahead criteria for bison,
2 Copyright 1984, 1986, 1989, 2000 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 *));
54 static short **includes
;
55 static shorts
**lookback
;
58 static short *VERTICES
;
75 INDEX
[i
] = height
= top
;
77 base
= F
+ i
* tokensetsize
;
78 fp3
= base
+ tokensetsize
;
83 while ((j
= *rp
++) >= 0)
88 if (INDEX
[i
] > INDEX
[j
])
92 fp2
= F
+ j
* tokensetsize
;
99 if (INDEX
[i
] == height
)
110 fp2
= F
+ j
* tokensetsize
;
120 digraph (short **relation
)
124 infinity
= ngotos
+ 2;
125 INDEX
= XCALLOC (short, ngotos
+ 1);
126 VERTICES
= XCALLOC (short, ngotos
+ 1);
131 for (i
= 0; i
< ngotos
; i
++)
134 for (i
= 0; i
< ngotos
; i
++)
136 if (INDEX
[i
] == 0 && R
[i
])
145 /*--------------------.
146 | Build STATE_TABLE. |
147 `--------------------*/
150 set_state_table (void)
152 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
153 might be used (see conflicts.c). It is too opaque for me to
154 provide a probably less hacky implementation. --akim */
155 state_table
= XCALLOC (state_t
, nstates
+ 1);
159 for (sp
= first_state
; sp
; sp
= sp
->next
)
161 state_table
[sp
->number
].state
= sp
;
162 state_table
[sp
->number
].accessing_symbol
= sp
->accessing_symbol
;
168 for (sp
= first_shift
; sp
; sp
= sp
->next
)
169 state_table
[sp
->number
].shift_table
= sp
;
174 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
175 state_table
[rp
->number
].reduction_table
= rp
;
178 /* Initializing the lookaheads members. Please note that it must be
179 performed after having set some of the other members which are
180 used below. Change with extreme caution. */
184 for (i
= 0; i
< nstates
; i
++)
187 reductions
*rp
= state_table
[i
].reduction_table
;
188 shifts
*sp
= state_table
[i
].shift_table
;
190 state_table
[i
].lookaheads
= count
;
194 || (sp
&& !ISVAR (state_table
[sp
->shifts
[0]].accessing_symbol
))))
197 state_table
[i
].consistent
= 1;
200 for (k
= 0; k
< sp
->nshifts
; k
++)
201 if (state_table
[sp
->shifts
[k
]].accessing_symbol
202 == error_token_number
)
204 state_table
[i
].consistent
= 0;
208 state_table
[nstates
].lookaheads
= count
;
222 for (itemp
= ritem
; *itemp
; itemp
++)
248 size_t nLA
= state_table
[nstates
].lookaheads
;
252 LA
= XCALLOC (unsigned, nLA
* tokensetsize
);
253 LAruleno
= XCALLOC (short, nLA
);
254 lookback
= XCALLOC (shorts
*, nLA
);
257 for (i
= 0; i
< nstates
; i
++)
258 if (!state_table
[i
].consistent
)
259 if ((rp
= state_table
[i
].reduction_table
))
260 for (j
= 0; j
< rp
->nreds
; j
++)
261 *np
++ = rp
->rules
[j
];
276 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
277 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
280 for (sp
= first_shift
; sp
; sp
= sp
->next
)
282 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
284 symbol
= state_table
[sp
->shifts
[i
]].accessing_symbol
;
286 if (ISTOKEN (symbol
))
289 if (ngotos
== MAXSHORT
)
290 fatal (_("too many gotos (max %d)"), MAXSHORT
);
298 for (i
= ntokens
; i
< nsyms
; i
++)
304 for (i
= ntokens
; i
< nsyms
; i
++)
305 goto_map
[i
] = temp_map
[i
];
307 goto_map
[nsyms
] = ngotos
;
308 temp_map
[nsyms
] = ngotos
;
310 from_state
= XCALLOC (short, ngotos
);
311 to_state
= XCALLOC (short, ngotos
);
313 for (sp
= first_shift
; sp
; sp
= sp
->next
)
316 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
318 state2
= sp
->shifts
[i
];
319 symbol
= state_table
[state2
].accessing_symbol
;
321 if (ISTOKEN (symbol
))
324 k
= temp_map
[symbol
]++;
325 from_state
[k
] = state1
;
326 to_state
[k
] = state2
;
330 XFREE (temp_map
+ ntokens
);
335 /*----------------------------------------------------------.
336 | Map a state/symbol pair into its numeric representation. |
337 `----------------------------------------------------------*/
340 map_goto (int state
, int symbol
)
347 low
= goto_map
[symbol
];
348 high
= goto_map
[symbol
+ 1] - 1;
352 middle
= (low
+ high
) / 2;
353 s
= from_state
[middle
];
384 nwords
= ngotos
* tokensetsize
;
385 F
= XCALLOC (unsigned, nwords
);
387 reads
= XCALLOC (short *, ngotos
);
388 edge
= XCALLOC (short, ngotos
+ 1);
392 for (i
= 0; i
< ngotos
; i
++)
394 stateno
= to_state
[i
];
395 sp
= state_table
[stateno
].shift_table
;
401 for (j
= 0; j
< k
; j
++)
403 symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
406 SETBIT (rowp
, symbol
);
411 symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
412 if (nullable
[symbol
])
413 edge
[nedges
++] = map_goto (stateno
, symbol
);
418 reads
[i
] = rp
= XCALLOC (short, nedges
+ 1);
420 for (j
= 0; j
< nedges
; j
++)
428 rowp
+= tokensetsize
;
433 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
+ rrhs
[*rulep
]; *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
++)
643 compute_lookaheads (void)
651 shorts
*sptmp
; /* JF */
653 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
656 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
659 fp2
= F
+ tokensetsize
* sp
->value
;
665 for (i
= 0; i
< state_table
[nstates
].lookaheads
; i
++)
667 /* JF removed ref to freed storage */
668 for (sp
= lookback
[i
]; sp
; sp
= sptmp
)
683 tokensetsize
= WORDSIZE (ntokens
);
692 compute_lookaheads ();