]>
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. */
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
= 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)
150 state_table
= XCALLOC (core
*, nstates
);
152 for (sp
= first_state
; sp
; sp
= sp
->next
)
153 state_table
[sp
->number
] = sp
;
158 set_accessing_symbol (void)
162 accessing_symbol
= XCALLOC (short, nstates
);
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
= XCALLOC (shifts
*, nstates
);
176 for (sp
= first_shift
; sp
; sp
= sp
->next
)
177 shift_table
[sp
->number
] = sp
;
182 set_reduction_table (void)
186 reduction_table
= XCALLOC (reductions
*, nstates
);
188 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
189 reduction_table
[rp
->number
] = rp
;
202 for (itemp
= ritem
; *itemp
; itemp
++)
230 consistent
= XCALLOC (char, nstates
);
231 lookaheads
= XCALLOC (short, nstates
+ 1);
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
= XCALLOC (unsigned, 1 * tokensetsize
);
264 LAruleno
= XCALLOC (short, 1);
265 lookback
= XCALLOC (shorts
*, 1);
269 LA
= XCALLOC (unsigned, count
* tokensetsize
);
270 LAruleno
= XCALLOC (short, count
);
271 lookback
= XCALLOC (shorts
*, count
);
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
= XCALLOC (short, nvars
+ 1) - ntokens
;
299 temp_map
= XCALLOC (short, nvars
+ 1) - 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
= XCALLOC (short, ngotos
);
333 to_state
= XCALLOC (short, ngotos
);
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 XFREE (temp_map
+ ntokens
);
357 /*----------------------------------------------------------.
358 | Map a state/symbol pair into its numeric representation. |
359 `----------------------------------------------------------*/
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
= XCALLOC (unsigned, nwords
);
409 reads
= XCALLOC (short *, ngotos
);
410 edge
= XCALLOC (short, ngotos
+ 1);
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
= XCALLOC (short, nedges
+ 1);
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
)
487 sp
= XCALLOC (shorts
, 1);
488 sp
->next
= lookback
[i
];
495 transpose (short **R_arg
, int n
)
504 nedges
= XCALLOC (short, n
);
506 for (i
= 0; i
< n
; i
++)
516 new_R
= XCALLOC (short *, n
);
517 temp_R
= XCALLOC (short *, n
);
519 for (i
= 0; i
< n
; i
++)
524 sp
= XCALLOC (short, k
+ 1);
533 for (i
= 0; i
< n
; i
++)
539 *temp_R
[*sp
++]++ = i
;
550 build_relations (void)
568 short **new_includes
;
570 includes
= XCALLOC (short *, ngotos
);
571 edge
= XCALLOC (short, ngotos
+ 1);
572 states
= XCALLOC (short, maxrhs
+ 1);
574 for (i
= 0; i
< ngotos
; i
++)
577 state1
= from_state
[i
];
578 symbol1
= accessing_symbol
[to_state
[i
]];
580 for (rulep
= derives
[symbol1
]; *rulep
> 0; rulep
++)
586 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
> 0; rp
++)
589 sp
= shift_table
[stateno
];
592 for (j
= 0; j
< k
; j
++)
594 stateno
= sp
->shifts
[j
];
595 if (accessing_symbol
[stateno
] == symbol2
)
599 states
[length
++] = stateno
;
602 if (!consistent
[stateno
])
603 add_lookback_edge (stateno
, *rulep
, i
);
611 /* JF added rp>=ritem && I hope to god its right! */
612 if (rp
>= ritem
&& ISVAR (*rp
))
614 stateno
= states
[--length
];
615 edge
[nedges
++] = map_goto (stateno
, *rp
);
624 includes
[i
] = shortp
= XCALLOC (short, nedges
+ 1);
625 for (j
= 0; j
< nedges
; j
++)
631 new_includes
= transpose (includes
, ngotos
);
633 for (i
= 0; i
< ngotos
; i
++)
639 includes
= new_includes
;
648 compute_FOLLOWS (void)
654 for (i
= 0; i
< ngotos
; i
++)
665 compute_lookaheads (void)
674 shorts
*sptmp
; /* JF */
677 n
= lookaheads
[nstates
];
678 for (i
= 0; i
< n
; i
++)
680 fp3
= rowp
+ tokensetsize
;
681 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
684 fp2
= F
+ tokensetsize
* sp
->value
;
692 for (i
= 0; i
< n
; i
++)
694 /* JF removed ref to freed storage */
695 for (sp
= lookback
[i
]; sp
; sp
= sptmp
)
710 tokensetsize
= WORDSIZE (ntokens
);
713 set_accessing_symbol ();
715 set_reduction_table ();
722 compute_lookaheads ();