⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lemon.c

📁 sqlite-3.4.1,嵌入式数据库.是一个功能强大的开源数据库,给学习和研发以及小型公司的发展带来了全所未有的好处.
💻 C
📖 第 1 页 / 共 5 页
字号:
}/*** Process a command line switch which has an argument.*/static int handleswitch(i,err)int i;FILE *err;{  int lv = 0;  double dv = 0.0;  char *sv = 0, *end;  char *cp;  int j;  int errcnt = 0;  cp = strchr(argv[i],'=');  assert( cp!=0 );  *cp = 0;  for(j=0; op[j].label; j++){    if( strcmp(argv[i],op[j].label)==0 ) break;  }  *cp = '=';  if( op[j].label==0 ){    if( err ){      fprintf(err,"%sundefined option.\n",emsg);      errline(i,0,err);    }    errcnt++;  }else{    cp++;    switch( op[j].type ){      case OPT_FLAG:      case OPT_FFLAG:        if( err ){          fprintf(err,"%soption requires an argument.\n",emsg);          errline(i,0,err);        }        errcnt++;        break;      case OPT_DBL:      case OPT_FDBL:        dv = strtod(cp,&end);        if( *end ){          if( err ){            fprintf(err,"%sillegal character in floating-point argument.\n",emsg);            errline(i,((unsigned long)end)-(unsigned long)argv[i],err);          }          errcnt++;        }        break;      case OPT_INT:      case OPT_FINT:        lv = strtol(cp,&end,0);        if( *end ){          if( err ){            fprintf(err,"%sillegal character in integer argument.\n",emsg);            errline(i,((unsigned long)end)-(unsigned long)argv[i],err);          }          errcnt++;        }        break;      case OPT_STR:      case OPT_FSTR:        sv = cp;        break;    }    switch( op[j].type ){      case OPT_FLAG:      case OPT_FFLAG:        break;      case OPT_DBL:        *(double*)(op[j].arg) = dv;        break;      case OPT_FDBL:        (*(void(*)())(op[j].arg))(dv);        break;      case OPT_INT:        *(int*)(op[j].arg) = lv;        break;      case OPT_FINT:        (*(void(*)())(op[j].arg))((int)lv);        break;      case OPT_STR:        *(char**)(op[j].arg) = sv;        break;      case OPT_FSTR:        (*(void(*)())(op[j].arg))(sv);        break;    }  }  return errcnt;}int OptInit(a,o,err)char **a;struct s_options *o;FILE *err;{  int errcnt = 0;  argv = a;  op = o;  errstream = err;  if( argv && *argv && op ){    int i;    for(i=1; argv[i]; i++){      if( argv[i][0]=='+' || argv[i][0]=='-' ){        errcnt += handleflags(i,err);      }else if( strchr(argv[i],'=') ){        errcnt += handleswitch(i,err);      }    }  }  if( errcnt>0 ){    fprintf(err,"Valid command line options for \"%s\" are:\n",*a);    OptPrint();    exit(1);  }  return 0;}int OptNArgs(){  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;    }  }  return cnt;}char *OptArg(n)int n;{  int i;  i = argindex(n);  return i>=0 ? argv[i] : 0;}void OptErr(n)int n;{  int i;  i = argindex(n);  if( i>=0 ) errline(i,0,errstream);}void OptPrint(){  int i;  int max, len;  max = 0;  for(i=0; op[i].label; i++){    len = strlen(op[i].label) + 1;    switch( op[i].type ){      case OPT_FLAG:      case OPT_FFLAG:        break;      case OPT_INT:      case OPT_FINT:        len += 9;       /* length of "<integer>" */        break;      case OPT_DBL:      case OPT_FDBL:        len += 6;       /* length of "<real>" */        break;      case OPT_STR:      case OPT_FSTR:        len += 8;       /* length of "<string>" */        break;    }    if( len>max ) max = len;  }  for(i=0; op[i].label; i++){    switch( op[i].type ){      case OPT_FLAG:      case OPT_FFLAG:        fprintf(errstream,"  -%-*s  %s\n",max,op[i].label,op[i].message);        break;      case OPT_INT:      case OPT_FINT:        fprintf(errstream,"  %s=<integer>%*s  %s\n",op[i].label,          (int)(max-strlen(op[i].label)-9),"",op[i].message);        break;      case OPT_DBL:      case OPT_FDBL:        fprintf(errstream,"  %s=<real>%*s  %s\n",op[i].label,          (int)(max-strlen(op[i].label)-6),"",op[i].message);        break;      case OPT_STR:      case OPT_FSTR:        fprintf(errstream,"  %s=<string>%*s  %s\n",op[i].label,          (int)(max-strlen(op[i].label)-8),"",op[i].message);        break;    }  }}/*********************** From the file "parse.c" ****************************//*** Input file parser for the LEMON parser generator.*//* The state of the parser */struct pstate {  char *filename;       /* Name of the input file */  int tokenlineno;      /* Linenumber at which current token starts */  int errorcnt;         /* Number of errors so far */  char *tokenstart;     /* Text of current token */  struct lemon *gp;     /* Global state vector */  enum e_state {    INITIALIZE,    WAITING_FOR_DECL_OR_RULE,    WAITING_FOR_DECL_KEYWORD,    WAITING_FOR_DECL_ARG,    WAITING_FOR_PRECEDENCE_SYMBOL,    WAITING_FOR_ARROW,    IN_RHS,    LHS_ALIAS_1,    LHS_ALIAS_2,    LHS_ALIAS_3,    RHS_ALIAS_1,    RHS_ALIAS_2,    PRECEDENCE_MARK_1,    PRECEDENCE_MARK_2,    RESYNC_AFTER_RULE_ERROR,    RESYNC_AFTER_DECL_ERROR,    WAITING_FOR_DESTRUCTOR_SYMBOL,    WAITING_FOR_DATATYPE_SYMBOL,    WAITING_FOR_FALLBACK_ID,    WAITING_FOR_WILDCARD_ID  } state;                   /* The state of the parser */  struct symbol *fallback;   /* The fallback token */  struct symbol *lhs;        /* Left-hand side of current rule */  char *lhsalias;            /* Alias for the LHS */  int nrhs;                  /* Number of right-hand side symbols seen */  struct symbol *rhs[MAXRHS];  /* RHS symbols */  char *alias[MAXRHS];       /* Aliases for each RHS symbol (or NULL) */  struct rule *prevrule;     /* Previous rule parsed */  char *declkeyword;         /* Keyword of a declaration */  char **declargslot;        /* Where the declaration argument should be put */  int *decllnslot;           /* Where the declaration linenumber is put */  enum e_assoc declassoc;    /* Assign this association to decl arguments */  int preccounter;           /* Assign this precedence to decl arguments */  struct rule *firstrule;    /* Pointer to first rule in the grammar */  struct rule *lastrule;     /* Pointer to the most recently parsed rule */};/* Parse a single token */static void parseonetoken(psp)struct pstate *psp;{  char *x;  x = Strsafe(psp->tokenstart);     /* Save the token permanently */#if 0  printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,    x,psp->state);#endif  switch( psp->state ){    case INITIALIZE:      psp->prevrule = 0;      psp->preccounter = 0;      psp->firstrule = psp->lastrule = 0;      psp->gp->nrule = 0;      /* Fall thru to next case */    case WAITING_FOR_DECL_OR_RULE:      if( x[0]=='%' ){        psp->state = WAITING_FOR_DECL_KEYWORD;      }else if( islower(x[0]) ){        psp->lhs = Symbol_new(x);        psp->nrhs = 0;        psp->lhsalias = 0;        psp->state = WAITING_FOR_ARROW;      }else if( x[0]=='{' ){        if( psp->prevrule==0 ){          ErrorMsg(psp->filename,psp->tokenlineno,"There is not prior rule opon which to attach the code \fragment which begins on this line.");          psp->errorcnt++;	}else if( psp->prevrule->code!=0 ){          ErrorMsg(psp->filename,psp->tokenlineno,"Code fragment beginning on this line is not the first \to follow the previous rule.");          psp->errorcnt++;        }else{          psp->prevrule->line = psp->tokenlineno;          psp->prevrule->code = &x[1];	}      }else if( x[0]=='[' ){        psp->state = PRECEDENCE_MARK_1;      }else{        ErrorMsg(psp->filename,psp->tokenlineno,          "Token \"%s\" should be either \"%%\" or a nonterminal name.",          x);        psp->errorcnt++;      }      break;    case PRECEDENCE_MARK_1:      if( !isupper(x[0]) ){        ErrorMsg(psp->filename,psp->tokenlineno,          "The precedence symbol must be a terminal.");        psp->errorcnt++;      }else if( psp->prevrule==0 ){        ErrorMsg(psp->filename,psp->tokenlineno,          "There is no prior rule to assign precedence \"[%s]\".",x);        psp->errorcnt++;      }else if( psp->prevrule->precsym!=0 ){        ErrorMsg(psp->filename,psp->tokenlineno,"Precedence mark on this line is not the first \to follow the previous rule.");        psp->errorcnt++;      }else{        psp->prevrule->precsym = Symbol_new(x);      }      psp->state = PRECEDENCE_MARK_2;      break;    case PRECEDENCE_MARK_2:      if( x[0]!=']' ){        ErrorMsg(psp->filename,psp->tokenlineno,          "Missing \"]\" on precedence mark.");        psp->errorcnt++;      }      psp->state = WAITING_FOR_DECL_OR_RULE;      break;    case WAITING_FOR_ARROW:      if( x[0]==':' && x[1]==':' && x[2]=='=' ){        psp->state = IN_RHS;      }else if( x[0]=='(' ){        psp->state = LHS_ALIAS_1;      }else{        ErrorMsg(psp->filename,psp->tokenlineno,          "Expected to see a \":\" following the LHS symbol \"%s\".",          psp->lhs->name);        psp->errorcnt++;        psp->state = RESYNC_AFTER_RULE_ERROR;      }      break;    case LHS_ALIAS_1:      if( isalpha(x[0]) ){        psp->lhsalias = x;        psp->state = LHS_ALIAS_2;      }else{        ErrorMsg(psp->filename,psp->tokenlineno,          "\"%s\" is not a valid alias for the LHS \"%s\"\n",          x,psp->lhs->name);        psp->errorcnt++;        psp->state = RESYNC_AFTER_RULE_ERROR;      }      break;    case LHS_ALIAS_2:      if( x[0]==')' ){        psp->state = LHS_ALIAS_3;      }else{        ErrorMsg(psp->filename,psp->tokenlineno,          "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);        psp->errorcnt++;        psp->state = RESYNC_AFTER_RULE_ERROR;      }      break;    case LHS_ALIAS_3:      if( x[0]==':' && x[1]==':' && x[2]=='=' ){        psp->state = IN_RHS;      }else{        ErrorMsg(psp->filename,psp->tokenlineno,          "Missing \"->\" following: \"%s(%s)\".",           psp->lhs->name,psp->lhsalias);        psp->errorcnt++;        psp->state = RESYNC_AFTER_RULE_ERROR;      }      break;    case IN_RHS:      if( x[0]=='.' ){        struct rule *rp;        rp = (struct rule *)malloc( sizeof(struct rule) +              sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs );        if( rp==0 ){          ErrorMsg(psp->filename,psp->tokenlineno,            "Can't allocate enough memory for this rule.");          psp->errorcnt++;          psp->prevrule = 0;	}else{          int i;          rp->ruleline = psp->tokenlineno;          rp->rhs = (struct symbol**)&rp[1];          rp->rhsalias = (char**)&(rp->rhs[psp->nrhs]);          for(i=0; i<psp->nrhs; i++){            rp->rhs[i] = psp->rhs[i];            rp->rhsalias[i] = psp->alias[i];	  }          rp->lhs = psp->lhs;          rp->lhsalias = psp->lhsalias;          rp->nrhs = psp->nrhs;          rp->code = 0;          rp->precsym = 0;          rp->index = psp->gp->nrule++;          rp->nextlhs = rp->lhs->rule;          rp->lhs->rule = rp;          rp->next = 0;          if( psp->firstrule==0 ){            psp->firstrule = psp->lastrule = rp;	  }else{            psp->lastrule->next = rp;            psp->lastrule = rp;	  }          psp->prevrule = rp;	}        psp->state = WAITING_FOR_DECL_OR_RULE;      }else if( isalpha(x[0]) ){        if( psp->nrhs>=MAXRHS ){          ErrorMsg(psp->filename,psp->tokenlineno,            "Too many symbols on RHS or rule beginning at \"%s\".",            x);          psp->errorcnt++;          psp->state = RESYNC_AFTER_RULE_ERROR;	}else{          psp->rhs[psp->nrhs] = Symbol_new(x);          psp->alias[psp->nrhs] = 0;          psp->nrhs++;	}      }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){        struct symbol *msp = psp->rhs[psp->nrhs-1];        if( msp->type!=MULTITERMINAL ){          struct symbol *origsp = msp;          msp = mal

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -