Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 897f5bec3c60201f7156b3f7781db1748a619a8b
Author: Kelvin Sherlock <[email protected]>
Date:   Tue Oct 1 21:56:04 2019 -0400

    tweak makefile

commit 77ed2da61e80a96ce0186f81d3906d6eb21fcb05
Author: Kelvin Sherlock <[email protected]>
Date:   Tue Oct 1 21:31:00 2019 -0400

    sync with lemonpar.c

commit 1b386f7435e61c90480fc83183883e5fb638d2a8
Author: Kelvin Sherlock <[email protected]>
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 <[email protected]>
Date:   Mon Jun 3 18:05:10 2019 -0400

    Fix harmless compiler warning; Fix a harmless memory leak.
  • Loading branch information
ksherlock committed Oct 2, 2019
1 parent 620f5c4 commit e3e4511
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 94 deletions.
102 changes: 61 additions & 41 deletions lemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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*));

Expand Down Expand Up @@ -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; i<amt-1; i++) freelist[i].next = &freelist[i+1];
freelist[amt-1].next = 0;
for(i=0; i<amt-1; i++) actionfreelist[i].next = &actionfreelist[i+1];
actionfreelist[amt-1].next = 0;
}
newaction = freelist;
freelist = freelist->next;
newaction = actionfreelist;
actionfreelist = actionfreelist->next;
return newaction;
}

Expand Down Expand Up @@ -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;

