aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/lex/header.c
blob: 8264f4c4ee7d0a9d1d8c734a5d1f68f02bd91bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# include "ldefs.h"

extern int nine;

void
phead1(void)
{
	Bprint(&fout,"typedef unsigned char Uchar;\n");
	if (nine) {
		Bprint(&fout,"# include <u.h>\n");
		Bprint(&fout,"# include <libc.h>\n");
	}
	Bprint(&fout,"# include <stdio.h>\n");
	Bprint(&fout, "# define U(x) x\n");
	Bprint(&fout, "# define NLSTATE yyprevious=YYNEWLINE\n");
	Bprint(&fout,"# define BEGIN yybgin = yysvec + 1 +\n");
	Bprint(&fout,"# define INITIAL 0\n");
	Bprint(&fout,"# define YYLERR yysvec\n");
	Bprint(&fout,"# define YYSTATE (yyestate-yysvec-1)\n");
	Bprint(&fout,"# define YYOPTIM 1\n");
# ifdef DEBUG
	Bprint(&fout,"# define LEXDEBUG 1\n");
# endif
	Bprint(&fout,"# define YYLMAX 200\n");
	Bprint(&fout,
"# define unput(c) {yytchar= (c);if(yytchar=='\\n')yylineno--;*yysptr++=yytchar;}\n");
	Bprint(&fout,"# define yymore() (yymorfg=1)\n");
	Bprint(&fout,"# define ECHO fprintf(yyout, \"%%s\",yytext)\n");
	Bprint(&fout,"# define REJECT { nstr = yyreject(); goto yyfussy;}\n");
	Bprint(&fout,"int yyleng; extern char yytext[];\n");
	Bprint(&fout,"int yymorfg;\n");
	Bprint(&fout,"extern Uchar *yysptr, yysbuf[];\n");
	Bprint(&fout,"int yytchar;\n");
/*	Bprint(&fout,"FILE *yyin = stdin, *yyout = stdout;\n"); */
	Bprint(&fout,"FILE *yyin, *yyout;\n");
	Bprint(&fout,"extern int yylineno;\n");
	Bprint(&fout,"struct yysvf { \n");
	Bprint(&fout,"\tstruct yywork *yystoff;\n");
	Bprint(&fout,"\tstruct yysvf *yyother;\n");
	Bprint(&fout,"\tint *yystops;};\n");
	Bprint(&fout,"struct yysvf *yyestate;\n");
	Bprint(&fout,"extern struct yysvf yysvec[], *yybgin;\n");
	Bprint(&fout,"int yylook(void), yywrap(void), yyback(int *, int);\n");
	if(nine) {
		Bprint(&fout,
				"int infd, outfd;\n"
				"\n"
				"void\n"
				"output(char c)\n"
				"{\n"
				"	int rv;\n"
				"	if ((rv = write(outfd, &c, 1)) < 0)\n"
				"		sysfatal(\"output: %%r\");\n"
				"	if (rv == 0)\n"
				"		sysfatal(\"output: EOF?\");\n"
				"}\n"
				"\n"
				"int\n"
				"input(void)\n"
				"{\n"
				"	if(yysptr > yysbuf)\n"
				"		yytchar = U(*--yysptr);\n"
				"	else {\n"
				"		int rv;\n"
				"		if ((rv = read(infd, &yytchar, 1)) < 0)\n"
				"			sysfatal(\"input: %%r\");\n"
				"		if (rv == 0)\n"
				"			return 0;\n"
				"	}\n"
				"	if (yytchar == '\\n')\n"
				"		yylineno++;\n"
				"	return yytchar;\n"
				"}\n");
	}
	else {
		Bprint(&fout,"# define output(c) putc(c,yyout)\n");
		Bprint(&fout, "%s%d%s\n",
 		 "# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==",
		'\n',
 		"?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
	}
}

void
phead2(void)
{
	Bprint(&fout,"while((nstr = yylook()) >= 0){\n");
	Bprint(&fout,"goto yyfussy; yyfussy: switch(nstr){\n");
	Bprint(&fout,"case 0:\n");
	Bprint(&fout,"if(yywrap()) return(0); break;\n");
}

void
ptail(void)
{
	if(!pflag){
		Bprint(&fout,"case -1:\nbreak;\n");		/* for reject */
		Bprint(&fout,"default:\n");
		Bprint(&fout,"fprintf(yyout,\"bad switch yylook %%d\",nstr);\n");
		Bprint(&fout,"}} return(0); }\n");
		Bprint(&fout,"/* end of yylex */\n");
	}
	pflag = 1;
}

void
statistics(void)
{
	fprint(errorf,"%d/%d nodes(%%e), %d/%d positions(%%p), %d/%d (%%n), %ld transitions\n",
		tptr, treesize, (int)(nxtpos-positions), maxpos, stnum+1, nstates, rcount);
	fprint(errorf, ", %d/%d packed char classes(%%k)", (int)(pcptr-pchar), pchlen);
	fprint(errorf,", %d/%d packed transitions(%%a)",nptr, ntrans);
	fprint(errorf, ", %d/%d output slots(%%o)", yytop, outsize);
	fprint(errorf,"\n");
}