]>
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.
58 extern short **derives
;
59 extern char *nullable
;
66 short *accessing_symbol
;
70 reductions
**reduction_table
;
75 extern void lalr
PARAMS((void));
77 static short **transpose
PARAMS((short **, int));
78 static void set_state_table
PARAMS((void));
79 static void set_accessing_symbol
PARAMS((void));
80 static void set_shift_table
PARAMS((void));
81 static void set_reduction_table
PARAMS((void));
82 static void set_maxrhs
PARAMS((void));
83 static void initialize_LA
PARAMS((void));
84 static void set_goto_map
PARAMS((void));
85 static int map_goto
PARAMS((int, int));
86 static void initialize_F
PARAMS((void));
87 static void build_relations
PARAMS((void));
88 static void add_lookback_edge
PARAMS((int, int, int));
89 static void compute_FOLLOWS
PARAMS((void));
90 static void compute_lookaheads
PARAMS((void));
91 static void digraph
PARAMS((short **));
92 static void traverse
PARAMS((register int));
94 extern void berror
PARAMS((const char *));
100 static short **includes
;
101 static shorts
**lookback
;
104 static short *VERTICES
;
111 tokensetsize
= WORDSIZE(ntokens
);
114 set_accessing_symbol();
116 set_reduction_table();
123 compute_lookaheads();
128 set_state_table (void)
132 state_table
= NEW2(nstates
, core
*);
134 for (sp
= first_state
; sp
; sp
= sp
->next
)
135 state_table
[sp
->number
] = sp
;
140 set_accessing_symbol (void)
144 accessing_symbol
= NEW2(nstates
, short);
146 for (sp
= first_state
; sp
; sp
= sp
->next
)
147 accessing_symbol
[sp
->number
] = sp
->accessing_symbol
;
152 set_shift_table (void)
156 shift_table
= NEW2(nstates
, shifts
*);
158 for (sp
= first_shift
; sp
; sp
= sp
->next
)
159 shift_table
[sp
->number
] = sp
;
164 set_reduction_table (void)
166 register reductions
*rp
;
168 reduction_table
= NEW2(nstates
, reductions
*);
170 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
171 reduction_table
[rp
->number
] = rp
;
178 register short *itemp
;
184 for (itemp
= ritem
; *itemp
; itemp
++)
192 if (length
> max
) max
= length
;
207 register reductions
*rp
;
211 consistent
= NEW2(nstates
, char);
212 lookaheads
= NEW2(nstates
+ 1, short);
215 for (i
= 0; i
< nstates
; i
++)
219 lookaheads
[i
] = count
;
221 rp
= reduction_table
[i
];
223 if (rp
&& (rp
->nreds
> 1
224 || (sp
&& ! ISVAR(accessing_symbol
[sp
->shifts
[0]]))))
230 for (k
= 0; k
< sp
->nshifts
; k
++)
232 if (accessing_symbol
[sp
->shifts
[k
]] == error_token_number
)
240 lookaheads
[nstates
] = count
;
244 LA
= NEW2(1 * tokensetsize
, unsigned);
245 LAruleno
= NEW2(1, short);
246 lookback
= NEW2(1, shorts
*);
250 LA
= NEW2(count
* tokensetsize
, unsigned);
251 LAruleno
= NEW2(count
, short);
252 lookback
= NEW2(count
, shorts
*);
256 for (i
= 0; i
< nstates
; i
++)
260 if ((rp
= reduction_table
[i
]))
261 for (j
= 0; j
< rp
->nreds
; j
++)
262 *np
++ = rp
->rules
[j
];
275 register short *temp_map
;
279 goto_map
= NEW2(nvars
+ 1, short) - ntokens
;
280 temp_map
= NEW2(nvars
+ 1, short) - ntokens
;
283 for (sp
= first_shift
; sp
; sp
= sp
->next
)
285 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
287 symbol
= accessing_symbol
[sp
->shifts
[i
]];
289 if (ISTOKEN(symbol
)) break;
291 if (ngotos
== MAXSHORT
)
292 fatal (_("too many gotos (max %d)"), MAXSHORT
);
300 for (i
= ntokens
; i
< nsyms
; i
++)
306 for (i
= ntokens
; i
< nsyms
; i
++)
307 goto_map
[i
] = temp_map
[i
];
309 goto_map
[nsyms
] = ngotos
;
310 temp_map
[nsyms
] = ngotos
;
312 from_state
= NEW2(ngotos
, short);
313 to_state
= NEW2(ngotos
, short);
315 for (sp
= first_shift
; sp
; sp
= sp
->next
)
318 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
320 state2
= sp
->shifts
[i
];
321 symbol
= accessing_symbol
[state2
];
323 if (ISTOKEN(symbol
)) break;
325 k
= temp_map
[symbol
]++;
326 from_state
[k
] = state1
;
327 to_state
[k
] = state2
;
331 FREE(temp_map
+ ntokens
);
336 /* Map_goto maps a state/symbol pair into its numeric representation. */
339 map_goto (int state
, int symbol
)
346 low
= goto_map
[symbol
];
347 high
= goto_map
[symbol
+ 1] - 1;
351 middle
= (low
+ high
) / 2;
352 s
= from_state
[middle
];
374 register short *edge
;
375 register unsigned *rowp
;
377 register short **reads
;
379 register int stateno
;
383 nwords
= ngotos
* tokensetsize
;
384 F
= NEW2(nwords
, unsigned);
386 reads
= NEW2(ngotos
, short *);
387 edge
= NEW2(ngotos
+ 1, short);
391 for (i
= 0; i
< ngotos
; i
++)
393 stateno
= to_state
[i
];
394 sp
= shift_table
[stateno
];
400 for (j
= 0; j
< k
; j
++)
402 symbol
= accessing_symbol
[sp
->shifts
[j
]];
405 SETBIT(rowp
, symbol
);
410 symbol
= accessing_symbol
[sp
->shifts
[j
]];
411 if (nullable
[symbol
])
412 edge
[nedges
++] = map_goto(stateno
, symbol
);
417 reads
[i
] = rp
= NEW2(nedges
+ 1, short);
419 for (j
= 0; j
< nedges
; j
++)
427 rowp
+= tokensetsize
;
432 for (i
= 0; i
< ngotos
; i
++)
444 build_relations (void)
449 register short *rulep
;
456 register int stateno
;
457 register int symbol1
;
458 register int symbol2
;
459 register short *shortp
;
460 register short *edge
;
461 register short *states
;
462 register short **new_includes
;
464 includes
= NEW2(ngotos
, short *);
465 edge
= NEW2(ngotos
+ 1, short);
466 states
= NEW2(maxrhs
+ 1, short);
468 for (i
= 0; i
< ngotos
; i
++)
471 state1
= from_state
[i
];
472 symbol1
= accessing_symbol
[to_state
[i
]];
474 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
480 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
> 0; rp
++)
483 sp
= shift_table
[stateno
];
486 for (j
= 0; j
< k
; j
++)
488 stateno
= sp
->shifts
[j
];
489 if (accessing_symbol
[stateno
] == symbol2
) break;
492 states
[length
++] = stateno
;
495 if (!consistent
[stateno
])
496 add_lookback_edge(stateno
, *rulep
, i
);
504 /* JF added rp>=ritem && I hope to god its right! */
505 if (rp
>=ritem
&& ISVAR(*rp
))
507 stateno
= states
[--length
];
508 edge
[nedges
++] = map_goto(stateno
, *rp
);
509 if (nullable
[*rp
]) done
= 0;
516 includes
[i
] = shortp
= NEW2(nedges
+ 1, short);
517 for (j
= 0; j
< nedges
; j
++)
523 new_includes
= transpose(includes
, ngotos
);
525 for (i
= 0; i
< ngotos
; i
++)
531 includes
= new_includes
;
539 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
546 i
= lookaheads
[stateno
];
547 k
= lookaheads
[stateno
+ 1];
549 while (!found
&& i
< k
)
551 if (LAruleno
[i
] == ruleno
)
558 berror("add_lookback_edge");
561 sp
->next
= lookback
[i
];
569 transpose (short **R_arg
, int n
)
571 register short **new_R
;
572 register short **temp_R
;
573 register short *nedges
;
578 nedges
= NEW2(n
, short);
580 for (i
= 0; i
< n
; i
++)
590 new_R
= NEW2(n
, short *);
591 temp_R
= NEW2(n
, short *);
593 for (i
= 0; i
< n
; i
++)
598 sp
= NEW2(k
+ 1, short);
607 for (i
= 0; i
< n
; i
++)
613 *temp_R
[*sp
++]++ = i
;
624 compute_FOLLOWS (void)
630 for (i
= 0; i
< ngotos
; i
++)
632 if (includes
[i
]) FREE(includes
[i
]);
640 compute_lookaheads (void)
644 register unsigned *fp1
;
645 register unsigned *fp2
;
646 register unsigned *fp3
;
648 register unsigned *rowp
;
649 /* register short *rulep; JF unused */
650 /* register int count; JF unused */
651 register shorts
*sptmp
;/* JF */
654 n
= lookaheads
[nstates
];
655 for (i
= 0; i
< n
; i
++)
657 fp3
= rowp
+ tokensetsize
;
658 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
661 fp2
= F
+ tokensetsize
* sp
->value
;
669 for (i
= 0; i
< n
; i
++)
670 {/* JF removed ref to freed storage */
671 for (sp
= lookback
[i
]; sp
; sp
= sptmp
) {
683 digraph (short **relation
)
687 infinity
= ngotos
+ 2;
688 INDEX
= NEW2(ngotos
+ 1, short);
689 VERTICES
= NEW2(ngotos
+ 1, short);
694 for (i
= 0; i
< ngotos
; i
++)
697 for (i
= 0; i
< ngotos
; i
++)
699 if (INDEX
[i
] == 0 && R
[i
])
709 traverse (register int i
)
711 register unsigned *fp1
;
712 register unsigned *fp2
;
713 register unsigned *fp3
;
721 INDEX
[i
] = height
= top
;
723 base
= F
+ i
* tokensetsize
;
724 fp3
= base
+ tokensetsize
;
729 while ((j
= *rp
++) >= 0)
734 if (INDEX
[i
] > INDEX
[j
])
738 fp2
= F
+ j
* tokensetsize
;
745 if (INDEX
[i
] == height
)
756 fp2
= F
+ j
* tokensetsize
;