]>
git.saurik.com Git - bison.git/blob - src/lalr.c
1 /* Compute look-ahead criteria for bison,
2 Copyright (C) 1984, 1986, 1989 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 extern short **derives
;
41 short *accessing_symbol
;
45 reductions
**reduction_table
;
50 extern void berror
PARAMS ((const char *));
56 static short **includes
;
57 static shorts
**lookback
;
60 static short *VERTICES
;
77 INDEX
[i
] = height
= top
;
79 base
= F
+ i
* tokensetsize
;
80 fp3
= base
+ tokensetsize
;
85 while ((j
= *rp
++) >= 0)
90 if (INDEX
[i
] > INDEX
[j
])
94 fp2
= F
+ j
* tokensetsize
;
101 if (INDEX
[i
] == height
)
112 fp2
= F
+ j
* tokensetsize
;
122 digraph (short **relation
)
126 infinity
= ngotos
+ 2;
127 INDEX
= NEW2 (ngotos
+ 1, short);
128 VERTICES
= NEW2 (ngotos
+ 1, short);
133 for (i
= 0; i
< ngotos
; i
++)
136 for (i
= 0; i
< ngotos
; i
++)
138 if (INDEX
[i
] == 0 && R
[i
])
147 set_state_table (void)
151 state_table
= NEW2 (nstates
, core
*);
153 for (sp
= first_state
; sp
; sp
= sp
->next
)
154 state_table
[sp
->number
] = sp
;
159 set_accessing_symbol (void)
163 accessing_symbol
= NEW2 (nstates
, short);
165 for (sp
= first_state
; sp
; sp
= sp
->next
)
166 accessing_symbol
[sp
->number
] = sp
->accessing_symbol
;
171 set_shift_table (void)
175 shift_table
= NEW2 (nstates
, shifts
*);
177 for (sp
= first_shift
; sp
; sp
= sp
->next
)
178 shift_table
[sp
->number
] = sp
;
183 set_reduction_table (void)
187 reduction_table
= NEW2 (nstates
, reductions
*);
189 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
190 reduction_table
[rp
->number
] = rp
;
203 for (itemp
= ritem
; *itemp
; itemp
++)
231 consistent
= NEW2 (nstates
, char);
232 lookaheads
= NEW2 (nstates
+ 1, short);
235 for (i
= 0; i
< nstates
; i
++)
239 lookaheads
[i
] = count
;
241 rp
= reduction_table
[i
];
243 if (rp
&& (rp
->nreds
> 1
244 || (sp
&& !ISVAR (accessing_symbol
[sp
->shifts
[0]]))))
250 for (k
= 0; k
< sp
->nshifts
; k
++)
252 if (accessing_symbol
[sp
->shifts
[k
]] == error_token_number
)
260 lookaheads
[nstates
] = count
;
264 LA
= NEW2 (1 * tokensetsize
, unsigned);
265 LAruleno
= NEW2 (1, short);
266 lookback
= NEW2 (1, shorts
*);
270 LA
= NEW2 (count
* tokensetsize
, unsigned);
271 LAruleno
= NEW2 (count
, short);
272 lookback
= NEW2 (count
, shorts
*);
276 for (i
= 0; i
< nstates
; i
++)
280 if ((rp
= reduction_table
[i
]))
281 for (j
= 0; j
< rp
->nreds
; j
++)
282 *np
++ = rp
->rules
[j
];
299 goto_map
= NEW2 (nvars
+ 1, short) - ntokens
;
300 temp_map
= NEW2 (nvars
+ 1, short) - ntokens
;
303 for (sp
= first_shift
; sp
; sp
= sp
->next
)
305 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
307 symbol
= accessing_symbol
[sp
->shifts
[i
]];
309 if (ISTOKEN (symbol
))
312 if (ngotos
== MAXSHORT
)
313 fatal (_("too many gotos (max %d)"), MAXSHORT
);
321 for (i
= ntokens
; i
< nsyms
; i
++)
327 for (i
= ntokens
; i
< nsyms
; i
++)
328 goto_map
[i
] = temp_map
[i
];
330 goto_map
[nsyms
] = ngotos
;
331 temp_map
[nsyms
] = ngotos
;
333 from_state
= NEW2 (ngotos
, short);
334 to_state
= NEW2 (ngotos
, short);
336 for (sp
= first_shift
; sp
; sp
= sp
->next
)
339 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
341 state2
= sp
->shifts
[i
];
342 symbol
= accessing_symbol
[state2
];
344 if (ISTOKEN (symbol
))
347 k
= temp_map
[symbol
]++;
348 from_state
[k
] = state1
;
349 to_state
[k
] = state2
;
353 FREE (temp_map
+ ntokens
);
358 /* Map_goto maps a state/symbol pair into its numeric representation. */
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
= NEW2 (nwords
, unsigned);
408 reads
= NEW2 (ngotos
, short *);
409 edge
= NEW2 (ngotos
+ 1, short);
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
= NEW2 (nedges
+ 1, short);
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
)
485 berror ("add_lookback_edge");
488 sp
->next
= lookback
[i
];
495 transpose (short **R_arg
, int n
)
504 nedges
= NEW2 (n
, short);
506 for (i
= 0; i
< n
; i
++)
516 new_R
= NEW2 (n
, short *);
517 temp_R
= NEW2 (n
, short *);
519 for (i
= 0; i
< n
; i
++)
524 sp
= NEW2 (k
+ 1, short);
533 for (i
= 0; i
< n
; i
++)
539 *temp_R
[*sp
++]++ = i
;
550 build_relations (void)
568 short **new_includes
;
570 includes
= NEW2 (ngotos
, short *);
571 edge
= NEW2 (ngotos
+ 1, short);
572 states
= NEW2 (maxrhs
+ 1, short);
574 for (i
= 0; i
< ngotos
; i
++)
577 state1
= from_state
[i
];
578 symbol1
= accessing_symbol
[to_state
[i
]];
580 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
586 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
> 0; rp
++)
589 sp
= shift_table
[stateno
];
592 for (j
= 0; j
< k
; j
++)
594 stateno
= sp
->shifts
[j
];
595 if (accessing_symbol
[stateno
] == symbol2
)
599 states
[length
++] = stateno
;
602 if (!consistent
[stateno
])
603 add_lookback_edge (stateno
, *rulep
, i
);
611 /* JF added rp>=ritem && I hope to god its right! */
612 if (rp
>= ritem
&& ISVAR (*rp
))
614 stateno
= states
[--length
];
615 edge
[nedges
++] = map_goto (stateno
, *rp
);
624 includes
[i
] = shortp
= NEW2 (nedges
+ 1, short);
625 for (j
= 0; j
< nedges
; j
++)
631 new_includes
= transpose (includes
, ngotos
);
633 for (i
= 0; i
< ngotos
; i
++)
639 includes
= new_includes
;
648 compute_FOLLOWS (void)
654 for (i
= 0; i
< ngotos
; i
++)
665 compute_lookaheads (void)
674 shorts
*sptmp
; /* JF */
677 n
= lookaheads
[nstates
];
678 for (i
= 0; i
< n
; i
++)
680 fp3
= rowp
+ tokensetsize
;
681 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
684 fp2
= F
+ tokensetsize
* sp
->value
;
692 for (i
= 0; i
< n
; i
++)
694 /* JF removed ref to freed storage */
695 for (sp
= lookback
[i
]; sp
; sp
= sptmp
)
710 tokensetsize
= WORDSIZE (ntokens
);
713 set_accessing_symbol ();
715 set_reduction_table ();
722 compute_lookaheads ();