]>
git.saurik.com Git - bison.git/blob - src/lalr.c
af2ea10012d48519898a6df2865a8260b1d5d263
1 /* Compute look-ahead criteria for bison,
2 Copyright (C) 1984, 1986, 1989, 2000 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. */
40 short *accessing_symbol
;
44 reductions
**reduction_table
;
49 extern void berror
PARAMS ((const char *));
55 static short **includes
;
56 static shorts
**lookback
;
59 static short *VERTICES
;
76 INDEX
[i
] = height
= top
;
78 base
= F
+ i
* tokensetsize
;
79 fp3
= base
+ tokensetsize
;
84 while ((j
= *rp
++) >= 0)
89 if (INDEX
[i
] > INDEX
[j
])
93 fp2
= F
+ j
* tokensetsize
;
100 if (INDEX
[i
] == height
)
111 fp2
= F
+ j
* tokensetsize
;
121 digraph (short **relation
)
125 infinity
= ngotos
+ 2;
126 INDEX
= NEW2 (ngotos
+ 1, short);
127 VERTICES
= NEW2 (ngotos
+ 1, short);
132 for (i
= 0; i
< ngotos
; i
++)
135 for (i
= 0; i
< ngotos
; i
++)
137 if (INDEX
[i
] == 0 && R
[i
])
146 set_state_table (void)
150 state_table
= NEW2 (nstates
, core
*);
152 for (sp
= first_state
; sp
; sp
= sp
->next
)
153 state_table
[sp
->number
] = sp
;
158 set_accessing_symbol (void)
162 accessing_symbol
= NEW2 (nstates
, short);
164 for (sp
= first_state
; sp
; sp
= sp
->next
)
165 accessing_symbol
[sp
->number
] = sp
->accessing_symbol
;
170 set_shift_table (void)
174 shift_table
= NEW2 (nstates
, shifts
*);
176 for (sp
= first_shift
; sp
; sp
= sp
->next
)
177 shift_table
[sp
->number
] = sp
;
182 set_reduction_table (void)
186 reduction_table
= NEW2 (nstates
, reductions
*);
188 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
189 reduction_table
[rp
->number
] = rp
;
202 for (itemp
= ritem
; *itemp
; itemp
++)
230 consistent
= NEW2 (nstates
, char);
231 lookaheads
= NEW2 (nstates
+ 1, short);
234 for (i
= 0; i
< nstates
; i
++)
238 lookaheads
[i
] = count
;
240 rp
= reduction_table
[i
];
242 if (rp
&& (rp
->nreds
> 1
243 || (sp
&& !ISVAR (accessing_symbol
[sp
->shifts
[0]]))))
249 for (k
= 0; k
< sp
->nshifts
; k
++)
251 if (accessing_symbol
[sp
->shifts
[k
]] == error_token_number
)
259 lookaheads
[nstates
] = count
;
263 LA
= NEW2 (1 * tokensetsize
, unsigned);
264 LAruleno
= NEW2 (1, short);
265 lookback
= NEW2 (1, shorts
*);
269 LA
= NEW2 (count
* tokensetsize
, unsigned);
270 LAruleno
= NEW2 (count
, short);
271 lookback
= NEW2 (count
, shorts
*);
275 for (i
= 0; i
< nstates
; i
++)
279 if ((rp
= reduction_table
[i
]))
280 for (j
= 0; j
< rp
->nreds
; j
++)
281 *np
++ = rp
->rules
[j
];
298 goto_map
= NEW2 (nvars
+ 1, short) - ntokens
;
299 temp_map
= NEW2 (nvars
+ 1, short) - ntokens
;
302 for (sp
= first_shift
; sp
; sp
= sp
->next
)
304 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
306 symbol
= accessing_symbol
[sp
->shifts
[i
]];
308 if (ISTOKEN (symbol
))
311 if (ngotos
== MAXSHORT
)
312 fatal (_("too many gotos (max %d)"), MAXSHORT
);
320 for (i
= ntokens
; i
< nsyms
; i
++)
326 for (i
= ntokens
; i
< nsyms
; i
++)
327 goto_map
[i
] = temp_map
[i
];
329 goto_map
[nsyms
] = ngotos
;
330 temp_map
[nsyms
] = ngotos
;
332 from_state
= NEW2 (ngotos
, short);
333 to_state
= NEW2 (ngotos
, short);
335 for (sp
= first_shift
; sp
; sp
= sp
->next
)
338 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
340 state2
= sp
->shifts
[i
];
341 symbol
= accessing_symbol
[state2
];
343 if (ISTOKEN (symbol
))
346 k
= temp_map
[symbol
]++;
347 from_state
[k
] = state1
;
348 to_state
[k
] = state2
;
352 FREE (temp_map
+ ntokens
);
357 /* Map_goto maps a state/symbol pair into its numeric representation. */
360 map_goto (int state
, int symbol
)
367 low
= goto_map
[symbol
];
368 high
= goto_map
[symbol
+ 1] - 1;
372 middle
= (low
+ high
) / 2;
373 s
= from_state
[middle
];
404 nwords
= ngotos
* tokensetsize
;
405 F
= NEW2 (nwords
, unsigned);
407 reads
= NEW2 (ngotos
, short *);
408 edge
= NEW2 (ngotos
+ 1, short);
412 for (i
= 0; i
< ngotos
; i
++)
414 stateno
= to_state
[i
];
415 sp
= shift_table
[stateno
];
421 for (j
= 0; j
< k
; j
++)
423 symbol
= accessing_symbol
[sp
->shifts
[j
]];
426 SETBIT (rowp
, symbol
);
431 symbol
= accessing_symbol
[sp
->shifts
[j
]];
432 if (nullable
[symbol
])
433 edge
[nedges
++] = map_goto (stateno
, symbol
);
438 reads
[i
] = rp
= NEW2 (nedges
+ 1, short);
440 for (j
= 0; j
< nedges
; j
++)
448 rowp
+= tokensetsize
;
453 for (i
= 0; i
< ngotos
; i
++)
465 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
472 i
= lookaheads
[stateno
];
473 k
= lookaheads
[stateno
+ 1];
475 while (!found
&& i
< k
)
477 if (LAruleno
[i
] == ruleno
)
486 sp
->next
= lookback
[i
];
493 transpose (short **R_arg
, int n
)
502 nedges
= NEW2 (n
, short);
504 for (i
= 0; i
< n
; i
++)
514 new_R
= NEW2 (n
, short *);
515 temp_R
= NEW2 (n
, short *);
517 for (i
= 0; i
< n
; i
++)
522 sp
= NEW2 (k
+ 1, short);
531 for (i
= 0; i
< n
; i
++)
537 *temp_R
[*sp
++]++ = i
;
548 build_relations (void)
566 short **new_includes
;
568 includes
= NEW2 (ngotos
, short *);
569 edge
= NEW2 (ngotos
+ 1, short);
570 states
= NEW2 (maxrhs
+ 1, short);
572 for (i
= 0; i
< ngotos
; i
++)
575 state1
= from_state
[i
];
576 symbol1
= accessing_symbol
[to_state
[i
]];
578 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
584 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
> 0; rp
++)
587 sp
= shift_table
[stateno
];
590 for (j
= 0; j
< k
; j
++)
592 stateno
= sp
->shifts
[j
];
593 if (accessing_symbol
[stateno
] == symbol2
)
597 states
[length
++] = stateno
;
600 if (!consistent
[stateno
])
601 add_lookback_edge (stateno
, *rulep
, i
);
609 /* JF added rp>=ritem && I hope to god its right! */
610 if (rp
>= ritem
&& ISVAR (*rp
))
612 stateno
= states
[--length
];
613 edge
[nedges
++] = map_goto (stateno
, *rp
);
622 includes
[i
] = shortp
= NEW2 (nedges
+ 1, short);
623 for (j
= 0; j
< nedges
; j
++)
629 new_includes
= transpose (includes
, ngotos
);
631 for (i
= 0; i
< ngotos
; i
++)
637 includes
= new_includes
;
646 compute_FOLLOWS (void)
652 for (i
= 0; i
< ngotos
; i
++)
663 compute_lookaheads (void)
672 shorts
*sptmp
; /* JF */
675 n
= lookaheads
[nstates
];
676 for (i
= 0; i
< n
; i
++)
678 fp3
= rowp
+ tokensetsize
;
679 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
682 fp2
= F
+ tokensetsize
* sp
->value
;
690 for (i
= 0; i
< n
; i
++)
692 /* JF removed ref to freed storage */
693 for (sp
= lookback
[i
]; sp
; sp
= sptmp
)
708 tokensetsize
= WORDSIZE (ntokens
);
711 set_accessing_symbol ();
713 set_reduction_table ();
720 compute_lookaheads ();