]>
git.saurik.com Git - bison.git/blob - src/lalr.c
36085271c090df60bd2853d607d2e2f775a1c1c7
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. */
34 extern short **derives
;
35 extern char *nullable
;
42 short *accessing_symbol
;
46 reductions
**reduction_table
;
51 extern void berror
PARAMS ((const char *));
57 static short **includes
;
58 static shorts
**lookback
;
61 static short *VERTICES
;
78 INDEX
[i
] = height
= top
;
80 base
= F
+ i
* tokensetsize
;
81 fp3
= base
+ tokensetsize
;
86 while ((j
= *rp
++) >= 0)
91 if (INDEX
[i
] > INDEX
[j
])
95 fp2
= F
+ j
* tokensetsize
;
102 if (INDEX
[i
] == height
)
113 fp2
= F
+ j
* tokensetsize
;
123 digraph (short **relation
)
127 infinity
= ngotos
+ 2;
128 INDEX
= NEW2 (ngotos
+ 1, short);
129 VERTICES
= NEW2 (ngotos
+ 1, short);
134 for (i
= 0; i
< ngotos
; i
++)
137 for (i
= 0; i
< ngotos
; i
++)
139 if (INDEX
[i
] == 0 && R
[i
])
148 set_state_table (void)
152 state_table
= NEW2 (nstates
, core
*);
154 for (sp
= first_state
; sp
; sp
= sp
->next
)
155 state_table
[sp
->number
] = sp
;
160 set_accessing_symbol (void)
164 accessing_symbol
= NEW2 (nstates
, short);
166 for (sp
= first_state
; sp
; sp
= sp
->next
)
167 accessing_symbol
[sp
->number
] = sp
->accessing_symbol
;
172 set_shift_table (void)
176 shift_table
= NEW2 (nstates
, shifts
*);
178 for (sp
= first_shift
; sp
; sp
= sp
->next
)
179 shift_table
[sp
->number
] = sp
;
184 set_reduction_table (void)
188 reduction_table
= NEW2 (nstates
, reductions
*);
190 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
191 reduction_table
[rp
->number
] = rp
;
204 for (itemp
= ritem
; *itemp
; itemp
++)
232 consistent
= NEW2 (nstates
, char);
233 lookaheads
= NEW2 (nstates
+ 1, short);
236 for (i
= 0; i
< nstates
; i
++)
240 lookaheads
[i
] = count
;
242 rp
= reduction_table
[i
];
244 if (rp
&& (rp
->nreds
> 1
245 || (sp
&& !ISVAR (accessing_symbol
[sp
->shifts
[0]]))))
251 for (k
= 0; k
< sp
->nshifts
; k
++)
253 if (accessing_symbol
[sp
->shifts
[k
]] == error_token_number
)
261 lookaheads
[nstates
] = count
;
265 LA
= NEW2 (1 * tokensetsize
, unsigned);
266 LAruleno
= NEW2 (1, short);
267 lookback
= NEW2 (1, shorts
*);
271 LA
= NEW2 (count
* tokensetsize
, unsigned);
272 LAruleno
= NEW2 (count
, short);
273 lookback
= NEW2 (count
, shorts
*);
277 for (i
= 0; i
< nstates
; i
++)
281 if ((rp
= reduction_table
[i
]))
282 for (j
= 0; j
< rp
->nreds
; j
++)
283 *np
++ = rp
->rules
[j
];
300 goto_map
= NEW2 (nvars
+ 1, short) - ntokens
;
301 temp_map
= NEW2 (nvars
+ 1, short) - ntokens
;
304 for (sp
= first_shift
; sp
; sp
= sp
->next
)
306 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
308 symbol
= accessing_symbol
[sp
->shifts
[i
]];
310 if (ISTOKEN (symbol
))
313 if (ngotos
== MAXSHORT
)
314 fatal (_("too many gotos (max %d)"), MAXSHORT
);
322 for (i
= ntokens
; i
< nsyms
; i
++)
328 for (i
= ntokens
; i
< nsyms
; i
++)
329 goto_map
[i
] = temp_map
[i
];
331 goto_map
[nsyms
] = ngotos
;
332 temp_map
[nsyms
] = ngotos
;
334 from_state
= NEW2 (ngotos
, short);
335 to_state
= NEW2 (ngotos
, short);
337 for (sp
= first_shift
; sp
; sp
= sp
->next
)
340 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
342 state2
= sp
->shifts
[i
];
343 symbol
= accessing_symbol
[state2
];
345 if (ISTOKEN (symbol
))
348 k
= temp_map
[symbol
]++;
349 from_state
[k
] = state1
;
350 to_state
[k
] = state2
;
354 FREE (temp_map
+ ntokens
);
359 /* Map_goto maps a state/symbol pair into its numeric representation. */
362 map_goto (int state
, int symbol
)
369 low
= goto_map
[symbol
];
370 high
= goto_map
[symbol
+ 1] - 1;
374 middle
= (low
+ high
) / 2;
375 s
= from_state
[middle
];
406 nwords
= ngotos
* tokensetsize
;
407 F
= NEW2 (nwords
, unsigned);
409 reads
= NEW2 (ngotos
, short *);
410 edge
= NEW2 (ngotos
+ 1, short);
414 for (i
= 0; i
< ngotos
; i
++)
416 stateno
= to_state
[i
];
417 sp
= shift_table
[stateno
];
423 for (j
= 0; j
< k
; j
++)
425 symbol
= accessing_symbol
[sp
->shifts
[j
]];
428 SETBIT (rowp
, symbol
);
433 symbol
= accessing_symbol
[sp
->shifts
[j
]];
434 if (nullable
[symbol
])
435 edge
[nedges
++] = map_goto (stateno
, symbol
);
440 reads
[i
] = rp
= NEW2 (nedges
+ 1, short);
442 for (j
= 0; j
< nedges
; j
++)
450 rowp
+= tokensetsize
;
455 for (i
= 0; i
< ngotos
; i
++)
467 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
474 i
= lookaheads
[stateno
];
475 k
= lookaheads
[stateno
+ 1];
477 while (!found
&& i
< k
)
479 if (LAruleno
[i
] == ruleno
)
486 berror ("add_lookback_edge");
489 sp
->next
= lookback
[i
];
496 transpose (short **R_arg
, int n
)
505 nedges
= NEW2 (n
, short);
507 for (i
= 0; i
< n
; i
++)
517 new_R
= NEW2 (n
, short *);
518 temp_R
= NEW2 (n
, short *);
520 for (i
= 0; i
< n
; i
++)
525 sp
= NEW2 (k
+ 1, short);
534 for (i
= 0; i
< n
; i
++)
540 *temp_R
[*sp
++]++ = i
;
551 build_relations (void)
569 short **new_includes
;
571 includes
= NEW2 (ngotos
, short *);
572 edge
= NEW2 (ngotos
+ 1, short);
573 states
= NEW2 (maxrhs
+ 1, short);
575 for (i
= 0; i
< ngotos
; i
++)
578 state1
= from_state
[i
];
579 symbol1
= accessing_symbol
[to_state
[i
]];
581 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
587 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
> 0; rp
++)
590 sp
= shift_table
[stateno
];
593 for (j
= 0; j
< k
; j
++)
595 stateno
= sp
->shifts
[j
];
596 if (accessing_symbol
[stateno
] == symbol2
)
600 states
[length
++] = stateno
;
603 if (!consistent
[stateno
])
604 add_lookback_edge (stateno
, *rulep
, i
);
612 /* JF added rp>=ritem && I hope to god its right! */
613 if (rp
>= ritem
&& ISVAR (*rp
))
615 stateno
= states
[--length
];
616 edge
[nedges
++] = map_goto (stateno
, *rp
);
625 includes
[i
] = shortp
= NEW2 (nedges
+ 1, short);
626 for (j
= 0; j
< nedges
; j
++)
632 new_includes
= transpose (includes
, ngotos
);
634 for (i
= 0; i
< ngotos
; i
++)
640 includes
= new_includes
;
649 compute_FOLLOWS (void)
655 for (i
= 0; i
< ngotos
; i
++)
666 compute_lookaheads (void)
675 shorts
*sptmp
; /* JF */
678 n
= lookaheads
[nstates
];
679 for (i
= 0; i
< n
; i
++)
681 fp3
= rowp
+ tokensetsize
;
682 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
685 fp2
= F
+ tokensetsize
* sp
->value
;
693 for (i
= 0; i
< n
; i
++)
695 /* JF removed ref to freed storage */
696 for (sp
= lookback
[i
]; sp
; sp
= sptmp
)
711 tokensetsize
= WORDSIZE (ntokens
);
714 set_accessing_symbol ();
716 set_reduction_table ();
723 compute_lookaheads ();