From e3e4511ec128ae26130a3efa8ef8bb1ea9cec7f6 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Tue, 1 Oct 2019 21:56:51 -0400 Subject: [PATCH] Squashed commit of the following: commit 897f5bec3c60201f7156b3f7781db1748a619a8b Author: Kelvin Sherlock Date: Tue Oct 1 21:56:04 2019 -0400 tweak makefile commit 77ed2da61e80a96ce0186f81d3906d6eb21fcb05 Author: Kelvin Sherlock Date: Tue Oct 1 21:31:00 2019 -0400 sync with lemonpar.c commit 1b386f7435e61c90480fc83183883e5fb638d2a8 Author: Kelvin Sherlock Date: Tue Oct 1 19:53:50 2019 -0400 upstream sync - Improve Lemon so that it enlarges some of its tables slightly in order to avoid having to index range checks on table lookups for a performance increase. - etc commit b4305b6dbe3792ef0bd21d663ccc8878c87d8d5b Author: Kelvin Sherlock Date: Mon Jun 3 18:05:10 2019 -0400 Fix harmless compiler warning; Fix a harmless memory leak. --- lemon.c | 102 ++++++++++++++++++++++++++++++-------------------- lempar.c | 29 ++++++-------- lempar.cpp | 31 +++++++-------- lempar.cxx | 31 +++++++-------- test/Makefile | 3 ++ 5 files changed, 102 insertions(+), 94 deletions(-) diff --git a/lemon.c b/lemon.c index ccc6624..c19e539 100644 --- a/lemon.c +++ b/lemon.c @@ -56,6 +56,7 @@ extern int access(const char *path, int mode); #define MAXRHS 1000 #endif +extern void memory_error(); static int showPrecedenceConflict = 0; static char *msort(char*,char**,int(*)(const char*,const char*)); @@ -492,22 +493,22 @@ void Configtable_clear(int(*)(struct config *)); /* Allocate a new parser action */ static struct action *Action_new(void){ - static struct action *freelist = 0; + static struct action *actionfreelist = 0; struct action *newaction; - if( freelist==0 ){ + if( actionfreelist==0 ){ int i; int amt = 100; - freelist = (struct action *)calloc(amt, sizeof(struct action)); - if( freelist==0 ){ + actionfreelist = (struct action *)calloc(amt, sizeof(struct action)); + if( actionfreelist==0 ){ fprintf(stderr,"Unable to allocate memory for a new parser action."); exit(1); } - for(i=0; inext; + newaction = actionfreelist; + actionfreelist = actionfreelist->next; return newaction; } @@ -1940,7 +1941,7 @@ static char *msort( return ep; } /************************ From the file "option.c" **************************/ -static char **argv; +static char **g_argv; static struct s_options *op; static FILE *errstream; @@ -1953,14 +1954,14 @@ static FILE *errstream; static void errline(int n, int k, FILE *err) { int spcnt, i; - if( argv[0] ) fprintf(err,"%s",argv[0]); - spcnt = lemonStrlen(argv[0]) + 1; - for(i=1; i=0 ? argv[i] : 0; + return i>=0 ? g_argv[i] : 0; } void OptErr(int n) @@ -2784,7 +2785,7 @@ to follow the previous rule."); case WAITING_FOR_CLASS_ID: if( !ISLOWER(x[0]) ){ ErrorMsg(psp->filename, psp->tokenlineno, - "%%token_class must be followed by an identifier: ", x); + "%%token_class must be followed by an identifier: %s", x); psp->errorcnt++; psp->state = RESYNC_AFTER_DECL_ERROR; }else if( Symbol_find(x) ){ @@ -3960,7 +3961,7 @@ PRIVATE int translate_code(struct lemon *lemp, struct rule *rp){ ErrorMsg(lemp->filename,rp->ruleline, "%s(%s) has the same label as the LHS but is not the left-most " "symbol on the RHS.", - rp->rhs[i]->name, rp->rhsalias); + rp->rhs[i]->name, rp->rhsalias[i]); lemp->errorcnt++; } for(j=0; jnsymbol; if( j==0 ) fprintf(out," /* %5d */ ", i); fprintf(out, " %4d,", la); - if( j==9 || i==n-1 ){ + if( j==9 ){ + fprintf(out, "\n"); lineno++; + j = 0; + }else{ + j++; + } + } + /* Add extra entries to the end of the yy_lookahead[] table so that + ** yy_shift_ofst[]+iToken will always be a valid index into the array, + ** even for the largest possible value of yy_shift_ofst[] and iToken. */ + nLookAhead = lemp->nterminal + lemp->nactiontab; + while( interminal); + if( j==9 ){ fprintf(out, "\n"); lineno++; j = 0; }else{ j++; } + i++; } + if( j>0 ){ fprintf(out, "\n"); lineno++; } fprintf(out, "};\n"); lineno++; /* Output the yy_shift_ofst[] table */ @@ -4747,7 +4765,9 @@ void ReportTable( */ if( lemp->has_fallback ){ int mx = lemp->nterminal - 1; - while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; } + /* 2019-08-28: Generate fallback entries for every token to avoid + ** having to do a range check on the index */ + /* while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; } */ lemp->tablesize += (mx+1)*szCodeType; for(i=0; i<=mx; i++){ struct symbol *p = lemp->symbols[i]; @@ -5069,6 +5089,7 @@ void ReportTable( /* Append any addition code the user desires */ tplt_print(out,lemp,lemp->extracode,&lineno); + acttab_free(pActtab); fclose(in); fclose(out); return; @@ -5338,7 +5359,6 @@ char *SetNew(void){ char *s; s = (char*)calloc( size, 1); if( s==0 ){ - extern void memory_error(); memory_error(); } return s; diff --git a/lempar.c b/lempar.c index 197fd77..a37f660 100644 --- a/lempar.c +++ b/lempar.c @@ -521,15 +521,18 @@ static YYACTIONTYPE yy_find_shift_action( do{ i = stateno <= YY_SHIFT_COUNT ? yy_shift_ofst[stateno] : stateno; assert( i>=0 ); - /* assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); */ + assert( i<=YY_ACTTAB_COUNT ); + assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); assert( iLookAhead!=YYNOCODE ); assert( iLookAhead < YYNTOKEN ); i += iLookAhead; - if( i>=YY_NLOOKAHEAD || yy_lookahead[i]!=iLookAhead ){ + assert( i<(int)YY_NLOOKAHEAD ); + if( yy_lookahead[i]!=iLookAhead ){ #ifdef YYFALLBACK YYCODETYPE iFallback; /* Fallback token */ - if( iLookAhead %s\n", @@ -544,16 +547,8 @@ static YYACTIONTYPE yy_find_shift_action( #ifdef YYWILDCARD { int j = i - iLookAhead + YYWILDCARD; - if( -#if YY_SHIFT_MIN+YYWILDCARD<0 - j>=0 && -#endif -#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT - j0 - ){ + assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) ); + if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){ #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", @@ -567,6 +562,7 @@ static YYACTIONTYPE yy_find_shift_action( #endif /* YYWILDCARD */ return yy_default[stateno]; }else{ + assert( i>=0 && i=0 ); - /* assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); */ + assert( i<=YY_ACTTAB_COUNT ); + assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); assert( iLookAhead!=YYNOCODE ); assert( iLookAhead < YYNTOKEN ); i += iLookAhead; - if( i>=YY_NLOOKAHEAD || yy_lookahead[i]!=iLookAhead ){ + assert( i<(int)YY_NLOOKAHEAD ); + if( yy_lookahead[i]!=iLookAhead ){ #ifdef YYFALLBACK YYCODETYPE iFallback; /* Fallback token */ - if( iLookAhead %s\n", @@ -682,16 +685,8 @@ static YYACTIONTYPE yy_find_shift_action( #ifdef YYWILDCARD { int j = i - iLookAhead + YYWILDCARD; - if( -#if YY_SHIFT_MIN+YYWILDCARD<0 - j>=0 && -#endif -#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT - j0 - ){ + assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) ); + if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){ #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", @@ -705,6 +700,7 @@ static YYACTIONTYPE yy_find_shift_action( #endif /* YYWILDCARD */ return yy_default[stateno]; }else{ + assert( i>=0 && iyytos++; @@ -1250,9 +1246,8 @@ void Parse( */ int ParseFallback(int iToken){ #ifdef YYFALLBACK - if( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) ){ - return yyFallback[iToken]; - } + assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) ); + return yyFallback[iToken]; #else (void)iToken; #endif diff --git a/lempar.cxx b/lempar.cxx index 72d7538..5f7324d 100644 --- a/lempar.cxx +++ b/lempar.cxx @@ -669,15 +669,18 @@ YYACTIONTYPE yypParser::yy_find_shift_action( do{ i = stateno <= YY_SHIFT_COUNT ? yy_shift_ofst[stateno] : stateno; assert( i>=0 ); - /* assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); */ + assert( i<=YY_ACTTAB_COUNT ); + assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); assert( iLookAhead!=YYNOCODE ); assert( iLookAhead < YYNTOKEN ); i += iLookAhead; - if( i>=YY_NLOOKAHEAD || yy_lookahead[i]!=iLookAhead ){ + assert( i<(int)YY_NLOOKAHEAD ); + if( yy_lookahead[i]!=iLookAhead ){ #ifdef YYFALLBACK YYCODETYPE iFallback; /* Fallback token */ - if( iLookAhead %s\n", @@ -692,16 +695,8 @@ YYACTIONTYPE yypParser::yy_find_shift_action( #ifdef YYWILDCARD { int j = i - iLookAhead + YYWILDCARD; - if( -#if YY_SHIFT_MIN+YYWILDCARD<0 - j>=0 && -#endif -#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT - j0 - ){ + assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) ); + if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){ #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", @@ -715,6 +710,7 @@ YYACTIONTYPE yypParser::yy_find_shift_action( #endif /* YYWILDCARD */ return yy_default[stateno]; }else{ + assert( i>=0 && i