Lemon update 2009-11-03 19:18:32 on branch trunk

- Enhancements to lemon to generate more compact action tables and to avoid making array bounds tests that can never fail on action table calculations. (user: drh)
- Update zcc-parse.lemon: YY_SZ_ACTTAB is now YY_ACTTAB_COUNT
This commit is contained in:
Randy Heit 2016-03-20 09:31:48 -05:00
parent cafbc8b50a
commit 1c592c9601
3 changed files with 48 additions and 28 deletions

View file

@ -90,7 +90,7 @@ static void SetNodeLine(ZCC_TreeNode *name, int line)
for (int j = 1; j < YYERRORSYMBOL; ++j) for (int j = 1; j < YYERRORSYMBOL; ++j)
{ {
int k = i + j; int k = i + j;
if (k >= 0 && k < YY_SZ_ACTTAB && yy_lookahead[k] == j) if (k >= 0 && k < YY_ACTTAB_COUNT && yy_lookahead[k] == j)
{ {
expecting << (expecting.IsEmpty() ? "Expecting " : " or ") << ZCCTokenName(j); expecting << (expecting.IsEmpty() ? "Expecting " : " or ") << ZCCTokenName(j);
} }

View file

@ -524,21 +524,9 @@ int acttab_insert(acttab *p){
** **
** i is the index in p->aAction[] where p->mnLookahead is inserted. ** i is the index in p->aAction[] where p->mnLookahead is inserted.
*/ */
for(i=0; i<p->nAction+p->mnLookahead; i++){ for(i=p->nAction-1; i>=0; i--){
if( p->aAction[i].lookahead<0 ){ /* First look for an existing action table entry that can be reused */
for(j=0; j<p->nLookahead; j++){ if( p->aAction[i].lookahead==p->mnLookahead ){
k = p->aLookahead[j].lookahead - p->mnLookahead + i;
if( k<0 ) break;
if( p->aAction[k].lookahead>=0 ) break;
}
if( j<p->nLookahead ) continue;
for(j=0; j<p->nAction; j++){
if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
}
if( j==p->nAction ){
break; /* Fits in empty slots */
}
}else if( p->aAction[i].lookahead==p->mnLookahead ){
if( p->aAction[i].action!=p->mnAction ) continue; if( p->aAction[i].action!=p->mnAction ) continue;
for(j=0; j<p->nLookahead; j++){ for(j=0; j<p->nLookahead; j++){
k = p->aLookahead[j].lookahead - p->mnLookahead + i; k = p->aLookahead[j].lookahead - p->mnLookahead + i;
@ -557,6 +545,25 @@ int acttab_insert(acttab *p){
} }
} }
} }
if( i<0 ){
/* If no reusable entry is found, look for an empty slot */
for(i=0; i<p->nAction; i++){
if( p->aAction[i].lookahead<0 ){
for(j=0; j<p->nLookahead; j++){
k = p->aLookahead[j].lookahead - p->mnLookahead + i;
if( k<0 ) break;
if( p->aAction[k].lookahead>=0 ) break;
}
if( j<p->nLookahead ) continue;
for(j=0; j<p->nAction; j++){
if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
}
if( j==p->nAction ){
break; /* Fits in empty slots */
}
}
}
}
/* Insert transaction set at index i. */ /* Insert transaction set at index i. */
for(j=0; j<p->nLookahead; j++){ for(j=0; j<p->nLookahead; j++){
k = p->aLookahead[j].lookahead - p->mnLookahead + i; k = p->aLookahead[j].lookahead - p->mnLookahead + i;
@ -3612,7 +3619,7 @@ int mhflag; /* Output in makeheaders format if true */
struct action *ap; struct action *ap;
struct rule *rp; struct rule *rp;
struct acttab *pActtab; struct acttab *pActtab;
int i, j, n; int i, j, k, n;
char *name; char *name;
int mnTknOfst, mxTknOfst; int mnTknOfst, mxTknOfst;
int mnNtOfst, mxNtOfst; int mnNtOfst, mxNtOfst;
@ -3771,8 +3778,9 @@ int mhflag; /* Output in makeheaders format if true */
free(ax); free(ax);
/* Output the yy_action table */ /* Output the yy_action table */
fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
n = acttab_size(pActtab); n = acttab_size(pActtab);
fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
for(i=j=0; i<n; i++){ for(i=j=0; i<n; i++){
int action = acttab_yyaction(pActtab, i); int action = acttab_yyaction(pActtab, i);
if( action<0 ) action = lemp->nstate + lemp->nrule + 2; if( action<0 ) action = lemp->nstate + lemp->nrule + 2;
@ -3807,7 +3815,9 @@ int mhflag; /* Output in makeheaders format if true */
fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++; fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++;
n = lemp->nstate; n = lemp->nstate;
while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--; while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
fprintf(out, "#define YY_SHIFT_MAX %d\n", n-1); lineno++; fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++;
fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++;
fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++;
fprintf(out, "static const %s yy_shift_ofst[] = {\n", fprintf(out, "static const %s yy_shift_ofst[] = {\n",
minimum_size_type(mnTknOfst-1, mxTknOfst)); lineno++; minimum_size_type(mnTknOfst-1, mxTknOfst)); lineno++;
for(i=j=0; i<n; i++){ for(i=j=0; i<n; i++){
@ -3830,7 +3840,9 @@ int mhflag; /* Output in makeheaders format if true */
fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++; fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
n = lemp->nstate; n = lemp->nstate;
while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--; while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
fprintf(out, "#define YY_REDUCE_MAX %d\n", n-1); lineno++; fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++;
fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++;
fprintf(out, "static const %s yy_reduce_ofst[] = {\n", fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
minimum_size_type(mnNtOfst-1, mxNtOfst)); lineno++; minimum_size_type(mnNtOfst-1, mxNtOfst)); lineno++;
for(i=j=0; i<n; i++){ for(i=j=0; i<n; i++){

View file

@ -134,7 +134,6 @@ static const YYMINORTYPE yyzerominor = { 0 };
** yy_default[] Default action for each state. ** yy_default[] Default action for each state.
*/ */
%% %%
#define YY_SZ_ACTTAB (int)(sizeof(yy_action)/sizeof(yy_action[0]))
/* The next table maps tokens into fallback tokens. If a construct /* The next table maps tokens into fallback tokens. If a construct
** like the following: ** like the following:
@ -394,12 +393,13 @@ static int yy_find_shift_action(
int i; int i;
int stateno = pParser->yystack[pParser->yyidx].stateno; int stateno = pParser->yystack[pParser->yyidx].stateno;
if( stateno>YY_SHIFT_MAX || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){ if( stateno>YY_SHIFT_COUNT
|| (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
return yy_default[stateno]; return yy_default[stateno];
} }
assert( iLookAhead!=YYNOCODE ); assert( iLookAhead!=YYNOCODE );
i += iLookAhead; i += iLookAhead;
if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){ if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
if( iLookAhead>0 ){ if( iLookAhead>0 ){
#ifdef YYFALLBACK #ifdef YYFALLBACK
YYCODETYPE iFallback; /* Fallback token */ YYCODETYPE iFallback; /* Fallback token */
@ -417,7 +417,15 @@ static int yy_find_shift_action(
#ifdef YYWILDCARD #ifdef YYWILDCARD
{ {
int j = i - iLookAhead + YYWILDCARD; int j = i - iLookAhead + YYWILDCARD;
if( j>=0 && j<YY_SZ_ACTTAB && yy_lookahead[j]==YYWILDCARD ){ if(
#if YY_SHIFT_MIN+YYWILDCARD<0
j>=0 &&
#endif
#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
j<YY_ACTTAB_COUNT &&
#endif
yy_lookahead[j]==YYWILDCARD
){
#ifndef NDEBUG #ifndef NDEBUG
if( yyTraceFILE ){ if( yyTraceFILE ){
fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
@ -449,22 +457,22 @@ static int yy_find_reduce_action(
){ ){
int i; int i;
#ifdef YYERRORSYMBOL #ifdef YYERRORSYMBOL
if( stateno>YY_REDUCE_MAX ){ if( stateno>YY_REDUCE_COUNT ){
return yy_default[stateno]; return yy_default[stateno];
} }
#else #else
assert( stateno<=YY_REDUCE_MAX ); assert( stateno<=YY_REDUCE_COUNT );
#endif #endif
i = yy_reduce_ofst[stateno]; i = yy_reduce_ofst[stateno];
assert( i!=YY_REDUCE_USE_DFLT ); assert( i!=YY_REDUCE_USE_DFLT );
assert( iLookAhead!=YYNOCODE ); assert( iLookAhead!=YYNOCODE );
i += iLookAhead; i += iLookAhead;
#ifdef YYERRORSYMBOL #ifdef YYERRORSYMBOL
if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){ if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
return yy_default[stateno]; return yy_default[stateno];
} }
#else #else
assert( i>=0 && i<YY_SZ_ACTTAB ); assert( i>=0 && i<YY_ACTTAB_COUNT );
assert( yy_lookahead[i]==iLookAhead ); assert( yy_lookahead[i]==iLookAhead );
#endif #endif
return yy_action[i]; return yy_action[i];