]>
git.saurik.com Git - bison.git/blob - src/lalr.c
1 /* Compute look-ahead criteria for bison,
2 Copyright 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. */
35 /* All the decorated states, indexed by the state number. Warning:
36 there is a state_TABLE in LR0.c, but it is different and static.
38 state_t
*state_table
= NULL
;
47 reductions
**reduction_table
;
52 extern void berror
PARAMS ((const char *));
58 static short **includes
;
59 static shorts
**lookback
;
62 static short *VERTICES
;
79 INDEX
[i
] = height
= top
;
81 base
= F
+ i
* tokensetsize
;
82 fp3
= base
+ tokensetsize
;
87 while ((j
= *rp
++) >= 0)
92 if (INDEX
[i
] > INDEX
[j
])
96 fp2
= F
+ j
* tokensetsize
;
103 if (INDEX
[i
] == height
)
114 fp2
= F
+ j
* tokensetsize
;
124 digraph (short **relation
)
128 infinity
= ngotos
+ 2;
129 INDEX
= XCALLOC (short, ngotos
+ 1);
130 VERTICES
= XCALLOC (short, ngotos
+ 1);
135 for (i
= 0; i
< ngotos
; i
++)
138 for (i
= 0; i
< ngotos
; i
++)
140 if (INDEX
[i
] == 0 && R
[i
])
149 set_state_table (void)
153 state_table
= XCALLOC (state_t
, nstates
);
155 for (sp
= first_state
; sp
; sp
= sp
->next
)
157 state_table
[sp
->number
].state
= sp
;
158 state_table
[sp
->number
].accessing_symbol
= sp
->accessing_symbol
;
164 set_shift_table (void)
168 shift_table
= XCALLOC (shifts
*, nstates
);
170 for (sp
= first_shift
; sp
; sp
= sp
->next
)
171 shift_table
[sp
->number
] = sp
;
176 set_reduction_table (void)
180 reduction_table
= XCALLOC (reductions
*, nstates
);
182 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
183 reduction_table
[rp
->number
] = rp
;
196 for (itemp
= ritem
; *itemp
; itemp
++)
224 consistent
= XCALLOC (char, nstates
);
225 lookaheads
= XCALLOC (short, nstates
+ 1);
228 for (i
= 0; i
< nstates
; i
++)
232 lookaheads
[i
] = count
;
234 rp
= reduction_table
[i
];
238 || (sp
&& !ISVAR (state_table
[sp
->shifts
[0]].accessing_symbol
))))
244 for (k
= 0; k
< sp
->nshifts
; k
++)
245 if (state_table
[sp
->shifts
[k
]].accessing_symbol
246 == error_token_number
)
253 lookaheads
[nstates
] = count
;
257 LA
= XCALLOC (unsigned, 1 * tokensetsize
);
258 LAruleno
= XCALLOC (short, 1);
259 lookback
= XCALLOC (shorts
*, 1);
263 LA
= XCALLOC (unsigned, count
* tokensetsize
);
264 LAruleno
= XCALLOC (short, count
);
265 lookback
= XCALLOC (shorts
*, count
);
269 for (i
= 0; i
< nstates
; i
++)
273 if ((rp
= reduction_table
[i
]))
274 for (j
= 0; j
< rp
->nreds
; j
++)
275 *np
++ = rp
->rules
[j
];
292 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
293 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
296 for (sp
= first_shift
; sp
; sp
= sp
->next
)
298 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
300 symbol
= state_table
[sp
->shifts
[i
]].accessing_symbol
;
302 if (ISTOKEN (symbol
))
305 if (ngotos
== MAXSHORT
)
306 fatal (_("too many gotos (max %d)"), MAXSHORT
);
314 for (i
= ntokens
; i
< nsyms
; i
++)
320 for (i
= ntokens
; i
< nsyms
; i
++)
321 goto_map
[i
] = temp_map
[i
];
323 goto_map
[nsyms
] = ngotos
;
324 temp_map
[nsyms
] = ngotos
;
326 from_state
= XCALLOC (short, ngotos
);
327 to_state
= XCALLOC (short, ngotos
);
329 for (sp
= first_shift
; sp
; sp
= sp
->next
)
332 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
334 state2
= sp
->shifts
[i
];
335 symbol
= state_table
[state2
].accessing_symbol
;
337 if (ISTOKEN (symbol
))
340 k
= temp_map
[symbol
]++;
341 from_state
[k
] = state1
;
342 to_state
[k
] = state2
;
346 XFREE (temp_map
+ ntokens
);
351 /*----------------------------------------------------------.
352 | Map a state/symbol pair into its numeric representation. |
353 `----------------------------------------------------------*/
356 map_goto (int state
, int symbol
)
363 low
= goto_map
[symbol
];
364 high
= goto_map
[symbol
+ 1] - 1;
368 middle
= (low
+ high
) / 2;
369 s
= from_state
[middle
];
400 nwords
= ngotos
* tokensetsize
;
401 F
= XCALLOC (unsigned, nwords
);
403 reads
= XCALLOC (short *, ngotos
);
404 edge
= XCALLOC (short, ngotos
+ 1);
408 for (i
= 0; i
< ngotos
; i
++)
410 stateno
= to_state
[i
];
411 sp
= shift_table
[stateno
];
417 for (j
= 0; j
< k
; j
++)
419 symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
422 SETBIT (rowp
, symbol
);
427 symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
428 if (nullable
[symbol
])
429 edge
[nedges
++] = map_goto (stateno
, symbol
);
434 reads
[i
] = rp
= XCALLOC (short, nedges
+ 1);
436 for (j
= 0; j
< nedges
; j
++)
444 rowp
+= tokensetsize
;
449 for (i
= 0; i
< ngotos
; i
++)
461 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
468 i
= lookaheads
[stateno
];
469 k
= lookaheads
[stateno
+ 1];
471 while (!found
&& i
< k
)
473 if (LAruleno
[i
] == ruleno
)
481 sp
= XCALLOC (shorts
, 1);
482 sp
->next
= lookback
[i
];
489 transpose (short **R_arg
, int n
)
498 nedges
= XCALLOC (short, n
);
500 for (i
= 0; i
< n
; i
++)
510 new_R
= XCALLOC (short *, n
);
511 temp_R
= XCALLOC (short *, n
);
513 for (i
= 0; i
< n
; i
++)
518 sp
= XCALLOC (short, k
+ 1);
527 for (i
= 0; i
< n
; i
++)
533 *temp_R
[*sp
++]++ = i
;
544 build_relations (void)
562 short **new_includes
;
564 includes
= XCALLOC (short *, ngotos
);
565 edge
= XCALLOC (short, ngotos
+ 1);
566 states
= XCALLOC (short, maxrhs
+ 1);
568 for (i
= 0; i
< ngotos
; i
++)
571 state1
= from_state
[i
];
572 symbol1
= state_table
[to_state
[i
]].accessing_symbol
;
574 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
580 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
> 0; rp
++)
583 sp
= shift_table
[stateno
];
586 for (j
= 0; j
< k
; j
++)
588 stateno
= sp
->shifts
[j
];
589 if (state_table
[stateno
].accessing_symbol
== symbol2
)
593 states
[length
++] = stateno
;
596 if (!consistent
[stateno
])
597 add_lookback_edge (stateno
, *rulep
, i
);
605 /* JF added rp>=ritem && I hope to god its right! */
606 if (rp
>= ritem
&& ISVAR (*rp
))
608 stateno
= states
[--length
];
609 edge
[nedges
++] = map_goto (stateno
, *rp
);
618 includes
[i
] = shortp
= XCALLOC (short, nedges
+ 1);
619 for (j
= 0; j
< nedges
; j
++)
625 new_includes
= transpose (includes
, ngotos
);
627 for (i
= 0; i
< ngotos
; i
++)
633 includes
= new_includes
;
642 compute_FOLLOWS (void)
648 for (i
= 0; i
< ngotos
; i
++)
659 compute_lookaheads (void)
668 shorts
*sptmp
; /* JF */
671 n
= lookaheads
[nstates
];
672 for (i
= 0; i
< n
; i
++)
674 fp3
= rowp
+ tokensetsize
;
675 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
678 fp2
= F
+ tokensetsize
* sp
->value
;
686 for (i
= 0; i
< n
; i
++)
688 /* JF removed ref to freed storage */
689 for (sp
= lookback
[i
]; sp
; sp
= sptmp
)
704 tokensetsize
= WORDSIZE (ntokens
);
708 set_reduction_table ();
715 compute_lookaheads ();