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