* Copyright (c) 1989 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)lalr.c 5.3 (Berkeley) 6/1/90";
reductions
**reduction_table
;
static shorts
**lookback
;
tokensetsize
= WORDSIZE(ntokens
);
state_table
= NEW2(nstates
, core
*);
for (sp
= first_state
; sp
; sp
= sp
->next
)
state_table
[sp
->number
] = sp
;
accessing_symbol
= NEW2(nstates
, short);
for (sp
= first_state
; sp
; sp
= sp
->next
)
accessing_symbol
[sp
->number
] = sp
->accessing_symbol
;
shift_table
= NEW2(nstates
, shifts
*);
for (sp
= first_shift
; sp
; sp
= sp
->next
)
shift_table
[sp
->number
] = sp
;
reduction_table
= NEW2(nstates
, reductions
*);
for (rp
= first_reduction
; rp
; rp
= rp
->next
)
reduction_table
[rp
->number
] = rp
;
register short *item_end
;
item_end
= ritem
+ nitems
;
for (itemp
= ritem
; itemp
< item_end
; itemp
++)
if (length
> max
) max
= length
;
lookaheads
= NEW2(nstates
+ 1, short);
for (i
= 0; i
< nstates
; i
++)
LA
= NEW2(k
* tokensetsize
, unsigned);
LAruleno
= NEW2(k
, short);
lookback
= NEW2(k
, shorts
*);
for (i
= 0; i
< nstates
; i
++)
for (j
= 0; j
< rp
->nreds
; j
++)
LAruleno
[k
] = rp
->rules
[j
];
register short *temp_map
;
goto_map
= NEW2(nvars
+ 1, short) - ntokens
;
temp_map
= NEW2(nvars
+ 1, short) - ntokens
;
for (sp
= first_shift
; sp
; sp
= sp
->next
)
for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
symbol
= accessing_symbol
[sp
->shift
[i
]];
if (ISTOKEN(symbol
)) break;
for (i
= ntokens
; i
< nsyms
; i
++)
for (i
= ntokens
; i
< nsyms
; i
++)
goto_map
[i
] = temp_map
[i
];
goto_map
[nsyms
] = ngotos
;
temp_map
[nsyms
] = ngotos
;
from_state
= NEW2(ngotos
, short);
to_state
= NEW2(ngotos
, short);
for (sp
= first_shift
; sp
; sp
= sp
->next
)
for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
symbol
= accessing_symbol
[state2
];
if (ISTOKEN(symbol
)) break;
FREE(temp_map
+ ntokens
);
/* Map_goto maps a state/symbol pair into its numeric representation. */
high
= goto_map
[symbol
+ 1];
middle
= (low
+ high
) >> 1;
nwords
= ngotos
* tokensetsize
;
F
= NEW2(nwords
, unsigned);
reads
= NEW2(ngotos
, short *);
edge
= NEW2(ngotos
+ 1, short);
for (i
= 0; i
< ngotos
; i
++)
sp
= shift_table
[stateno
];
symbol
= accessing_symbol
[sp
->shift
[j
]];
symbol
= accessing_symbol
[sp
->shift
[j
]];
edge
[nedges
++] = map_goto(stateno
, symbol
);
reads
[i
] = rp
= NEW2(nedges
+ 1, short);
for (j
= 0; j
< nedges
; j
++)
for (i
= 0; i
< ngotos
; i
++)
register short **new_includes
;
includes
= NEW2(ngotos
, short *);
edge
= NEW2(ngotos
+ 1, short);
states
= NEW2(maxrhs
+ 1, short);
for (i
= 0; i
< ngotos
; i
++)
symbol1
= accessing_symbol
[to_state
[i
]];
for (rulep
= derives
[symbol1
]; *rulep
>= 0; rulep
++)
for (rp
= ritem
+ rrhs
[*rulep
]; *rp
>= 0; rp
++)
sp
= shift_table
[stateno
];
if (accessing_symbol
[stateno
] == symbol2
) break;
states
[length
++] = stateno
;
add_lookback_edge(stateno
, *rulep
, i
);
stateno
= states
[--length
];
edge
[nedges
++] = map_goto(stateno
, *rp
);
if (nullable
[*rp
] && length
> 0) done
= 0;
includes
[i
] = shortp
= NEW2(nedges
+ 1, short);
for (j
= 0; j
< nedges
; j
++)
new_includes
= transpose(includes
, ngotos
);
for (i
= 0; i
< ngotos
; i
++)
add_lookback_edge(stateno
, ruleno
, gotono
)
int stateno
, ruleno
, gotono
;
k
= lookaheads
[stateno
+ 1];
if (LAruleno
[i
] == ruleno
)
new_R
= NEW2(n
, short *);
temp_R
= NEW2(n
, short *);
register unsigned *fp1
, *fp2
, *fp3
;
register shorts
*sp
, *next
;
fp3
= rowp
+ tokensetsize
;
for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
fp2
= F
+ tokensetsize
* sp
->value
;
for (sp
= lookback
[i
]; sp
; sp
= next
)
INDEX
= NEW2(ngotos
+ 1, short);
VERTICES
= NEW2(ngotos
+ 1, short);
for (i
= 0; i
< ngotos
; i
++)
for (i
= 0; i
< ngotos
; i
++)
if (INDEX
[i
] == 0 && R
[i
])
base
= F
+ i
* tokensetsize
;
fp3
= base
+ tokensetsize
;
fp2
= F
+ j
* tokensetsize
;
fp2
= F
+ j
* tokensetsize
;