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

📄 btyaccpa.ske

📁 从lex&yacc说到编译器
💻 SKE
📖 第 1 页 / 共 2 页
字号:
/*  The banner used here should be replaced with an #ident directive    *//*  if the target C compiler supports #ident directives.                *//*                                                                      *//*  If the skeleton is changed, the banner should be changed so that    *//*  the altered version can easily be distinguished from the original.  */%% banner//// @(#)btyaccpar, based on byacc 1.8 (Berkeley)//#define YYBTYACC 1#include <stdio.h>#include <stdlib.h>#include <string.h>typedef int Yshort;%% tables#define _C_ "C"extern _C_ Yshort yylhs[];extern _C_ Yshort yylen[];extern _C_ Yshort yydefred[];extern _C_ Yshort yydgoto[];extern _C_ Yshort yysindex[];extern _C_ Yshort yyrindex[];extern _C_ Yshort yycindex[];extern _C_ Yshort yygindex[];extern _C_ Yshort yytable[];extern _C_ Yshort yycheck[];extern _C_ Yshort yyctable[];#if YYDEBUGextern _C_ char *yyname[];extern _C_ char *yyrule[];#endif%% header//// YYPOSN is user-defined text position type.//#ifndef YYPOSN#define YYPOSN int#endif#ifdef YYREDUCEPOSNFUNC#define YYCALLREDUCEPOSN(e)   \	if(reduce_posn) {     \	  YYREDUCEPOSNFUNC(yyps->pos, &(yyps->psp)[1-yym], &(yyps->vsp)[1-yym], \			   yym, yyps->psp - yyps->ps, yychar, yyposn, e);       \	  reduce_posn = 0;    \	}#ifndef YYCALLREDUCEPOSNARG#define YYCALLREDUCEPOSNARG yyps->val#endif#define YYPOSNARG(n) ((yyps->psp)[1-yym+(n)-1])#define YYPOSNOUT    (yyps->pos)#endif// If delete function is not defined by the user, do not deletions.#ifndef YYDELETEVAL#define YYDELETEVAL(v) #endif// If delete function is not defined by the user, do not deletions.#ifndef YYDELETEPOSN#define YYDELETEPOSN(v) #endif#define yyclearin (yychar=(-1))#define yyerrok (yyps->errflag=0)#ifndef YYSTACKGROWTH#define YYSTACKGROWTH 16#endif#ifndef YYDEFSTACKSIZE#define YYDEFSTACKSIZE 12#endif#ifdef YYDEBUGint yydebug;#endifint yynerrs;/* These value/posn are taken from the lexer */YYSTYPE yylval;YYPOSN  yyposn;/* These value/posn of the root non-terminal are returned to the caller */YYSTYPE yyretlval;YYPOSN  yyretposn;#define YYABORT  goto yyabort#define YYACCEPT goto yyaccept#define YYERROR  goto yyerrlab#define YYVALID         do { if (yyps->save)          goto yyvalid; } while(0)#define YYVALID_NESTED  do { if (yyps->save && \                                 yyps->save->save==0) goto yyvalid; } while(0)struct yyparsestate {  yyparsestate *save;        // Previously saved parser state  int           state;  int           errflag;  Yshort       *ssp;         // state stack pointer  YYSTYPE      *vsp;         // value stack pointer  YYPOSN       *psp;         // position stack pointer  YYSTYPE       val;         // value as returned by actions  YYPOSN        pos;         // position as returned by universal action  Yshort       *ss;          // state stack base  YYSTYPE      *vs;          // values stack base  YYPOSN       *ps;          // position stack base  int           lexeme;      // index of the conflict lexeme in the lexical queue  unsigned int  stacksize;   // current maximum stack size  Yshort        ctry;        // index in yyctable[] for this conflict};// Current parser statestatic yyparsestate *yyps=0;// yypath!=NULL: do the full parse, starting at *yypath parser state.static yyparsestate *yypath=0;// Base of the lexical value queuestatic YYSTYPE *yylvals=0;// Current posistion at lexical value queuestatic YYSTYPE *yylvp=0;// End position of lexical value queuestatic YYSTYPE *yylve=0;// The last allocated position at the lexical value queuestatic YYSTYPE *yylvlim=0;// Base of the lexical position queuestatic YYPOSN *yylpsns=0;// Current posistion at lexical position queuestatic YYPOSN *yylpp=0;// End position of lexical position queuestatic YYPOSN *yylpe=0;// The last allocated position at the lexical position queuestatic YYPOSN *yylplim=0;// Current position at lexical token queuestatic Yshort *yylexp=0;static Yshort *yylexemes=0;//// For use in generated program//#define yytrial (yyps->save)#define yyvsp   (yyps->vsp)#define yyval   (yyps->val)#define yydepth (yyps->ssp - yyps->ss)//// Local prototypes.//int yyparse(void);int  YYLex1();int  yyexpand();void YYSCopy(YYSTYPE *to, YYSTYPE *from, int size);void YYPCopy(YYPOSN  *to, YYPOSN  *from, int size);void YYMoreStack(yyparsestate *yyps);yyparsestate *YYNewState(int size);void YYFreeState(yyparsestate *p);%% body//// Parser function//int yyparse() {  int yym, yyn, yystate, yychar, yynewerrflag;  yyparsestate *yyerrctx = NULL;  int reduce_posn;#if YYDEBUG  char *yys;    if ((yys = getenv("YYDEBUG"))) {    yyn = *yys;    if (yyn >= '0' && yyn <= '9')      yydebug = yyn - '0';   }#endif    yyps = YYNewState(YYDEFSTACKSIZE);  yyps->save = 0;  yynerrs = 0;  yyps->errflag = 0;  yychar = (-1);    yyps->ssp = yyps->ss;  yyps->vsp = yyps->vs;  yyps->psp = yyps->ps;  *(yyps->ssp) = yystate = 0;    //  // Main parsing loop  // yyloop:  if ((yyn = yydefred[yystate])) {    goto yyreduce;  }  //  // Read one token  //  if (yychar < 0) {    if ((yychar = YYLex1()) < 0) yychar = 0;#if YYDEBUG    if (yydebug) {      yys = 0;      if (yychar <= YYMAXTOKEN) yys = yyname[yychar];      if (!yys) yys = "illegal-symbol";      printf("yydebug[%d,%d]: state %d, reading %d (%s)", 	     yydepth, (int)yytrial, yystate, yychar, yys);#ifdef YYDBPR      printf("<");      YYDBPR(yylval);      printf(">");#endif      printf("\n");     }#endif  }  //  // Do we have a conflict?  //  if ((yyn = yycindex[yystate]) &&      (yyn += yychar) >= 0 &&      yyn <= YYTABLESIZE &&      yycheck[yyn] == yychar) {    int ctry;    if (yypath) {#if YYDEBUG      if (yydebug) {        printf("yydebug[%d,%d]: CONFLICT in state %d: following successful trial parse\n", 	       yydepth, (int)yytrial, yystate);      }#endif      // Switch to the next conflict context      yyparsestate *save = yypath;      yypath = save->save;      ctry = save->ctry;      if (save->state != yystate)         goto yyabort;      YYFreeState(save);     } else {#if YYDEBUG      if (yydebug) {        printf("yydebug[%d,%d]: CONFLICT in state %d. ", 	       yydepth, (int)yytrial, yystate);        if(yyps->save) {          printf("ALREADY in conflict. Continue trial parse.");        } else {          printf("Start trial parse.");        }        printf("\n");      }#endif      yyparsestate *save = YYNewState(yyps->ssp - yyps->ss);      save->save    = yyps->save;      save->state   = yystate;      save->errflag = yyps->errflag;      save->ssp     = save->ss + (yyps->ssp - yyps->ss);      save->vsp     = save->vs + (yyps->vsp - yyps->vs);      save->psp     = save->ps + (yyps->psp - yyps->ps);      memcpy (save->ss, yyps->ss, (yyps->ssp - yyps->ss + 1)*sizeof(Yshort));      YYSCopy(save->vs, yyps->vs, (yyps->ssp - yyps->ss + 1));      YYPCopy(save->ps, yyps->ps, (yyps->ssp - yyps->ss + 1));      ctry = yytable[yyn];      if (yyctable[ctry] == -1) {#if YYDEBUG        if (yydebug && yychar >= 0)          printf("yydebug[%d]: backtracking 1 token\n", 		 (int)yytrial);#endif        ctry++;       }      save->ctry = ctry;      if (!yyps->save) {        // If this is a first conflict in the stack, start saving lexemes        if (!yylexemes) {          yylexemes = new Yshort[YYSTACKGROWTH];          yylvals = new YYSTYPE[YYSTACKGROWTH];          yylvlim = yylvals + YYSTACKGROWTH;           yylpsns = new YYPOSN[YYSTACKGROWTH];          yylplim = yylpsns + YYSTACKGROWTH;         }        if (yylvp == yylve) {          yylvp = yylve = yylvals;	  yylpp = yylpe = yylpsns;          yylexp = yylexemes;          if (yychar >= 0) {            *yylve++ = yylval;            *yylpe++ = yyposn;            *yylexp = yychar;            yychar = -1;           }         }       }      if (yychar >= 0) {        yylvp--, yylpp--, yylexp--;        yychar = -1;       }      save->lexeme = yylvp - yylvals;      yyps->save = save;     }    if (yytable[yyn] == ctry) {#if YYDEBUG      if (yydebug)        printf("yydebug[%d,%d]: state %d, shifting to state %d\n",               yydepth, (int)yytrial, yystate, yyctable[ctry]);#endif      if (yychar < 0)        yylvp++, yylpp++, yylexp++;      yychar = -1;      if (yyps->errflag > 0) --yyps->errflag;      yystate = yyctable[ctry];      goto yyshift;     } else {      yyn = yyctable[ctry];      goto yyreduce;     }   }  //  // Is action a shift?  //  if ((yyn = yysindex[yystate]) &&      (yyn += yychar) >= 0 &&      yyn <= YYTABLESIZE &&      yycheck[yyn] == yychar) {#if YYDEBUG    if (yydebug)      printf("yydebug[%d,%d]: state %d, shifting to state %d\n",             yydepth, (int)yytrial, yystate, yytable[yyn]);#endif    yychar = (-1);    if (yyps->errflag > 0)  --yyps->errflag;    yystate = yytable[yyn];  yyshift:    if (yyps->ssp >= yyps->ss + yyps->stacksize - 1) {      YYMoreStack(yyps);    }    *++(yyps->ssp) = yystate;    *++(yyps->vsp) = yylval;    *++(yyps->psp) = yyposn;    goto yyloop;  }  if ((yyn = yyrindex[yystate]) &&      (yyn += yychar) >= 0 &&      yyn <= YYTABLESIZE &&      yycheck[yyn] == yychar) {    yyn = yytable[yyn];    goto yyreduce;  }  //  // Action: error  //  if (yyps->errflag) goto yyinrecovery;  yynewerrflag = 1;  goto yyerrhandler;yyerrlab:  yynewerrflag = 0;yyerrhandler:  while (yyps->save) {     int ctry;     yyparsestate *save = yyps->save;#if YYDEBUG    if (yydebug)      printf("yydebug[%d,%d]: ERROR in state %d, CONFLICT BACKTRACKING to state %d, %d tokens\n",             yydepth, (int)yytrial, yystate, yyps->save->state, yylvp - yylvals - yyps->save->lexeme);#endif    // Memorize most forward-looking error state in case    // it's really an error.    if(yyerrctx==NULL || yyerrctx->lexeme<yylvp-yylvals) {      // Free old saved error context state      if(yyerrctx) YYFreeState(yyerrctx);      // Create and fill out new saved error context state      yyerrctx = YYNewState(yyps->ssp - yyps->ss);      yyerrctx->save = yyps->save;      yyerrctx->state = yystate;      yyerrctx->errflag = yyps->errflag;      yyerrctx->ssp = yyerrctx->ss + (yyps->ssp - yyps->ss);      yyerrctx->vsp = yyerrctx->vs + (yyps->vsp - yyps->vs);      yyerrctx->psp = yyerrctx->ps + (yyps->psp - yyps->ps);

⌨️ 快捷键说明

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