]>
git.saurik.com Git - bison.git/blob - src/lalr.c
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
= 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 /* 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
= XCALLOC (unsigned, nwords
);
407 reads
= XCALLOC (short *, ngotos
);
408 edge
= XCALLOC (short, ngotos
+ 1);
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
= XCALLOC (short, nedges
+ 1);
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
)
485 sp
= XCALLOC (shorts
, 1);
486 sp
->next
= lookback
[i
];
493 transpose (short **R_arg
, int n
)
502 nedges
= XCALLOC (short, n
);
504 for (i
= 0; i
< n
; i
++)
514 new_R
= XCALLOC (short *, n
);
515 temp_R
= XCALLOC (short *, n
);
517 for (i
= 0; i
< n
; i
++)
522 sp
= XCALLOC (short, k
+ 1);
531 for (i
= 0; i
< n
; i
++)
537 *temp_R
[*sp
++]++ = i
;
548 build_relations (void)
566 short **new_includes
;
568 includes
= XCALLOC (short *, ngotos
);
569 edge
= XCALLOC (short, ngotos
+ 1);
570 states
= XCALLOC (short, maxrhs
+ 1);
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
= XCALLOC (short, nedges
+ 1);
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 ();