Lemon update 2015-09-07 14:22:24 on branch trunk

- In the "parse.out" output file from Lemon, show addition the complete text of rules on reduce actions. (user: drh)
This commit is contained in:
Randy Heit 2016-03-20 10:24:15 -05:00
parent 00de97aca2
commit 2b5cef0c17

View file

@ -244,7 +244,7 @@ struct state {
struct action *ap; /* Array of actions for this state */ struct action *ap; /* Array of actions for this state */
int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */ int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */
int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */ int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */
int iDflt; /* Default action */ int iDflt; /* Default action is reduce by this rule */
}; };
#define NO_OFFSET (-2147483647) #define NO_OFFSET (-2147483647)
@ -2884,15 +2884,14 @@ void Reprint(struct lemon *lemp)
} }
} }
void ConfigPrint(FILE *fp, struct config *cfp) /* Print a single rule.
{ */
struct rule *rp; void RulePrint(FILE *fp, struct rule *rp, int iCursor){
struct symbol *sp; struct symbol *sp;
int i, j; int i, j;
rp = cfp->rp;
fprintf(fp,"%s ::=",rp->lhs->name); fprintf(fp,"%s ::=",rp->lhs->name);
for(i=0; i<=rp->nrhs; i++){ for(i=0; i<=rp->nrhs; i++){
if( i==cfp->dot ) fprintf(fp," *"); if( i==iCursor ) fprintf(fp," *");
if( i==rp->nrhs ) break; if( i==rp->nrhs ) break;
sp = rp->rhs[i]; sp = rp->rhs[i];
if( sp->type==MULTITERMINAL ){ if( sp->type==MULTITERMINAL ){
@ -2906,6 +2905,12 @@ void ConfigPrint(FILE *fp, struct config *cfp)
} }
} }
/* Print the rule for a configuration.
*/
void ConfigPrint(FILE *fp, struct config *cfp){
RulePrint(fp, cfp->rp, cfp->dot);
}
/* #define TEST */ /* #define TEST */
#if 0 #if 0
/* Print a set */ /* Print a set */
@ -2945,15 +2950,29 @@ char *tag;
/* Print an action to the given file descriptor. Return FALSE if /* Print an action to the given file descriptor. Return FALSE if
** nothing was actually printed. ** nothing was actually printed.
*/ */
int PrintAction(struct action *ap, FILE *fp, int indent){ int PrintAction(
struct action *ap, /* The action to print */
FILE *fp, /* Print the action here */
int indent, /* Indent by this amount */
struct rule **apRule /* All rules by index */
){
int result = 1; int result = 1;
switch( ap->type ){ switch( ap->type ){
case SHIFT: case SHIFT: {
fprintf(fp,"%*s shift %d",indent,ap->sp->name,ap->x.stp->statenum); struct state *stp = ap->x.stp;
fprintf(fp,"%*s shift %-7d",indent,ap->sp->name,stp->statenum);
if( stp->nTknAct==0 && stp->nNtAct==0 && apRule ){
fprintf(fp,"then reduce %d: ", stp->iDflt);
RulePrint(fp, apRule[stp->iDflt], -1);
}
break; break;
case REDUCE: }
fprintf(fp,"%*s reduce %d",indent,ap->sp->name,ap->x.rp->index); case REDUCE: {
struct rule *rp = ap->x.rp;
fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->index);
if( apRule ) RulePrint(fp, apRule[rp->index], -1);
break; break;
}
case ACCEPT: case ACCEPT:
fprintf(fp,"%*s accept",indent,ap->sp->name); fprintf(fp,"%*s accept",indent,ap->sp->name);
break; break;
@ -2962,16 +2981,16 @@ int PrintAction(struct action *ap, FILE *fp, int indent){
break; break;
case SRCONFLICT: case SRCONFLICT:
case RRCONFLICT: case RRCONFLICT:
fprintf(fp,"%*s reduce %-3d ** Parsing conflict **", fprintf(fp,"%*s reduce %-7d ** Parsing conflict **",
indent,ap->sp->name,ap->x.rp->index); indent,ap->sp->name,ap->x.rp->index);
break; break;
case SSCONFLICT: case SSCONFLICT:
fprintf(fp,"%*s shift %-3d ** Parsing conflict **", fprintf(fp,"%*s shift %-7d ** Parsing conflict **",
indent,ap->sp->name,ap->x.stp->statenum); indent,ap->sp->name,ap->x.stp->statenum);
break; break;
case SH_RESOLVED: case SH_RESOLVED:
if( showPrecedenceConflict ){ if( showPrecedenceConflict ){
fprintf(fp,"%*s shift %-3d -- dropped by precedence", fprintf(fp,"%*s shift %-7d -- dropped by precedence",
indent,ap->sp->name,ap->x.stp->statenum); indent,ap->sp->name,ap->x.stp->statenum);
}else{ }else{
result = 0; result = 0;
@ -2979,7 +2998,7 @@ int PrintAction(struct action *ap, FILE *fp, int indent){
break; break;
case RD_RESOLVED: case RD_RESOLVED:
if( showPrecedenceConflict ){ if( showPrecedenceConflict ){
fprintf(fp,"%*s reduce %-3d -- dropped by precedence", fprintf(fp,"%*s reduce %-7d -- dropped by precedence",
indent,ap->sp->name,ap->x.rp->index); indent,ap->sp->name,ap->x.rp->index);
}else{ }else{
result = 0; result = 0;
@ -3000,7 +3019,17 @@ void ReportOutput(struct lemon *lemp)
struct config *cfp; struct config *cfp;
struct action *ap; struct action *ap;
FILE *fp; FILE *fp;
struct rule **apRule;
apRule = malloc( sizeof(apRule[0])*(lemp->nrule+1) );
if( apRule ){
struct rule *x;
memset(apRule, 0, sizeof(apRule[0])*(lemp->nrule+1) );
for(x=lemp->rule; x; x=x->next){
assert( x->index>=0 && x->index<(lemp->nrule+1) );
apRule[x->index] = x;
}
}
fp = file_open(lemp,".out","wb"); fp = file_open(lemp,".out","wb");
if( fp==0 ) return; if( fp==0 ) return;
for(i=0; i<lemp->nstate; i++){ for(i=0; i<lemp->nstate; i++){
@ -3028,7 +3057,7 @@ void ReportOutput(struct lemon *lemp)
} }
fprintf(fp,"\n"); fprintf(fp,"\n");
for(ap=stp->ap; ap; ap=ap->next){ for(ap=stp->ap; ap; ap=ap->next){
if( PrintAction(ap,fp,30) ) fprintf(fp,"\n"); if( PrintAction(ap,fp,30,apRule) ) fprintf(fp,"\n");
} }
fprintf(fp,"\n"); fprintf(fp,"\n");
} }
@ -3054,6 +3083,7 @@ void ReportOutput(struct lemon *lemp)
fprintf(fp, "\n"); fprintf(fp, "\n");
} }
fclose(fp); fclose(fp);
free(apRule);
return; return;
} }
@ -3956,7 +3986,7 @@ void ReportTable(
for(i=j=0; i<n; i++){ for(i=j=0; i<n; i++){
stp = lemp->sorted[i]; stp = lemp->sorted[i];
if( j==0 ) fprintf(out," /* %5d */ ", i); if( j==0 ) fprintf(out," /* %5d */ ", i);
fprintf(out, " %4d,", stp->iDflt); fprintf(out, " %4d,", stp->iDflt+n);
if( j==9 || i==n-1 ){ if( j==9 || i==n-1 ){
fprintf(out, "\n"); lineno++; fprintf(out, "\n"); lineno++;
j = 0; j = 0;
@ -4279,7 +4309,7 @@ void ResortStates(struct lemon *lemp)
for(i=0; i<lemp->nstate; i++){ for(i=0; i<lemp->nstate; i++){
stp = lemp->sorted[i]; stp = lemp->sorted[i];
stp->nTknAct = stp->nNtAct = 0; stp->nTknAct = stp->nNtAct = 0;
stp->iDflt = lemp->nstate + lemp->nrule; stp->iDflt = lemp->nrule;
stp->iTknOfst = NO_OFFSET; stp->iTknOfst = NO_OFFSET;
stp->iNtOfst = NO_OFFSET; stp->iNtOfst = NO_OFFSET;
for(ap=stp->ap; ap; ap=ap->next){ for(ap=stp->ap; ap; ap=ap->next){
@ -4289,7 +4319,7 @@ void ResortStates(struct lemon *lemp)
}else if( ap->sp->index<lemp->nsymbol ){ }else if( ap->sp->index<lemp->nsymbol ){
stp->nNtAct++; stp->nNtAct++;
}else{ }else{
stp->iDflt = compute_action(lemp, ap); stp->iDflt = compute_action(lemp, ap) - lemp->nstate;
} }
} }
} }