/ Check-in [5283e0d1]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix some issues with lemon. Tickets #2852 and #2835. (CVS 4641)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5283e0d1467e0fc0ebbd1be553b5a8762e9c7975
User & Date: drh 2007-12-21 00:02:11
Context
2007-12-21
04:47
Fix for #2854. "BEGIN EXCLUSIVE" excludes other shared cache users from using the database. (CVS 4642) check-in: 2e59b1d0 user: danielk1977 tags: trunk
00:02
Fix some issues with lemon. Tickets #2852 and #2835. (CVS 4641) check-in: 5283e0d1 user: drh tags: trunk
2007-12-19
09:20
Add the ioerr3.test file, that found an error in br3317. The error is not present in this branch. (CVS 4640) check-in: d3efec14 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to tool/lemon.c.

   187    187   struct action {
   188    188     struct symbol *sp;       /* The look-ahead symbol */
   189    189     enum e_action {
   190    190       SHIFT,
   191    191       ACCEPT,
   192    192       REDUCE,
   193    193       ERROR,
   194         -    CONFLICT,                /* Was a reduce, but part of a conflict */
          194  +    SSCONFLICT,              /* A shift/shift conflict */
          195  +    SRCONFLICT,              /* Was a reduce, but part of a conflict */
          196  +    RRCONFLICT,              /* Was a reduce, but part of a conflict */
   195    197       SH_RESOLVED,             /* Was a shift.  Precedence resolved conflict */
   196    198       RD_RESOLVED,             /* Was reduce.  Precedence resolved conflict */
   197    199       NOT_USED                 /* Deleted by compression */
   198    200     } type;
   199    201     union {
   200    202       struct state *stp;     /* The new state, if a shift */
   201    203       struct rule *rp;       /* The rule, if a reduce */
................................................................................
   333    335   static struct action *Action_new(void){
   334    336     static struct action *freelist = 0;
   335    337     struct action *new;
   336    338   
   337    339     if( freelist==0 ){
   338    340       int i;
   339    341       int amt = 100;
   340         -    freelist = (struct action *)malloc( sizeof(struct action)*amt );
          342  +    freelist = (struct action *)calloc(amt, sizeof(struct action));
   341    343       if( freelist==0 ){
   342    344         fprintf(stderr,"Unable to allocate memory for a new parser action.");
   343    345         exit(1);
   344    346       }
   345    347       for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
   346    348       freelist[amt-1].next = 0;
   347    349     }
................................................................................
   356    358   */
   357    359   static int actioncmp(
   358    360     struct action *ap1,
   359    361     struct action *ap2
   360    362   ){
   361    363     int rc;
   362    364     rc = ap1->sp->index - ap2->sp->index;
   363         -  if( rc==0 ) rc = (int)ap1->type - (int)ap2->type;
   364    365     if( rc==0 ){
          366  +    rc = (int)ap1->type - (int)ap2->type;
          367  +  }
          368  +  if( rc==0 && ap1->type==REDUCE ){
   365    369       rc = ap1->x.rp->index - ap2->x.rp->index;
   366    370     }
   367    371     return rc;
   368    372   }
   369    373   
   370    374   /* Sort parser actions */
   371    375   static struct action *Action_sort(
................................................................................
   433    437     free( p->aAction );
   434    438     free( p->aLookahead );
   435    439     free( p );
   436    440   }
   437    441   
   438    442   /* Allocate a new acttab structure */
   439    443   acttab *acttab_alloc(void){
   440         -  acttab *p = malloc( sizeof(*p) );
          444  +  acttab *p = calloc( 1, sizeof(*p) );
   441    445     if( p==0 ){
   442    446       fprintf(stderr,"Unable to allocate memory for a new acttab.");
   443    447       exit(1);
   444    448     }
   445    449     memset(p, 0, sizeof(*p));
   446    450     return p;
   447    451   }
................................................................................
  1006   1010   struct action *apy;
  1007   1011   struct symbol *errsym;   /* The error symbol (if defined.  NULL otherwise) */
  1008   1012   {
  1009   1013     struct symbol *spx, *spy;
  1010   1014     int errcnt = 0;
  1011   1015     assert( apx->sp==apy->sp );  /* Otherwise there would be no conflict */
  1012   1016     if( apx->type==SHIFT && apy->type==SHIFT ){
  1013         -    apy->type = CONFLICT;
         1017  +    apy->type = SSCONFLICT;
  1014   1018       errcnt++;
  1015   1019     }
  1016   1020     if( apx->type==SHIFT && apy->type==REDUCE ){
  1017   1021       spx = apx->sp;
  1018   1022       spy = apy->x.rp->precsym;
  1019   1023       if( spy==0 || spx->prec<0 || spy->prec<0 ){
  1020   1024         /* Not enough precedence information. */
  1021         -      apy->type = CONFLICT;
         1025  +      apy->type = SRCONFLICT;
  1022   1026         errcnt++;
  1023   1027       }else if( spx->prec>spy->prec ){    /* Lower precedence wins */
  1024   1028         apy->type = RD_RESOLVED;
  1025   1029       }else if( spx->prec<spy->prec ){
  1026   1030         apx->type = SH_RESOLVED;
  1027   1031       }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
  1028   1032         apy->type = RD_RESOLVED;                             /* associativity */
  1029   1033       }else if( spx->prec==spy->prec && spx->assoc==LEFT ){  /* to break tie */
  1030   1034         apx->type = SH_RESOLVED;
  1031   1035       }else{
  1032   1036         assert( spx->prec==spy->prec && spx->assoc==NONE );
  1033         -      apy->type = CONFLICT;
         1037  +      apy->type = SRCONFLICT;
  1034   1038         errcnt++;
  1035   1039       }
  1036   1040     }else if( apx->type==REDUCE && apy->type==REDUCE ){
  1037   1041       spx = apx->x.rp->precsym;
  1038   1042       spy = apy->x.rp->precsym;
  1039   1043       if( spx==0 || spy==0 || spx->prec<0 ||
  1040   1044       spy->prec<0 || spx->prec==spy->prec ){
  1041         -      apy->type = CONFLICT;
         1045  +      apy->type = RRCONFLICT;
  1042   1046         errcnt++;
  1043   1047       }else if( spx->prec>spy->prec ){
  1044   1048         apy->type = RD_RESOLVED;
  1045   1049       }else if( spx->prec<spy->prec ){
  1046   1050         apx->type = RD_RESOLVED;
  1047   1051       }
  1048   1052     }else{
  1049   1053       assert( 
  1050   1054         apx->type==SH_RESOLVED ||
  1051   1055         apx->type==RD_RESOLVED ||
  1052         -      apx->type==CONFLICT ||
         1056  +      apx->type==SSCONFLICT ||
         1057  +      apx->type==SRCONFLICT ||
         1058  +      apx->type==RRCONFLICT ||
  1053   1059         apy->type==SH_RESOLVED ||
  1054   1060         apy->type==RD_RESOLVED ||
  1055         -      apy->type==CONFLICT
         1061  +      apy->type==SSCONFLICT ||
         1062  +      apy->type==SRCONFLICT ||
         1063  +      apy->type==RRCONFLICT
  1056   1064       );
  1057   1065       /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
  1058   1066       ** REDUCEs on the list.  If we reach this point it must be because
  1059   1067       ** the parser conflict had already been resolved. */
  1060   1068     }
  1061   1069     return errcnt;
  1062   1070   }
................................................................................
  1074   1082   
  1075   1083   /* Return a pointer to a new configuration */
  1076   1084   PRIVATE struct config *newconfig(){
  1077   1085     struct config *new;
  1078   1086     if( freelist==0 ){
  1079   1087       int i;
  1080   1088       int amt = 3;
  1081         -    freelist = (struct config *)malloc( sizeof(struct config)*amt );
         1089  +    freelist = (struct config *)calloc( amt, sizeof(struct config) );
  1082   1090       if( freelist==0 ){
  1083   1091         fprintf(stderr,"Unable to allocate memory for a new configuration.");
  1084   1092         exit(1);
  1085   1093       }
  1086   1094       for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
  1087   1095       freelist[amt-1].next = 0;
  1088   1096     }
................................................................................
  1450   1458     lem.nterminal = i;
  1451   1459   
  1452   1460     /* Generate a reprint of the grammar, if requested on the command line */
  1453   1461     if( rpflag ){
  1454   1462       Reprint(&lem);
  1455   1463     }else{
  1456   1464       /* Initialize the size for all follow and first sets */
  1457         -    SetSize(lem.nterminal);
         1465  +    SetSize(lem.nterminal+1);
  1458   1466   
  1459   1467       /* Find the precedence for every production rule (that has one) */
  1460   1468       FindRulePrecedences(&lem);
  1461   1469   
  1462   1470       /* Compute the lambda-nonterminals and the first-sets for every
  1463   1471       ** nonterminal */
  1464   1472       FindFirstSets(&lem);
................................................................................
  2087   2095           psp->errorcnt++;
  2088   2096           psp->state = RESYNC_AFTER_RULE_ERROR;
  2089   2097         }
  2090   2098         break;
  2091   2099       case IN_RHS:
  2092   2100         if( x[0]=='.' ){
  2093   2101           struct rule *rp;
  2094         -        rp = (struct rule *)malloc( sizeof(struct rule) + 
  2095         -             sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs );
         2102  +        rp = (struct rule *)calloc( sizeof(struct rule) + 
         2103  +             sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
  2096   2104           if( rp==0 ){
  2097   2105             ErrorMsg(psp->filename,psp->tokenlineno,
  2098   2106               "Can't allocate enough memory for this rule.");
  2099   2107             psp->errorcnt++;
  2100   2108             psp->prevrule = 0;
  2101   2109   	}else{
  2102   2110             int i;
................................................................................
  2137   2145             psp->alias[psp->nrhs] = 0;
  2138   2146             psp->nrhs++;
  2139   2147   	}
  2140   2148         }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
  2141   2149           struct symbol *msp = psp->rhs[psp->nrhs-1];
  2142   2150           if( msp->type!=MULTITERMINAL ){
  2143   2151             struct symbol *origsp = msp;
  2144         -          msp = malloc(sizeof(*msp));
         2152  +          msp = calloc(1,sizeof(*msp));
  2145   2153             memset(msp, 0, sizeof(*msp));
  2146   2154             msp->type = MULTITERMINAL;
  2147   2155             msp->nsubsym = 1;
  2148         -          msp->subsym = malloc(sizeof(struct symbol*));
         2156  +          msp->subsym = calloc(1,sizeof(struct symbol*));
  2149   2157             msp->subsym[0] = origsp;
  2150   2158             msp->name = origsp->name;
  2151   2159             psp->rhs[psp->nrhs-1] = msp;
  2152   2160           }
  2153   2161           msp->nsubsym++;
  2154   2162           msp->subsym = realloc(msp->subsym, sizeof(struct symbol*)*msp->nsubsym);
  2155   2163           msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
................................................................................
  2594   2602   /* Allocate a new plink */
  2595   2603   struct plink *Plink_new(){
  2596   2604     struct plink *new;
  2597   2605   
  2598   2606     if( plink_freelist==0 ){
  2599   2607       int i;
  2600   2608       int amt = 100;
  2601         -    plink_freelist = (struct plink *)malloc( sizeof(struct plink)*amt );
         2609  +    plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
  2602   2610       if( plink_freelist==0 ){
  2603   2611         fprintf(stderr,
  2604   2612         "Unable to allocate memory for a new follow-set propagation link.\n");
  2605   2613         exit(1);
  2606   2614       }
  2607   2615       for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
  2608   2616       plink_freelist[amt-1].next = 0;
................................................................................
  2820   2828         break;
  2821   2829       case ACCEPT:
  2822   2830         fprintf(fp,"%*s accept",indent,ap->sp->name);
  2823   2831         break;
  2824   2832       case ERROR:
  2825   2833         fprintf(fp,"%*s error",indent,ap->sp->name);
  2826   2834         break;
  2827         -    case CONFLICT:
         2835  +    case SRCONFLICT:
         2836  +    case RRCONFLICT:
  2828   2837         fprintf(fp,"%*s reduce %-3d ** Parsing conflict **",
  2829   2838           indent,ap->sp->name,ap->x.rp->index);
  2830   2839         break;
         2840  +    case SSCONFLICT:
         2841  +      fprintf(fp,"%*s shift  %d ** Parsing conflict **", 
         2842  +        indent,ap->sp->name,ap->x.stp->statenum);
         2843  +      break;
  2831   2844       case SH_RESOLVED:
  2832   2845       case RD_RESOLVED:
  2833   2846       case NOT_USED:
  2834   2847         result = 0;
  2835   2848         break;
  2836   2849     }
  2837   2850     return result;
................................................................................
  3332   3345     char *stddt;              /* Standardized name for a datatype */
  3333   3346     int i,j;                  /* Loop counters */
  3334   3347     int hash;                 /* For hashing the name of a type */
  3335   3348     char *name;               /* Name of the parser */
  3336   3349   
  3337   3350     /* Allocate and initialize types[] and allocate stddt[] */
  3338   3351     arraysize = lemp->nsymbol * 2;
  3339         -  types = (char**)malloc( arraysize * sizeof(char*) );
         3352  +  types = (char**)calloc( arraysize, sizeof(char*) );
  3340   3353     for(i=0; i<arraysize; i++) types[i] = 0;
  3341   3354     maxdtlength = 0;
  3342   3355     if( lemp->vartype ){
  3343   3356       maxdtlength = strlen(lemp->vartype);
  3344   3357     }
  3345   3358     for(i=0; i<lemp->nsymbol; i++){
  3346   3359       int len;
................................................................................
  3578   3591     **                     shifting terminals.
  3579   3592     **  yy_reduce_ofst[]   For each state, the offset into yy_action for
  3580   3593     **                     shifting non-terminals after a reduce.
  3581   3594     **  yy_default[]       Default action for each state.
  3582   3595     */
  3583   3596   
  3584   3597     /* Compute the actions on all states and count them up */
  3585         -  ax = malloc( sizeof(ax[0])*lemp->nstate*2 );
         3598  +  ax = calloc(lemp->nstate*2, sizeof(ax[0]));
  3586   3599     if( ax==0 ){
  3587   3600       fprintf(stderr,"malloc failed\n");
  3588   3601       exit(1);
  3589   3602     }
  3590   3603     for(i=0; i<lemp->nstate; i++){
  3591   3604       stp = lemp->sorted[i];
  3592   3605       ax[i*2].stp = stp;
................................................................................
  4050   4063     size = n+1;
  4051   4064   }
  4052   4065   
  4053   4066   /* Allocate a new set */
  4054   4067   char *SetNew(){
  4055   4068     char *s;
  4056   4069     int i;
  4057         -  s = (char*)malloc( size );
         4070  +  s = (char*)calloc( size, 1);
  4058   4071     if( s==0 ){
  4059   4072       extern void memory_error();
  4060   4073       memory_error();
  4061   4074     }
  4062         -  for(i=0; i<size; i++) s[i] = 0;
  4063   4075     return s;
  4064   4076   }
  4065   4077   
  4066   4078   /* Deallocate a set */
  4067   4079   void SetFree(s)
  4068   4080   char *s;
  4069   4081   {
................................................................................
  4073   4085   /* Add a new element to the set.  Return TRUE if the element was added
  4074   4086   ** and FALSE if it was already there. */
  4075   4087   int SetAdd(s,e)
  4076   4088   char *s;
  4077   4089   int e;
  4078   4090   {
  4079   4091     int rv;
         4092  +  assert( e>=0 && e<size );
  4080   4093     rv = s[e];
  4081   4094     s[e] = 1;
  4082   4095     return !rv;
  4083   4096   }
  4084   4097   
  4085   4098   /* Add every element of s2 to s1.  Return TRUE if s1 changes. */
  4086   4099   int SetUnion(s1,s2)
................................................................................
  4262   4275   struct symbol *Symbol_new(x)
  4263   4276   char *x;
  4264   4277   {
  4265   4278     struct symbol *sp;
  4266   4279   
  4267   4280     sp = Symbol_find(x);
  4268   4281     if( sp==0 ){
  4269         -    sp = (struct symbol *)malloc( sizeof(struct symbol) );
         4282  +    sp = (struct symbol *)calloc(1, sizeof(struct symbol) );
  4270   4283       MemoryCheck(sp);
  4271   4284       sp->name = Strsafe(x);
  4272   4285       sp->type = isupper(*x) ? TERMINAL : NONTERMINAL;
  4273   4286       sp->rule = 0;
  4274   4287       sp->fallback = 0;
  4275   4288       sp->prec = -1;
  4276   4289       sp->assoc = UNK;
................................................................................
  4445   4458   ** problems, or if the array is empty. */
  4446   4459   struct symbol **Symbol_arrayof()
  4447   4460   {
  4448   4461     struct symbol **array;
  4449   4462     int i,size;
  4450   4463     if( x2a==0 ) return 0;
  4451   4464     size = x2a->count;
  4452         -  array = (struct symbol **)malloc( sizeof(struct symbol *)*size );
         4465  +  array = (struct symbol **)calloc(size, sizeof(struct symbol *));
  4453   4466     if( array ){
  4454   4467       for(i=0; i<size; i++) array[i] = x2a->tbl[i].data;
  4455   4468     }
  4456   4469     return array;
  4457   4470   }
  4458   4471   
  4459   4472   /* Compare two configurations */
................................................................................
  4496   4509     return h;
  4497   4510   }
  4498   4511   
  4499   4512   /* Allocate a new state structure */
  4500   4513   struct state *State_new()
  4501   4514   {
  4502   4515     struct state *new;
  4503         -  new = (struct state *)malloc( sizeof(struct state) );
         4516  +  new = (struct state *)calloc(1, sizeof(struct state) );
  4504   4517     MemoryCheck(new);
  4505   4518     return new;
  4506   4519   }
  4507   4520   
  4508   4521   /* There is one instance of the following structure for each
  4509   4522   ** associative array of type "x3".
  4510   4523   */