static char sccsid
[] = "@(#)y4.c 4.1 (Berkeley) %G%";
int * ggreed
= lkst
[0].lset
;
int * pgo
= wsets
[0].ws
.lset
;
int *yypgo
= &nontrst
[0].tvalue
;
int maxspr
= 0; /* maximum spread of any entry */
int maxoff
= 0; /* maximum offset into a array */
register i
, *p
, j
, k
, *q
;
/* read the arrays from tempfile and set parameters */
if( (finput
=fopen(TEMPNAME
,"r")) == NULL
) error( "optimizer cannot open tempfile" );
yypact
[++nstate
] = (--pmem
) - mem
;
yypact
[nstate
] = yypgo
[0] = (--pmem
) - mem
;
yypgo
[++nnonter
]= pmem
-mem
;
yypgo
[nnonter
--] = (--pmem
) - mem
;
for( i
=0; i
<nstate
; ++i
){
for( p
= mem
+ yypact
[i
]; p
<q
; p
+= 2 ){
if( k
<= j
){ /* nontrivial situation */
/* temporarily, kill this for compatibility
j -= k; /* j is now the range */
if( k
> maxoff
) maxoff
= k
;
greed
[i
] = (yypact
[i
+1]-yypact
[i
]) + 2*j
;
if( j
> maxspr
) maxspr
= j
;
/* initialize ggreed table */
for( i
=1; i
<=nnonter
; ++i
){
/* minimum entry index is always 0 */
for( p
= mem
+yypgo
[i
]; p
<q
; p
+= 2 ) {
ggreed
[i
] = ggreed
[i
] + 2*j
;
if( j
> maxoff
) maxoff
= j
;
/* now, prepare to put the shift actions into the a array */
for( i
=0; i
<ACTSIZE
; ++i
) a
[i
] = 0;
for( i
=0; i
<nstate
; ++i
) {
if( greed
[i
]==0 && adb
>1 ) fprintf( ftable
, "State %d: null\n", i
);
while( (i
= nxti()) != NOMORE
) {
if( adb
>2 ){ /* print a array */
for( p
=a
; p
<= maxa
; p
+= 10){
fprintf( ftable
, "%4d ", p
-a
);
for( i
=0; i
<10; ++i
) fprintf( ftable
, "%4d ", p
[i
] );
/* write out the output appropriate to the language */
register *p
, *r
, *s
, *q1
, *q2
;
/* enter gotos on nonterminal i into array a */
q2
= mem
+ yypgo
[i
+1] - 1;
/* now, find a place for it */
for( p
=a
; p
< &a
[ACTSIZE
]; ++p
){
if( (maxa
=s
) > &a
[ACTSIZE
] ) error( "a array overflow" );
/* we have found a spot */
if( (maxa
=p
) > &a
[ACTSIZE
] ) error( "a array overflow" );
if( adb
>1 ) fprintf( ftable
, "Nonterminal %d, entry at %d\n" , i
, pgo
[i
] );
error( "cannot place goto %d\n", i
);
register *r
, *s
, n
, flag
, j
, *q1
, *q2
;
/* enter state i into the a array */
/* find an acceptable place */
for( n
= -maxoff
; n
<ACTSIZE
; ++n
){
for( r
= q1
; r
< q2
; r
+= 2 ){
if( (s
= *r
+ n
+ a
) < a
) goto nextn
;
else if( *s
!= r
[1] ) goto nextn
;
/* check that the position equals another only if the states are identical */
for( j
=0; j
<nstate
; ++j
){
if( flag
) goto nextn
; /* we have some disagreement */
if( yypact
[j
+1] + yypact
[i
] == yypact
[j
] + yypact
[i
+1] ){
if( adb
>1 ) fprintf( ftable
, "State %d: entry at %d equals state %d\n",
goto nextn
; /* we have some disagreement */
for( r
= q1
; r
< q2
; r
+= 2 ){
if( (s
= *r
+ n
+ a
) >= &a
[ACTSIZE
] ) error( "out of space in optimizer a array" );
if( *s
!= 0 && *s
!= r
[1] ) error( "clobber of a array, pos'n %d, by %d", s
-a
, r
[1] );
if( adb
>1 ) fprintf( ftable
, "State %d: entry at %d\n", i
, pa
[i
] );
error( "Error; failure to place state %d\n", i
);
nxti(){ /* finds the next i */
for( i
=1; i
<= nnonter
; ++i
) if( ggreed
[i
] >= max
){
for( i
=0; i
<nstate
; ++i
) if( greed
[i
] >= max
){
if( nxdb
) fprintf( ftable
, "nxti = %d, max = %d\n", maxi
, max
);
if( max
==0 ) return( NOMORE
);
if( foutput
== NULL
) return;
for( p
=maxa
; p
>=a
; --p
) {
fprintf( foutput
, "Optimizer space used: input %d/%d, output %d/%d\n",
pmem
-mem
+1, MEMSIZE
, maxa
-a
+1, ACTSIZE
);
fprintf( foutput
, "%d table entries, %d zero\n", (maxa
-a
)+1, i
);
fprintf( foutput
, "maximum spread: %d, maximum offset: %d\n", maxspr
, maxoff
);
aoutput(){ /* this version is for C */
/* write out the optimized parser */
fprintf( ftable
, "# define YYLAST %d\n", maxa
-a
+1 );
arout( "yyact", a
, (maxa
-a
)+1 );
arout( "yypact", pa
, nstate
);
arout( "yypgo", pgo
, nnonter
+1 );
arout( s
, v
, n
) char *s
; int *v
, n
; {
fprintf( ftable
, "short %s[]={\n", s
);
if( i
%10 == 0 ) fprintf( ftable
, "\n" );
fprintf( ftable
, "%4d", v
[i
] );
if( ++i
== n
) fprintf( ftable
, " };\n" );
else fprintf( ftable
, "," );
/* read and convert an integer from the standard input */
/* return the terminating character */
/* blanks, tabs, and newlines are ignored */
while( (c
=getc(finput
)) != EOF
){
val
= val
* 10 + c
- '0';
else if ( c
== '-' ) s
= -1;
if( pmem
> &mem
[MEMSIZE
] ) error( "out of space" );