]>
git.saurik.com Git - bison.git/blob - src/lalr.c
d4795b57e41b141246b8829eb6bf597707ff5bb9
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
;
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
= XCALLOC (short, ngotos
+ 1);
127 VERTICES
= XCALLOC (short, ngotos
+ 1);
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)
148 state_table
= XCALLOC (state_t
, nstates
);
152 for (sp
= first_state
; sp
; sp
= sp
->next
)
154 state_table
[sp
->number
].state
= sp
;
155 state_table
[sp
->number
].accessing_symbol
= sp
->accessing_symbol
;
161 for (sp
= first_shift
; sp
; sp
= sp
->next
)
162 state_table
[sp
->number
].shift_table
= sp
;
167 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
168 state_table
[rp
->number
].reduction_table
= rp
;
182 for (itemp
= ritem
; *itemp
; itemp
++)
210 lookaheads
= XCALLOC (short, nstates
+ 1);
213 for (i
= 0; i
< nstates
; i
++)
217 lookaheads
[i
] = count
;
219 rp
= state_table
[i
].reduction_table
;
220 sp
= state_table
[i
].shift_table
;
223 || (sp
&& !ISVAR (state_table
[sp
->shifts
[0]].accessing_symbol
))))
226 state_table
[i
].consistent
= 1;
229 for (k
= 0; k
< sp
->nshifts
; k
++)
230 if (state_table
[sp
->shifts
[k
]].accessing_symbol
231 == error_token_number
)
233 state_table
[i
].consistent
= 0;
238 lookaheads
[nstates
] = count
;
242 LA
= XCALLOC (unsigned, 1 * tokensetsize
);
243 LAruleno
= XCALLOC (short, 1);
244 lookback
= XCALLOC (shorts
*, 1);
248 LA
= XCALLOC (unsigned, count
* tokensetsize
);
249 LAruleno
= XCALLOC (short, count
);
250 lookback
= XCALLOC (shorts
*, count
);
254 for (i
= 0; i
< nstates
; i
++)
256 if (!state_table
[i
].consistent
)
258 if ((rp
= state_table
[i
].reduction_table
))
259 for (j
= 0; j
< rp
->nreds
; j
++)
260 *np
++ = rp
->rules
[j
];
277 goto_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
278 temp_map
= XCALLOC (short, nvars
+ 1) - ntokens
;
281 for (sp
= first_shift
; sp
; sp
= sp
->next
)
283 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
285 symbol
= state_table
[sp
->shifts
[i
]].accessing_symbol
;
287 if (ISTOKEN (symbol
))
290 if (ngotos
== MAXSHORT
)
291 fatal (_("too many gotos (max %d)"), MAXSHORT
);
299 for (i
= ntokens
; i
< nsyms
; i
++)
305 for (i
= ntokens
; i
< nsyms
; i
++)
306 goto_map
[i
] = temp_map
[i
];
308 goto_map
[nsyms
] = ngotos
;
309 temp_map
[nsyms
] = ngotos
;
311 from_state
= XCALLOC (short, ngotos
);
312 to_state
= XCALLOC (short, ngotos
);
314 for (sp
= first_shift
; sp
; sp
= sp
->next
)
317 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
319 state2
= sp
->shifts
[i
];
320 symbol
= state_table
[state2
].accessing_symbol
;
322 if (ISTOKEN (symbol
))
325 k
= temp_map
[symbol
]++;
326 from_state
[k
] = state1
;
327 to_state
[k
] = state2
;
331 XFREE (temp_map
+ ntokens
);
336 /*----------------------------------------------------------.
337 | Map a state/symbol pair into its numeric representation. |
338 `----------------------------------------------------------*/
341 map_goto (int state
, int symbol
)
348 low
= goto_map
[symbol
];
349 high
= goto_map
[symbol
+ 1] - 1;
353 middle
= (low
+ high
) / 2;
354 s
= from_state
[middle
];
385 nwords
= ngotos
* tokensetsize
;
386 F
= XCALLOC (unsigned, nwords
);
388 reads
= XCALLOC (short *, ngotos
);
389 edge
= XCALLOC (short, ngotos
+ 1);
393 for (i
= 0; i
< ngotos
; i
++)
395 stateno
= to_state
[i
];
396 sp
= state_table
[stateno
].shift_table
;
402 for (j
= 0; j
< k
; j
++)
404 symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
407 SETBIT (rowp
, symbol
);
412 symbol
= state_table
[sp
->shifts
[j
]].accessing_symbol
;
413 if (nullable
[symbol
])
414 edge
[nedges
++] = map_goto (stateno
, symbol
);
419 reads
[i
] = rp
= XCALLOC (short, nedges
+ 1);
421 for (j
= 0; j
< nedges
; j
++)
429 rowp
+= tokensetsize
;
434 for (i
= 0; i
< ngotos
; i
++)
446 add_lookback_edge (int stateno
, int ruleno
, int gotono
)
453 i
= lookaheads
[stateno
];
454 k
= lookaheads
[stateno
+ 1];
456 while (!found
&& i
< k
)
458 if (LAruleno
[i
] == ruleno
)
466 sp
= XCALLOC (shorts
, 1);
467 sp
->next
= lookback
[i
];
474 transpose (short **R_arg
, int n
)
483 nedges
= XCALLOC (short, n
);
485 for (i
= 0; i
< n
; i
++)
495 new_R
= XCALLOC (short *, n
);
496 temp_R
= XCALLOC (short *, n
);
498 for (i
= 0; i
< n
; i
++)
503 sp
= XCALLOC (short, k
+ 1);
512 for (i
= 0; i
< n
; i
++)
518 *temp_R
[*sp
++]++ = i
;
529 build_relations (void)
547 short **new_includes
;
549 includes
= XCALLOC (short *, ngotos
);
550 edge
= XCALLOC (short, ngotos
+ 1);
551 states
= XCALLOC (short, maxrhs
+ 1);
553 for (i
= 0; i
< ngotos
; i
++)
556 state1
= from_state
[i
];
557 symbol1
= state_table
[to_state
[i
]].accessing_symbol
;
559 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
565 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
> 0; rp
++)
568 sp
= state_table
[stateno
].shift_table
;
571 for (j
= 0; j
< k
; j
++)
573 stateno
= sp
->shifts
[j
];
574 if (state_table
[stateno
].accessing_symbol
== symbol2
)
578 states
[length
++] = stateno
;
581 if (!state_table
[stateno
].consistent
)
582 add_lookback_edge (stateno
, *rulep
, i
);
590 /* JF added rp>=ritem && I hope to god its right! */
591 if (rp
>= ritem
&& ISVAR (*rp
))
593 stateno
= states
[--length
];
594 edge
[nedges
++] = map_goto (stateno
, *rp
);
603 includes
[i
] = shortp
= XCALLOC (short, nedges
+ 1);
604 for (j
= 0; j
< nedges
; j
++)
610 new_includes
= transpose (includes
, ngotos
);
612 for (i
= 0; i
< ngotos
; i
++)
618 includes
= new_includes
;
627 compute_FOLLOWS (void)
633 for (i
= 0; i
< ngotos
; i
++)
644 compute_lookaheads (void)
653 shorts
*sptmp
; /* JF */
656 n
= lookaheads
[nstates
];
657 for (i
= 0; i
< n
; i
++)
659 fp3
= rowp
+ tokensetsize
;
660 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
663 fp2
= F
+ tokensetsize
* sp
->value
;
671 for (i
= 0; i
< n
; i
++)
673 /* JF removed ref to freed storage */
674 for (sp
= lookback
[i
]; sp
; sp
= sptmp
)
689 tokensetsize
= WORDSIZE (ntokens
);
698 compute_lookaheads ();