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

📄 add.c

📁 yacc和lex的一个例子代码,完全原创
💻 C
字号:
# include "stdio.h"#if defined(__cplusplus)   extern "C" {#endif#if (defined(__cplusplus) || defined(__STDC__))     extern int yyreject();     extern int yywrap();     extern int yylook();     extern int yyback(int *, int);     extern int yyinput();     extern void yyoutput(int);     extern void yyunput(int);     extern int yylex();     extern int yyless(int);#ifdef LEXDEBUG     extern void allprint();     extern void sprint();#endif#if defined(__cplusplus)   }#endif#endif	/* __cplusplus or __STDC__ */# define U(x) x# define NLSTATE yyprevious=YYNEWLINE# define BEGIN yybgin = yysvec + 1 +# define INITIAL 0# define YYLERR yysvec# define YYSTATE (yyestate-yysvec-1)# define YYOPTIM 1# define YYLMAX 200# define output(c) putc(c,yyout)# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)# define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--;*yysptr++=yytchar;}# define yymore() (yymorfg=1)# define ECHO fprintf(yyout, "%s",yytext)# define REJECT { nstr = yyreject(); goto yyfussy;}long yyleng;long yylenguc;extern unsigned char yytextarr[];# ifdef YYCHAR_ARRAYextern char yytext[];# elseextern unsigned char yytext[];# endifint yyposix_point=0;int yynls16=0;int yynls_wchar=0;char *yylocale = "C C C C C C";int yymorfg;extern unsigned char *yysptr, yysbuf[];int yytchar;FILE *yyin = {stdin}, *yyout = {stdout};extern int yylineno;struct yysvf { 	int yystoff;	struct yysvf *yyother;	int *yystops;};struct yysvf *yyestate;extern struct yysvf yysvec[], *yybgin;#include <stdlib.h> extern void yyerror(const char* s);# define YYNEWLINE 10yylex(){   int nstr; extern int yyprevious;   while((nstr = yylook()) >= 0)yyfussy: switch(nstr){case 0:   if(yywrap()) return(0); break;case 1:	{				yylval = atoi((const char*)yytext); 				return INTEGER; 		}break;case 2:	return *yytext;break;case 3:	;break;case 4:	yyerror("invalid character");break;case -1:break;default:   fprintf(yyout,"bad switch yylook %d",nstr);} return(0); }/* end of yylex */#ifndef __cplusplusstatic void __yy__unused() { main(); }#endifint yywrap(void) { 	return 1; } int yyvstop[] = {0,4,0,3,4,0,2,0,2,4,0,1,4,0,1,0,0};# define YYTYPE unsigned charstruct yywork { YYTYPE verify, advance; } yycrank[] = {{0,0},	{0,0},	{1,3},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{1,4},	{1,5},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{0,0},	{1,6},	{0,0},	{0,0},	{0,0},	{0,0},	{1,7},	{7,8},	{7,8},	{7,8},	{7,8},	{7,8},	{7,8},	{7,8},	{7,8},	{7,8},	{7,8},	{0,0}};struct yysvf yysvec[] = {{0,	0,	0},{-1,	0,		0},	{0,	yysvec+1,	0},	{0,	0,		yyvstop+1},{0,	0,		yyvstop+3},{0,	0,		yyvstop+6},{0,	0,		yyvstop+8},{2,	0,		yyvstop+11},{0,	yysvec+7,	yyvstop+14},{0,	0,	0}};struct yywork *yytop = yycrank+59;struct yysvf *yybgin = yysvec+1;unsigned char yymatch[] = {00  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,011 ,012 ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,011 ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,'+' ,01  ,'+' ,01  ,01  ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,01  ,0};unsigned char yyextra[] = {0,0,0,0,0,0,0,0,0};/* @(#) B.11.11.06 HP C LANGUAGE TOOL (NCFORM) 010402 $      */#ifdef YYREENTRANT__thread int yylineno;#elseint yylineno = 1;#endif   /* reentrant */# define YYU(x) x# define NLSTATE yyprevious=YYNEWLINE #ifdef YYNLS16_WCHAR#  ifdef YYREENTRANT__thread unsigned char yytextuc[YYLMAX * sizeof(wchar_t)];#  elseunsigned char yytextuc[YYLMAX * sizeof(wchar_t)];#  endif   /* reentrant */# ifdef YY_PCT_POINT /* for %pointer */#   ifdef YYREENTRANT__thread wchar_t yytextarr[YYLMAX];__thread wchar_t *yytext;#   elsewchar_t yytextarr[YYLMAX];wchar_t *yytext;#   endif            /* reentrant */# else               /* %array */#   ifdef YYREENTRANT__thread wchar_t yytextarr[YYLMAX];__thread wchar_t yytext[YYLMAX];#   elsewchar_t yytextarr[YYLMAX];wchar_t yytext[YYLMAX];#   endif            /* reentrant */# endif              /* YY_PCT_POINT */#else#  ifdef YYREENTRANT__thread unsigned char yytextuc;#  elseunsigned char yytextuc;#  endif             /* reentrant */# ifdef YY_PCT_POINT /* for %pointer */#   ifdef YYREENTRANT__thread unsigned char yytextarr[YYLMAX];__thread unsigned char *yytext;#   elseunsigned char yytextarr[YYLMAX];unsigned char *yytext;#   endif            /* reentrant */# else               /* %array */#   ifdef YYREENTRANT__thread unsigned char yytextarr[YYLMAX];#   elseunsigned char yytextarr[YYLMAX];#   endif            /* reentrant */# ifdef YYCHAR_ARRAY#   ifdef YYREENTRANT__thread char yytext[YYLMAX];#   elsechar yytext[YYLMAX];#   endif            /* reentrant */# else#   ifdef YYREENTRANT__thread unsigned char yytext[YYLMAX];#   elseunsigned char yytext[YYLMAX];#   endif            /* reentrant */# endif              /* YYCHAR_ARRAY */# endif              /* YY_PCT_POINT */#endif               /* YYNLS16_WCHAR */#ifdef YYREENTRANT__thread struct yysvf *yylstate[YYLMAX], **yylsp, **yyolsp;__thread unsigned char yysbuf[YYLMAX];__thread unsigned char *yysptr;  /*  = yysbuf; */__thread int *yyfnd;__thread extern struct yysvf *yyestate;#elsestruct yysvf *yylstate [YYLMAX], **yylsp, **yyolsp;unsigned char yysbuf[YYLMAX];unsigned char *yysptr = yysbuf; int *yyfnd;extern struct yysvf *yyestate;#endif#ifdef YYREENTRANT__thread int yyprevious;  /* YYNEWLINE; */#elseint yyprevious = YYNEWLINE; #endif#ifdef YYREENTRANT/* * initialize tls data.  These were global data and were init  * when declared.  Lex will always generate this function  * but invoke it only once.   */yyinit_tls(){  yylineno = 1;  yysptr = yysbuf;  yyprevious = YYNEWLINE;   if (!yyin) yyin = stdin;  if (!yyout) yyout = stdout; }#endifyylook(){	register struct yysvf *yystate, **lsp;	register struct yywork *yyt;	struct yysvf *yyz;	int yych, yyfirst;	struct yywork *yyr;# ifdef LEXDEBUG	int debug;# endif/*	char *yylastch; * ***** nls8 ***** */	unsigned char *yylastch, sec, third, fourth;	/* start off machines */# ifdef LEXDEBUG	debug = 0;# endif	yyfirst=1;	if (!yymorfg)#ifdef YYNLS16_WCHAR		yylastch = yytextuc;#else# ifdef YYCHAR_ARRAY		yylastch = (unsigned char *)yytext;# else		yylastch = yytext;# endif#endif	else {		yymorfg=0;#ifdef YYNLS16_WCHAR		yylastch = yytextuc+yylenguc;#else# ifdef YYCHAR_ARRAY		yylastch = (unsigned char *)yytext+yyleng;# else		yylastch = yytext+yyleng;# endif#endif		}	for(;;){		lsp = yylstate;		yyestate = yystate = yybgin;		if (yyprevious==YYNEWLINE) yystate++;		for (;;){# ifdef LEXDEBUG			if(debug)fprintf(yyout,"state %d\n",yystate-yysvec-1);# endif			yyt = &yycrank[yystate->yystoff];			if(yyt == yycrank && !yyfirst){  /* may not be any transitions */				yyz = yystate->yyother;				if(yyz == 0)break;				if(yyz->yystoff == 0)break;				}			*yylastch++ = yych = input();			yyfirst=0;		tryagain:# ifdef LEXDEBUG			if(debug){				fprintf(yyout,"char ");				allprint(yych);				putchar('\n');				}# endif			yyr = yyt;			if ((long)yyt > (long)yycrank){				yyt = yyr + yych;				if (yyt <= yytop && yyt->verify+yysvec == yystate){					if(yyt->advance+yysvec == YYLERR)	/* error transitions */						{unput(*--yylastch);break;}					*lsp++ = yystate = yyt->advance+yysvec;					goto contin;					}				}# ifdef YYOPTIM			else if((long)yyt < (long)yycrank) {  /* r < yycrank */				yyt = yyr = yycrank+(yycrank-yyt);# ifdef LEXDEBUG				if(debug)fprintf(yyout,"compressed state\n");# endif				yyt = yyt + yych;				if(yyt <= yytop && yyt->verify+yysvec == yystate){					if(yyt->advance+yysvec == YYLERR)	/* error transitions */						{unput(*--yylastch);break;}					*lsp++ = yystate = yyt->advance+yysvec;					goto contin;					}				yyt = yyr + YYU(yymatch[yych]);# ifdef LEXDEBUG				if(debug){					fprintf(yyout,"try fall back character ");					allprint(YYU(yymatch[yych]));					putchar('\n');					}# endif				if(yyt <= yytop && yyt->verify+yysvec == yystate){					if(yyt->advance+yysvec == YYLERR)	/* error transition */						{unput(*--yylastch);break;}					*lsp++ = yystate = yyt->advance+yysvec;					goto contin;					}				}			if ((yystate = yystate->yyother) && (yyt = &yycrank[yystate->yystoff]) != yycrank){# ifdef LEXDEBUG				if(debug)fprintf(yyout,"fall back to state %d\n",yystate-yysvec-1);# endif				goto tryagain;				}# endif			else				{unput(*--yylastch);break;}		contin:# ifdef LEXDEBUG			if(debug){				fprintf(yyout,"state %d char ",yystate-yysvec-1);				allprint(yych);				putchar('\n');				}# endif			;			}# ifdef LEXDEBUG		if(debug){			fprintf(yyout,"stopped at %d with ",*(lsp-1)-yysvec-1);			allprint(yych);			putchar('\n');			}# endif		while (lsp-- > yylstate){			*yylastch-- = 0;			if (*lsp != 0 && (yyfnd= (*lsp)->yystops) && *yyfnd > 0){				yyolsp = lsp;				if(yyextra[*yyfnd]){		/* must backup */					while(yyback((*lsp)->yystops,-*yyfnd) != 1 && lsp > yylstate){						lsp--;						unput(*yylastch--);						}					}				yyprevious = YYU(*yylastch);				yylsp = lsp;#ifdef YYNLS16_WCHAR				yylenguc = yylastch-yytextuc+1;				yytextuc[yylenguc] = 0;#else# ifdef YYCHAR_ARRAY				yyleng = yylastch-(unsigned char*)yytext+1;# else				yyleng = yylastch-yytext+1;# endif				yytext[yyleng] = 0;#endif# ifdef LEXDEBUG				if(debug){					fprintf(yyout,"\nmatch ");#ifdef YYNLS16_WCHAR					sprint(yytextuc);#else					sprint(yytext);#endif					fprintf(yyout," action %d\n",*yyfnd);					}# endif				return(*yyfnd++);				}			unput(*yylastch);			}#ifdef YYNLS16_WCHAR		if (yytextuc[0] == 0  /* && feof(yyin) */)#else		if (yytext[0] == 0  /* && feof(yyin) */)#endif			{			yysptr=yysbuf;			return(0);			}#ifdef YYNLS16_WCHAR		yyprevious = yytextuc[0] = input();#else		yyprevious = yytext[0] = input();#endif		if (yyprevious>0) {			output(yyprevious);#ifdef YYNLS16                        if (yynls16) {			int noBytes;                        sec = input();                        third = input();                        fourth = input();#ifdef YYNLS16_WCHAR                        noBytes = MultiByte(yytextuc[0],sec,third,fourth);#else                         noBytes = MultiByte(yytext[0],sec,third,fourth);#endif               					switch(noBytes) {     					case 2:#ifdef YYNLS16_WCHAR 						output(yyprevious=yytextuc[0]=sec);#else 						output(yyprevious=yytext[0]=sec);#endif                                                 unput(fourth);                                                 unput(third);                                                 break;     					case 3:#ifdef YYNLS16_WCHAR 						output(yyprevious=yytextuc[0]=sec); 						output(yyprevious=yytextuc[0]=third);#else 						output(yyprevious=yytext[0]=sec); 						output(yyprevious=yytext[0]=third);#endif                                                 unput(fourth);                                                 break;                                          case 4:#ifdef YYNLS16_WCHAR 						output(yyprevious=yytextuc[0]=sec); 						output(yyprevious=yytextuc[0]=third); 						output(yyprevious=yytextuc[0]=fourth);#else 						output(yyprevious=yytext[0]=sec); 						output(yyprevious=yytext[0]=third); 						output(yyprevious=yytext[0]=fourth);#endif                                                 break;                                                                                            					default:					        unput(fourth);					        unput(third);						unput(sec);						break;						}					}#endif                }#ifdef YYNLS16_WCHAR		yylastch=yytextuc;#else# ifdef YYCHAR_ARRAY		yylastch=(unsigned char*)yytext;# else		yylastch=yytext;# endif#endif# ifdef LEXDEBUG		if(debug)putchar('\n');# endif		}	}# ifdef __cplusplusyyback(int *p, int m)# elseyyback(p, m)	int *p;# endif{if (p==0) return(0);while (*p)	{	if (*p++ == m)		return(1);	}return(0);}	/* the following are only used in the lex library */yyinput(){	return(input());		}#if (defined(__cplusplus) || defined(__STDC__))void yyoutput(int c)#elseyyoutput(c)  int c;# endif{	output(c);}#if (defined(__cplusplus) || defined(__STDC__))void yyunput(int c)#elseyyunput(c)   int c;#endif{	unput(c);}

⌨️ 快捷键说明

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