]>
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. */
39 short *accessing_symbol
;
43 reductions
**reduction_table
;
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 set_state_table (void)
149 state_table
= XCALLOC (core
*, nstates
);
151 for (sp
= first_state
; sp
; sp
= sp
->next
)
152 state_table
[sp
->number
] = sp
;
157 set_accessing_symbol (void)
161 accessing_symbol
= XCALLOC (short, nstates
);
163 for (sp
= first_state
; sp
; sp
= sp
->next
)
164 accessing_symbol
[sp
->number
] = sp
->accessing_symbol
;
169 set_shift_table (void)
173 shift_table
= XCALLOC (shifts
*, nstates
);
175 for (sp
= first_shift
; sp
; sp
= sp
->next
)
176 shift_table
[sp
->number
] = sp
;
181 set_reduction_table (void)
185 reduction_table
= XCALLOC (reductions
*, nstates
);
187 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
188 reduction_table
[rp
->number
] = rp
;
201 for (itemp
= ritem
; *itemp
; itemp
++)
229 consistent
= XCALLOC (char, nstates
);
230 lookaheads
= XCALLOC (short, nstates
+ 1);
233 for (i
= 0; i
< nstates
; i
++)
237 lookaheads
[i
] = count
;
239 rp
= reduction_table
[i
];
241 if (rp
&& (rp
->nreds
> 1
242 || (sp
&& !ISVAR (accessing_symbol
[sp
->shifts
[0]]))))
248 for (k
= 0; k
< sp
->nshifts
; k
++)
250 if (accessing_symbol
[sp
->shifts
[k
]] == error_token_number
)
258 lookaheads
[nstates
] = count
;
262 LA
= XCALLOC (unsigned, 1 * tokensetsize
);
263 LAruleno
= XCALLOC (short, 1);
264 lookback
= XCALLOC (shorts
*, 1);
268 LA
= XCALLOC (unsigned, count
* tokensetsize
);
269 LAruleno
= XCALLOC (short, count
);
270 lookback
= XCALLOC (shorts
*, count
);
274 for (i
= 0; i
< nstates
; i
++)
278 if ((rp
= reduction_table
[i
]))
279 for (j
= 0; j
< rp
->nreds
; j
++)
280 *np
++ = rp
->rules
[j
];
297 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
298 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
301 for (sp
= first_shift
; sp
; sp
= sp
->next
)
303 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
305 symbol
= accessing_symbol
[sp
->shifts
[i
]];
307 if (ISTOKEN (symbol
))
310 if (ngotos
== MAXSHORT
)
311 fatal (_("too many gotos (max %d)"), MAXSHORT
);
319 for (i
= ntokens
; i
< nsyms
; i
++)
325 for (i
= ntokens
; i
< nsyms
; i
++)
326 goto_map
[i
] = temp_map
[i
];
328 goto_map
[nsyms
] = ngotos
;
329 temp_map
[nsyms
] = ngotos
;
331 from_state
= XCALLOC (short, ngotos
);
332 to_state
= XCALLOC (short, ngotos
);
334 for (sp
= first_shift
; sp
; sp
= sp
->next
)
337 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
339 state2
= sp
->shifts
[i
];
340 symbol
= accessing_symbol
[state2
];
342 if (ISTOKEN (symbol
))
345 k
= temp_map
[symbol
]++;
346 from_state
[k
] = state1
;
347 to_state
[k
] = state2
;
351 XFREE (temp_map
+ ntokens
);
356 /*----------------------------------------------------------.
357 | Map a state/symbol pair into its numeric representation. |
358 `----------------------------------------------------------*/
361 map_goto (int state
, int symbol
)
368 low
= goto_map
[symbol
];
369 high
= goto_map
[symbol
+ 1] - 1;
373 middle
= (low
+ high
) / 2;
374 s
= from_state
[middle
];
405 nwords
= ngotos
* tokensetsize
;
406 F
= XCALLOC (unsigned, nwords
);
408 reads
= XCALLOC (short *, ngotos
);
409 edge
= XCALLOC (short, ngotos
+ 1);
413 for (i
= 0; i
< ngotos
; i
++)
415 stateno
= to_state
[i
];
416 sp
= shift_table
[stateno
];
422 for (j
= 0; j
< k
; j
++)
424 symbol
= accessing_symbol
[sp
->shifts
[j
]];
427 SETBIT (rowp
, symbol
);
432 symbol
= accessing_symbol
[sp
->shifts
[j
]];
433 if (nullable
[symbol
])
434 edge
[nedges
++] = map_goto (stateno
, symbol
);
439 reads
[i
] = rp
= XCALLOC (short, nedges
+ 1);
441 for (j
= 0; j
< nedges
; j
++)
449 rowp
+= tokensetsize
;
454 for (i
= 0; i
< ngotos
; i
++)
466 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
473 i
= lookaheads
[stateno
];
474 k
= lookaheads
[stateno
+ 1];
476 while (!found
&& i
< k
)
478 if (LAruleno
[i
] == ruleno
)
486 sp
= XCALLOC (shorts
, 1);
487 sp
->next
= lookback
[i
];
494 transpose (short **R_arg
, int n
)
503 nedges
= XCALLOC (short, n
);
505 for (i
= 0; i
< n
; i
++)
515 new_R
= XCALLOC (short *, n
);
516 temp_R
= XCALLOC (short *, n
);
518 for (i
= 0; i
< n
; i
++)
523 sp
= XCALLOC (short, k
+ 1);
532 for (i
= 0; i
< n
; i
++)
538 *temp_R
[*sp
++]++ = i
;
549 build_relations (void)
567 short **new_includes
;
569 includes
= XCALLOC (short *, ngotos
);
570 edge
= XCALLOC (short, ngotos
+ 1);
571 states
= XCALLOC (short, maxrhs
+ 1);
573 for (i
= 0; i
< ngotos
; i
++)
576 state1
= from_state
[i
];
577 symbol1
= accessing_symbol
[to_state
[i
]];
579 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
585 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
> 0; rp
++)
588 sp
= shift_table
[stateno
];
591 for (j
= 0; j
< k
; j
++)
593 stateno
= sp
->shifts
[j
];
594 if (accessing_symbol
[stateno
] == symbol2
)
598 states
[length
++] = stateno
;
601 if (!consistent
[stateno
])
602 add_lookback_edge (stateno
, *rulep
, i
);
610 /* JF added rp>=ritem && I hope to god its right! */
611 if (rp
>= ritem
&& ISVAR (*rp
))
613 stateno
= states
[--length
];
614 edge
[nedges
++] = map_goto (stateno
, *rp
);
623 includes
[i
] = shortp
= XCALLOC (short, nedges
+ 1);
624 for (j
= 0; j
< nedges
; j
++)
630 new_includes
= transpose (includes
, ngotos
);
632 for (i
= 0; i
< ngotos
; i
++)
638 includes
= new_includes
;
647 compute_FOLLOWS (void)
653 for (i
= 0; i
< ngotos
; i
++)
664 compute_lookaheads (void)
673 shorts
*sptmp
; /* JF */
676 n
= lookaheads
[nstates
];
677 for (i
= 0; i
< n
; i
++)
679 fp3
= rowp
+ tokensetsize
;
680 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
683 fp2
= F
+ tokensetsize
* sp
->value
;
691 for (i
= 0; i
< n
; i
++)
693 /* JF removed ref to freed storage */
694 for (sp
= lookback
[i
]; sp
; sp
= sptmp
)
709 tokensetsize
= WORDSIZE (ntokens
);
712 set_accessing_symbol ();
714 set_reduction_table ();
721 compute_lookaheads ();