]>
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* Compute how to make the finite state machine deterministic;
22 find which rules need lookahead in each state, and which lookahead tokens they accept.
24 lalr(), the entry point, builds these data structures:
26 goto_map, from_state and to_state
27 record each shift transition which accepts a variable (a nonterminal).
28 ngotos is the number of such transitions.
29 from_state[t] is the state number which a transition leads from
30 and to_state[t] is the state number it leads to.
31 All the transitions that accept a particular variable are grouped together and
32 goto_map[i - ntokens] is the index in from_state and to_state of the first of them.
34 consistent[s] is nonzero if no lookahead is needed to decide what to do in state s.
36 LAruleno is a vector which records the rules that need lookahead in various states.
37 The elements of LAruleno that apply to state s are those from
38 lookaheads[s] through lookaheads[s+1]-1.
39 Each element of LAruleno is a rule number.
41 If lr is the length of LAruleno, then a number from 0 to lr-1
42 can specify both a rule and a state where the rule might be applied.
44 LA is a lr by ntokens matrix of bits.
45 LA[l, i] is 1 if the rule LAruleno[l] is applicable in the appropriate state
46 when the next token is symbol i.
47 If LA[l, i] and LA[l, j] are both 1 for i != j, it is a conflict.
59 extern short **derives
;
60 extern char *nullable
;
67 short *accessing_symbol
;
71 reductions
**reduction_table
;
76 void lalr
PARAMS((void));
77 short **transpose
PARAMS((short **, int));
78 void set_state_table
PARAMS((void));
79 void set_accessing_symbol
PARAMS((void));
80 void set_shift_table
PARAMS((void));
81 void set_reduction_table
PARAMS((void));
82 void set_maxrhs
PARAMS((void));
83 void initialize_LA
PARAMS((void));
84 void set_goto_map
PARAMS((void));
85 int map_goto
PARAMS((int, int));
86 void initialize_F
PARAMS((void));
87 void build_relations
PARAMS((void));
88 void add_lookback_edge
PARAMS((int, int, int));
89 void compute_FOLLOWS
PARAMS((void));
90 void compute_lookaheads
PARAMS((void));
91 void digraph
PARAMS((short **));
92 void traverse
PARAMS((register int));
94 extern void toomany
PARAMS((char *));
95 extern void berror
PARAMS((char *));
101 static short **includes
;
102 static shorts
**lookback
;
105 static short *VERTICES
;
112 tokensetsize
= WORDSIZE(ntokens
);
115 set_accessing_symbol();
117 set_reduction_table();
124 compute_lookaheads();
129 set_state_table (void)
133 state_table
= NEW2(nstates
, core
*);
135 for (sp
= first_state
; sp
; sp
= sp
->next
)
136 state_table
[sp
->number
] = sp
;
141 set_accessing_symbol (void)
145 accessing_symbol
= NEW2(nstates
, short);
147 for (sp
= first_state
; sp
; sp
= sp
->next
)
148 accessing_symbol
[sp
->number
] = sp
->accessing_symbol
;
153 set_shift_table (void)
157 shift_table
= NEW2(nstates
, shifts
*);
159 for (sp
= first_shift
; sp
; sp
= sp
->next
)
160 shift_table
[sp
->number
] = sp
;
165 set_reduction_table (void)
167 register reductions
*rp
;
169 reduction_table
= NEW2(nstates
, reductions
*);
171 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
172 reduction_table
[rp
->number
] = rp
;
179 register short *itemp
;
185 for (itemp
= ritem
; *itemp
; itemp
++)
193 if (length
> max
) max
= length
;
208 register reductions
*rp
;
212 consistent
= NEW2(nstates
, char);
213 lookaheads
= NEW2(nstates
+ 1, short);
216 for (i
= 0; i
< nstates
; i
++)
220 lookaheads
[i
] = count
;
222 rp
= reduction_table
[i
];
224 if (rp
&& (rp
->nreds
> 1
225 || (sp
&& ! ISVAR(accessing_symbol
[sp
->shifts
[0]]))))
231 for (k
= 0; k
< sp
->nshifts
; k
++)
233 if (accessing_symbol
[sp
->shifts
[k
]] == error_token_number
)
241 lookaheads
[nstates
] = count
;
245 LA
= NEW2(1 * tokensetsize
, unsigned);
246 LAruleno
= NEW2(1, short);
247 lookback
= NEW2(1, shorts
*);
251 LA
= NEW2(count
* tokensetsize
, unsigned);
252 LAruleno
= NEW2(count
, short);
253 lookback
= NEW2(count
, shorts
*);
257 for (i
= 0; i
< nstates
; i
++)
261 if ((rp
= reduction_table
[i
]))
262 for (j
= 0; j
< rp
->nreds
; j
++)
263 *np
++ = rp
->rules
[j
];
276 register short *temp_map
;
280 goto_map
= NEW2(nvars
+ 1, short) - ntokens
;
281 temp_map
= NEW2(nvars
+ 1, short) - ntokens
;
284 for (sp
= first_shift
; sp
; sp
= sp
->next
)
286 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
288 symbol
= accessing_symbol
[sp
->shifts
[i
]];
290 if (ISTOKEN(symbol
)) break;
292 if (ngotos
== MAXSHORT
)
301 for (i
= ntokens
; i
< nsyms
; i
++)
307 for (i
= ntokens
; i
< nsyms
; i
++)
308 goto_map
[i
] = temp_map
[i
];
310 goto_map
[nsyms
] = ngotos
;
311 temp_map
[nsyms
] = ngotos
;
313 from_state
= NEW2(ngotos
, short);
314 to_state
= NEW2(ngotos
, short);
316 for (sp
= first_shift
; sp
; sp
= sp
->next
)
319 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
321 state2
= sp
->shifts
[i
];
322 symbol
= accessing_symbol
[state2
];
324 if (ISTOKEN(symbol
)) break;
326 k
= temp_map
[symbol
]++;
327 from_state
[k
] = state1
;
328 to_state
[k
] = state2
;
332 FREE(temp_map
+ ntokens
);
337 /* Map_goto maps a state/symbol pair into its numeric representation. */
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
];
375 register short *edge
;
376 register unsigned *rowp
;
378 register short **reads
;
380 register int stateno
;
384 nwords
= ngotos
* tokensetsize
;
385 F
= NEW2(nwords
, unsigned);
387 reads
= NEW2(ngotos
, short *);
388 edge
= NEW2(ngotos
+ 1, short);
392 for (i
= 0; i
< ngotos
; i
++)
394 stateno
= to_state
[i
];
395 sp
= shift_table
[stateno
];
401 for (j
= 0; j
< k
; j
++)
403 symbol
= accessing_symbol
[sp
->shifts
[j
]];
406 SETBIT(rowp
, symbol
);
411 symbol
= accessing_symbol
[sp
->shifts
[j
]];
412 if (nullable
[symbol
])
413 edge
[nedges
++] = map_goto(stateno
, symbol
);
418 reads
[i
] = rp
= NEW2(nedges
+ 1, short);
420 for (j
= 0; j
< nedges
; j
++)
428 rowp
+= tokensetsize
;
433 for (i
= 0; i
< ngotos
; i
++)
445 build_relations (void)
450 register short *rulep
;
457 register int stateno
;
458 register int symbol1
;
459 register int symbol2
;
460 register short *shortp
;
461 register short *edge
;
462 register short *states
;
463 register short **new_includes
;
465 includes
= NEW2(ngotos
, short *);
466 edge
= NEW2(ngotos
+ 1, short);
467 states
= NEW2(maxrhs
+ 1, short);
469 for (i
= 0; i
< ngotos
; i
++)
472 state1
= from_state
[i
];
473 symbol1
= accessing_symbol
[to_state
[i
]];
475 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
481 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
> 0; rp
++)
484 sp
= shift_table
[stateno
];
487 for (j
= 0; j
< k
; j
++)
489 stateno
= sp
->shifts
[j
];
490 if (accessing_symbol
[stateno
] == symbol2
) break;
493 states
[length
++] = stateno
;
496 if (!consistent
[stateno
])
497 add_lookback_edge(stateno
, *rulep
, i
);
505 /* JF added rp>=ritem && I hope to god its right! */
506 if (rp
>=ritem
&& ISVAR(*rp
))
508 stateno
= states
[--length
];
509 edge
[nedges
++] = map_goto(stateno
, *rp
);
510 if (nullable
[*rp
]) done
= 0;
517 includes
[i
] = shortp
= NEW2(nedges
+ 1, short);
518 for (j
= 0; j
< nedges
; j
++)
524 new_includes
= transpose(includes
, ngotos
);
526 for (i
= 0; i
< ngotos
; i
++)
532 includes
= new_includes
;
540 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
547 i
= lookaheads
[stateno
];
548 k
= lookaheads
[stateno
+ 1];
550 while (!found
&& i
< k
)
552 if (LAruleno
[i
] == ruleno
)
559 berror("add_lookback_edge");
562 sp
->next
= lookback
[i
];
570 transpose (short **R_arg
, int n
)
572 register short **new_R
;
573 register short **temp_R
;
574 register short *nedges
;
579 nedges
= NEW2(n
, short);
581 for (i
= 0; i
< n
; i
++)
591 new_R
= NEW2(n
, short *);
592 temp_R
= NEW2(n
, short *);
594 for (i
= 0; i
< n
; i
++)
599 sp
= NEW2(k
+ 1, short);
608 for (i
= 0; i
< n
; i
++)
614 *temp_R
[*sp
++]++ = i
;
625 compute_FOLLOWS (void)
631 for (i
= 0; i
< ngotos
; i
++)
633 if (includes
[i
]) FREE(includes
[i
]);
641 compute_lookaheads (void)
645 register unsigned *fp1
;
646 register unsigned *fp2
;
647 register unsigned *fp3
;
649 register unsigned *rowp
;
650 /* register short *rulep; JF unused */
651 /* register int count; JF unused */
652 register shorts
*sptmp
;/* JF */
655 n
= lookaheads
[nstates
];
656 for (i
= 0; i
< n
; i
++)
658 fp3
= rowp
+ tokensetsize
;
659 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
662 fp2
= F
+ tokensetsize
* sp
->value
;
670 for (i
= 0; i
< n
; i
++)
671 {/* JF removed ref to freed storage */
672 for (sp
= lookback
[i
]; sp
; sp
= sptmp
) {
684 digraph (short **relation
)
688 infinity
= ngotos
+ 2;
689 INDEX
= NEW2(ngotos
+ 1, short);
690 VERTICES
= NEW2(ngotos
+ 1, short);
695 for (i
= 0; i
< ngotos
; i
++)
698 for (i
= 0; i
< ngotos
; i
++)
700 if (INDEX
[i
] == 0 && R
[i
])
710 traverse (register int i
)
712 register unsigned *fp1
;
713 register unsigned *fp2
;
714 register unsigned *fp3
;
722 INDEX
[i
] = height
= top
;
724 base
= F
+ i
* tokensetsize
;
725 fp3
= base
+ tokensetsize
;
730 while ((j
= *rp
++) >= 0)
735 if (INDEX
[i
] > INDEX
[j
])
739 fp2
= F
+ j
* tokensetsize
;
746 if (INDEX
[i
] == height
)
757 fp2
= F
+ j
* tokensetsize
;