Expand All @@ -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<n && argv[i]; i++){
fprintf(err," %s",argv[i]);
spcnt += lemonStrlen(argv[i])+1;
if( g_argv[0] ) fprintf(err,"%s",g_argv[0]);
spcnt = lemonStrlen(g_argv[0]) + 1;
for(i=1; i<n && g_argv[i]; i++){
fprintf(err," %s",g_argv[i]);
spcnt += lemonStrlen(g_argv[i])+1;
}
spcnt += k;
for(; argv[i]; i++) fprintf(err," %s",argv[i]);
for(; g_argv[i]; i++) fprintf(err," %s",g_argv[i]);
if( spcnt<20 ){
fprintf(err,"\n%*s^-- here\n",spcnt,"");
}else{
Expand All @@ -1976,13 +1977,13 @@ static int argindex(int n)
{
int i;
int dashdash = 0;
if( argv!=0 && *argv!=0 ){
for(i=1; argv[i]; i++){
if( dashdash || !ISOPT(argv[i]) ){
if( g_argv!=0 && *g_argv!=0 ){
for(i=1; g_argv[i]; i++){
if( dashdash || !ISOPT(g_argv[i]) ){
if( n==0 ) return i;
n--;
}
if( strcmp(argv[i],"--")==0 ) dashdash = 1;
if( strcmp(g_argv[i],"--")==0 ) dashdash = 1;
}
}
return -1;
Expand All @@ -1999,9 +2000,9 @@ static int handleflags(int i, FILE *err)
int errcnt = 0;
int j;
for(j=0; op[j].label; j++){
if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
if( strncmp(&g_argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
}
v = argv[i][0]=='-' ? 1 : 0;
v = g_argv[i][0]=='-' ? 1 : 0;
if( op[j].label==0 ){
if( err ){
fprintf(err,"%sundefined option.\n",emsg);
Expand All @@ -2015,7 +2016,7 @@ static int handleflags(int i, FILE *err)
}else if( op[j].type==OPT_FFLAG ){
(*(void(*)(int))(op[j].arg))(v);
}else if( op[j].type==OPT_FSTR ){
(*(void(*)(char *))(op[j].arg))(&argv[i][2]);
(*(void(*)(char *))(op[j].arg))(&g_argv[i][2]);
}else{
if( err ){
fprintf(err,"%smissing argument on switch.\n",emsg);
Expand All @@ -2037,11 +2038,11 @@ static int handleswitch(int i, FILE *err)
char *cp;
int j;
int errcnt = 0;
cp = strchr(argv[i],'=');
cp = strchr(g_argv[i],'=');
assert( cp!=0 );
*cp = 0;
for(j=0; op[j].label; j++){
if( strcmp(argv[i],op[j].label)==0 ) break;
if( strcmp(g_argv[i],op[j].label)==0 ) break;
}
*cp = '=';
if( op[j].label==0 ){
Expand All @@ -2068,7 +2069,7 @@ static int handleswitch(int i, FILE *err)
if( err ){
fprintf(err,
"%sillegal character in floating-point argument.\n",emsg);
errline(i,(int)((char*)end-(char*)argv[i]),err);
errline(i,(int)((char*)end-(char*)g_argv[i]),err);
}
errcnt++;
}
Expand All @@ -2079,7 +2080,7 @@ static int handleswitch(int i, FILE *err)
if( *end ){
if( err ){
fprintf(err,"%sillegal character in integer argument.\n",emsg);
errline(i,(int)((char*)end-(char*)argv[i]),err);
errline(i,(int)((char*)end-(char*)g_argv[i]),err);
}
errcnt++;
}
Expand Down Expand Up @@ -2119,15 +2120,15 @@ static int handleswitch(int i, FILE *err)
int OptInit(char **a, struct s_options *o, FILE *err)
{
int errcnt = 0;
argv = a;
g_argv = a;
op = o;
errstream = err;
if( argv && *argv && op ){
if( g_argv && *g_argv && op ){
int i;
for(i=1; argv[i]; i++){
if( argv[i][0]=='+' || argv[i][0]=='-' ){
for(i=1; g_argv[i]; i++){
if( g_argv[i][0]=='+' || g_argv[i][0]=='-' ){
errcnt += handleflags(i,err);
}else if( strchr(argv[i],'=') ){
}else if( strchr(g_argv[i],'=') ){
errcnt += handleswitch(i,err);
}
}
Expand All @@ -2144,10 +2145,10 @@ int OptNArgs(void){
int cnt = 0;
int dashdash = 0;
int i;
if( argv!=0 && argv[0]!=0 ){
for(i=1; argv[i]; i++){
if( dashdash || !ISOPT(argv[i]) ) cnt++;
if( strcmp(argv[i],"--")==0 ) dashdash = 1;
if( g_argv!=0 && g_argv[0]!=0 ){
for(i=1; g_argv[i]; i++){
if( dashdash || !ISOPT(g_argv[i]) ) cnt++;
if( strcmp(g_argv[i],"--")==0 ) dashdash = 1;
}
}
return cnt;
Expand All @@ -2157,7 +2158,7 @@ char *OptArg(int n)
{
int i;
i = argindex(n);
return i>=0 ? argv[i] : 0;
return i>=0 ? g_argv[i] : 0;
}

void OptErr(int n)
Expand Down Expand Up @@ -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) ){
Expand Down Expand Up @@ -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; j<i; j++){
Expand Down Expand Up @@ -4411,6 +4412,7 @@ void ReportTable(
struct rule *rp;
struct acttab *pActtab;
int i, j, n, sz;
int nLookAhead;
int szActionType; /* sizeof(YYACTIONTYPE) */
int szCodeType; /* sizeof(YYCODETYPE) */
const char *name;
Expand Down Expand Up @@ -4661,13 +4663,29 @@ void ReportTable(
if( la<0 ) la = lemp->nsymbol;
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( i<nLookAhead ){
if( j==0 ) fprintf(out," /* %5d */ ", i);
fprintf(out, " %4d,", lemp->nterminal);
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 */
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 12 additions & 17 deletions lempar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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<sizeof(yyFallback)/sizeof(yyFallback[0])
&& (iFallback = yyFallback[iLookAhead])!=0 ){
assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) );
iFallback = yyFallback[iLookAhead];
if( iFallback!=0 ){
#ifndef NDEBUG
if( yyTraceFILE ){
fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
Expand All @@ -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
j<YY_ACTTAB_COUNT &&
#endif
j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) &&
yy_lookahead[j]==YYWILDCARD && iLookAhead>0
){
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",
Expand All @@ -567,6 +562,7 @@ static YYACTIONTYPE yy_find_shift_action(
#endif /* YYWILDCARD */
return yy_default[stateno];
}else{
assert( i>=0 && i<sizeof(yy_action)/sizeof(yy_action[0]) );
return yy_action[i];
}
}while(1);
Expand Down Expand Up @@ -1068,9 +1064,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
Expand Down
Loading

0 comments on commit e3e4511

Please sign in to comment.