]>
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;
23 find which rules need lookahead in each state, and which lookahead tokens they accept.
25 lalr(), the entry point, builds these data structures:
27 goto_map, from_state and to_state
28 record each shift transition which accepts a variable (a nonterminal).
29 ngotos is the number of such transitions.
30 from_state[t] is the state number which a transition leads from
31 and to_state[t] is the state number it leads to.
32 All the transitions that accept a particular variable are grouped together and
33 goto_map[i - ntokens] is the index in from_state and to_state of the first of them.
35 consistent[s] is nonzero if no lookahead is needed to decide what to do in state s.
37 LAruleno is a vector which records the rules that need lookahead in various states.
38 The elements of LAruleno that apply to state s are those from
39 lookaheads[s] through lookaheads[s+1]-1.
40 Each element of LAruleno is a rule number.
42 If lr is the length of LAruleno, then a number from 0 to lr-1
43 can specify both a rule and a state where the rule might be applied.
45 LA is a lr by ntokens matrix of bits.
46 LA[l, i] is 1 if the rule LAruleno[l] is applicable in the appropriate state
47 when the next token is symbol i.
48 If LA[l, i] and LA[l, j] are both 1 for i != j, it is a conflict.
60 extern short **derives
;
61 extern char *nullable
;
68 short *accessing_symbol
;
72 reductions
**reduction_table
;
77 void lalr
PARAMS((void));
78 short **transpose
PARAMS((short **, int));
79 void set_state_table
PARAMS((void));
80 void set_accessing_symbol
PARAMS((void));
81 void set_shift_table
PARAMS((void));
82 void set_reduction_table
PARAMS((void));
83 void set_maxrhs
PARAMS((void));
84 void initialize_LA
PARAMS((void));
85 void set_goto_map
PARAMS((void));
86 int map_goto
PARAMS((int, int));
87 void initialize_F
PARAMS((void));
88 void build_relations
PARAMS((void));
89 void add_lookback_edge
PARAMS((int, int, int));
90 void compute_FOLLOWS
PARAMS((void));
91 void compute_lookaheads
PARAMS((void));
92 void digraph
PARAMS((short **));
93 void traverse
PARAMS((register int));
95 extern void toomany
PARAMS((char *));
96 extern void berror
PARAMS((char *));
102 static short **includes
;
103 static shorts
**lookback
;
106 static short *VERTICES
;
113 tokensetsize
= WORDSIZE(ntokens
);
116 set_accessing_symbol();
118 set_reduction_table();
125 compute_lookaheads();
130 set_state_table (void)
134 state_table
= NEW2(nstates
, core
*);
136 for (sp
= first_state
; sp
; sp
= sp
->next
)
137 state_table
[sp
->number
] = sp
;
142 set_accessing_symbol (void)
146 accessing_symbol
= NEW2(nstates
, short);
148 for (sp
= first_state
; sp
; sp
= sp
->next
)
149 accessing_symbol
[sp
->number
] = sp
->accessing_symbol
;
154 set_shift_table (void)
158 shift_table
= NEW2(nstates
, shifts
*);
160 for (sp
= first_shift
; sp
; sp
= sp
->next
)
161 shift_table
[sp
->number
] = sp
;
166 set_reduction_table (void)
168 register reductions
*rp
;
170 reduction_table
= NEW2(nstates
, reductions
*);
172 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
173 reduction_table
[rp
->number
] = rp
;
180 register short *itemp
;
186 for (itemp
= ritem
; *itemp
; itemp
++)
194 if (length
> max
) max
= length
;
209 register reductions
*rp
;
213 consistent
= NEW2(nstates
, char);
214 lookaheads
= NEW2(nstates
+ 1, short);
217 for (i
= 0; i
< nstates
; i
++)
221 lookaheads
[i
] = count
;
223 rp
= reduction_table
[i
];
225 if (rp
&& (rp
->nreds
> 1
226 || (sp
&& ! ISVAR(accessing_symbol
[sp
->shifts
[0]]))))
232 for (k
= 0; k
< sp
->nshifts
; k
++)
234 if (accessing_symbol
[sp
->shifts
[k
]] == error_token_number
)
242 lookaheads
[nstates
] = count
;
246 LA
= NEW2(1 * tokensetsize
, unsigned);
247 LAruleno
= NEW2(1, short);
248 lookback
= NEW2(1, shorts
*);
252 LA
= NEW2(count
* tokensetsize
, unsigned);
253 LAruleno
= NEW2(count
, short);
254 lookback
= NEW2(count
, shorts
*);
258 for (i
= 0; i
< nstates
; i
++)
262 if ((rp
= reduction_table
[i
]))
263 for (j
= 0; j
< rp
->nreds
; j
++)
264 *np
++ = rp
->rules
[j
];
277 register short *temp_map
;
281 goto_map
= NEW2(nvars
+ 1, short) - ntokens
;
282 temp_map
= NEW2(nvars
+ 1, short) - ntokens
;
285 for (sp
= first_shift
; sp
; sp
= sp
->next
)
287 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
289 symbol
= accessing_symbol
[sp
->shifts
[i
]];
291 if (ISTOKEN(symbol
)) break;
293 if (ngotos
== MAXSHORT
)
302 for (i
= ntokens
; i
< nsyms
; i
++)
308 for (i
= ntokens
; i
< nsyms
; i
++)
309 goto_map
[i
] = temp_map
[i
];
311 goto_map
[nsyms
] = ngotos
;
312 temp_map
[nsyms
] = ngotos
;
314 from_state
= NEW2(ngotos
, short);
315 to_state
= NEW2(ngotos
, short);
317 for (sp
= first_shift
; sp
; sp
= sp
->next
)
320 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
322 state2
= sp
->shifts
[i
];
323 symbol
= accessing_symbol
[state2
];
325 if (ISTOKEN(symbol
)) break;
327 k
= temp_map
[symbol
]++;
328 from_state
[k
] = state1
;
329 to_state
[k
] = state2
;
333 FREE(temp_map
+ ntokens
);
338 /* Map_goto maps a state/symbol pair into its numeric representation. */
341 map_goto (int state
, int symbol
)
348 low
= goto_map
[symbol
];
349 high
= goto_map
[symbol
+ 1] - 1;
353 middle
= (low
+ high
) / 2;
354 s
= from_state
[middle
];
376 register short *edge
;
377 register unsigned *rowp
;
379 register short **reads
;
381 register int stateno
;
385 nwords
= ngotos
* tokensetsize
;
386 F
= NEW2(nwords
, unsigned);
388 reads
= NEW2(ngotos
, short *);
389 edge
= NEW2(ngotos
+ 1, short);
393 for (i
= 0; i
< ngotos
; i
++)
395 stateno
= to_state
[i
];
396 sp
= shift_table
[stateno
];
402 for (j
= 0; j
< k
; j
++)
404 symbol
= accessing_symbol
[sp
->shifts
[j
]];
407 SETBIT(rowp
, symbol
);
412 symbol
= accessing_symbol
[sp
->shifts
[j
]];
413 if (nullable
[symbol
])
414 edge
[nedges
++] = map_goto(stateno
, symbol
);
419 reads
[i
] = rp
= NEW2(nedges
+ 1, short);
421 for (j
= 0; j
< nedges
; j
++)
429 rowp
+= tokensetsize
;
434 for (i
= 0; i
< ngotos
; i
++)
446 build_relations (void)
451 register short *rulep
;
458 register int stateno
;
459 register int symbol1
;
460 register int symbol2
;
461 register short *shortp
;
462 register short *edge
;
463 register short *states
;
464 register short **new_includes
;
466 includes
= NEW2(ngotos
, short *);
467 edge
= NEW2(ngotos
+ 1, short);
468 states
= NEW2(maxrhs
+ 1, short);
470 for (i
= 0; i
< ngotos
; i
++)
473 state1
= from_state
[i
];
474 symbol1
= accessing_symbol
[to_state
[i
]];
476 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
482 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
> 0; rp
++)
485 sp
= shift_table
[stateno
];
488 for (j
= 0; j
< k
; j
++)
490 stateno
= sp
->shifts
[j
];
491 if (accessing_symbol
[stateno
] == symbol2
) break;
494 states
[length
++] = stateno
;
497 if (!consistent
[stateno
])
498 add_lookback_edge(stateno
, *rulep
, i
);
506 /* JF added rp>=ritem && I hope to god its right! */
507 if (rp
>=ritem
&& ISVAR(*rp
))
509 stateno
= states
[--length
];
510 edge
[nedges
++] = map_goto(stateno
, *rp
);
511 if (nullable
[*rp
]) done
= 0;
518 includes
[i
] = shortp
= NEW2(nedges
+ 1, short);
519 for (j
= 0; j
< nedges
; j
++)
525 new_includes
= transpose(includes
, ngotos
);
527 for (i
= 0; i
< ngotos
; i
++)
533 includes
= new_includes
;
541 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
548 i
= lookaheads
[stateno
];
549 k
= lookaheads
[stateno
+ 1];
551 while (!found
&& i
< k
)
553 if (LAruleno
[i
] == ruleno
)
560 berror("add_lookback_edge");
563 sp
->next
= lookback
[i
];
571 transpose (short **R_arg
, int n
)
573 register short **new_R
;
574 register short **temp_R
;
575 register short *nedges
;
580 nedges
= NEW2(n
, short);
582 for (i
= 0; i
< n
; i
++)
592 new_R
= NEW2(n
, short *);
593 temp_R
= NEW2(n
, short *);
595 for (i
= 0; i
< n
; i
++)
600 sp
= NEW2(k
+ 1, short);
609 for (i
= 0; i
< n
; i
++)
615 *temp_R
[*sp
++]++ = i
;
626 compute_FOLLOWS (void)
632 for (i
= 0; i
< ngotos
; i
++)
634 if (includes
[i
]) FREE(includes
[i
]);
642 compute_lookaheads (void)
646 register unsigned *fp1
;
647 register unsigned *fp2
;
648 register unsigned *fp3
;
650 register unsigned *rowp
;
651 /* register short *rulep; JF unused */
652 /* register int count; JF unused */
653 register shorts
*sptmp
;/* JF */
656 n
= lookaheads
[nstates
];
657 for (i
= 0; i
< n
; i
++)
659 fp3
= rowp
+ tokensetsize
;
660 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
663 fp2
= F
+ tokensetsize
* sp
->value
;
671 for (i
= 0; i
< n
; i
++)
672 {/* JF removed ref to freed storage */
673 for (sp
= lookback
[i
]; sp
; sp
= sptmp
) {
685 digraph (short **relation
)
689 infinity
= ngotos
+ 2;
690 INDEX
= NEW2(ngotos
+ 1, short);
691 VERTICES
= NEW2(ngotos
+ 1, short);
696 for (i
= 0; i
< ngotos
; i
++)
699 for (i
= 0; i
< ngotos
; i
++)
701 if (INDEX
[i
] == 0 && R
[i
])
711 traverse (register int i
)
713 register unsigned *fp1
;
714 register unsigned *fp2
;
715 register unsigned *fp3
;
723 INDEX
[i
] = height
= top
;
725 base
= F
+ i
* tokensetsize
;
726 fp3
= base
+ tokensetsize
;
731 while ((j
= *rp
++) >= 0)
736 if (INDEX
[i
] > INDEX
[j
])
740 fp2
= F
+ j
* tokensetsize
;
747 if (INDEX
[i
] == height
)
758 fp2
= F
+ j
* tokensetsize
;