xref: /plan9/sys/src/cmd/lex/header.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 # include "ldefs.h"
2 void
3 phead1(void)
4 {
5 	Bprint(&fout,"typedef unsigned char Uchar;\n");
6 	Bprint(&fout,"# include <stdio.h>\n");
7 	Bprint(&fout, "# define U(x) x\n");
8 	Bprint(&fout, "# define NLSTATE yyprevious=YYNEWLINE\n");
9 	Bprint(&fout,"# define BEGIN yybgin = yysvec + 1 +\n");
10 	Bprint(&fout,"# define INITIAL 0\n");
11 	Bprint(&fout,"# define YYLERR yysvec\n");
12 	Bprint(&fout,"# define YYSTATE (yyestate-yysvec-1)\n");
13 	Bprint(&fout,"# define YYOPTIM 1\n");
14 # ifdef DEBUG
15 	Bprint(&fout,"# define LEXDEBUG 1\n");
16 # endif
17 	Bprint(&fout,"# define YYLMAX 200\n");
18 	Bprint(&fout,"# define output(c) putc(c,yyout)\n");
19 	Bprint(&fout, "%s%d%s\n",
20   "# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==",
21 	'\n',
22  "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
23 	Bprint(&fout,
24 "# define unput(c) {yytchar= (c);if(yytchar=='\\n')yylineno--;*yysptr++=yytchar;}\n");
25 	Bprint(&fout,"# define yymore() (yymorfg=1)\n");
26 	Bprint(&fout,"# define ECHO fprintf(yyout, \"%%s\",yytext)\n");
27 	Bprint(&fout,"# define REJECT { nstr = yyreject(); goto yyfussy;}\n");
28 	Bprint(&fout,"int yyleng; extern char yytext[];\n");
29 	Bprint(&fout,"int yymorfg;\n");
30 	Bprint(&fout,"extern Uchar *yysptr, yysbuf[];\n");
31 	Bprint(&fout,"int yytchar;\n");
32 	Bprint(&fout,"FILE *yyin = {stdin}, *yyout = {stdout};\n");
33 	Bprint(&fout,"extern int yylineno;\n");
34 	Bprint(&fout,"struct yysvf { \n");
35 	Bprint(&fout,"\tstruct yywork *yystoff;\n");
36 	Bprint(&fout,"\tstruct yysvf *yyother;\n");
37 	Bprint(&fout,"\tint *yystops;};\n");
38 	Bprint(&fout,"struct yysvf *yyestate;\n");
39 	Bprint(&fout,"extern struct yysvf yysvec[], *yybgin;\n");
40 	Bprint(&fout,"int yylook(void), yywrap(void), yyback(int *, int);\n");
41 }
42 
43 void
44 phead2(void)
45 {
46 	Bprint(&fout,"while((nstr = yylook()) >= 0)\n");
47 	Bprint(&fout,"yyfussy: switch(nstr){\n");
48 	Bprint(&fout,"case 0:\n");
49 	Bprint(&fout,"if(yywrap()) return(0); break;\n");
50 }
51 
52 void
53 ptail(void)
54 {
55 	if(!pflag){
56 		Bprint(&fout,"case -1:\nbreak;\n");		/* for reject */
57 		Bprint(&fout,"default:\n");
58 		Bprint(&fout,"fprintf(yyout,\"bad switch yylook %%d\",nstr);\n");
59 		Bprint(&fout,"} return(0); }\n");
60 		Bprint(&fout,"/* end of yylex */\n");
61 	}
62 	pflag = 1;
63 }
64 
65 void
66 statistics(void)
67 {
68 	fprint(errorf,"%d/%d nodes(%%e), %d/%d positions(%%p), %d/%d (%%n), %ld transitions\n",
69 		tptr, treesize, nxtpos-positions, maxpos, stnum+1, nstates, rcount);
70 	fprint(errorf, ", %d/%d packed char classes(%%k)", pcptr-pchar, pchlen);
71 	fprint(errorf,", %d/%d packed transitions(%%a)",nptr, ntrans);
72 	fprint(errorf, ", %d/%d output slots(%%o)", yytop, outsize);
73 	fprint(errorf,"\n");
74 }
75