xref: /csrg-svn/old/lex/lmain.c (revision 18034)
1 #ifndef lint
2 static char sccsid[] = "@(#)lmain.c	4.3 (Berkeley) 02/21/85";
3 #endif
4 
5 # include "ldefs.c"
6 # include "once.c"
7 
8 	/* lex [-[drcyvntf]] [file] ... [file] */
9 
10 	/* Copyright 1976, Bell Telephone Laboratories, Inc.,
11 	    written by Eric Schmidt, August 27, 1976   */
12 
13 main(argc,argv)
14   int argc;
15   char **argv; {
16 	register int i;
17 # ifdef DEBUG
18 #include <signal.h>
19 	signal(SIGBUS,buserr);
20 	signal(SIGSEGV,segviol);
21 # endif
22 	while (argc > 1 && argv[1][0] == '-' ){
23 		i = 0;
24 		while(argv[1][++i]){
25 			switch (argv[1][i]){
26 # ifdef DEBUG
27 				case 'd': debug++; break;
28 				case 'y': yydebug = TRUE; break;
29 # endif
30 				case 'r': case 'R':
31 					ratfor=TRUE; break;
32 				case 'c': case 'C':
33 					ratfor=FALSE; break;
34 				case 't': case 'T':
35 					fout = stdout;
36 					errorf = stderr;
37 					break;
38 				case 'v': case 'V':
39 					report = 1;
40 					break;
41 				case 'f': case 'F':
42 					optim = FALSE;
43 					break;
44 				case 'n': case 'N':
45 					report = 0;
46 					break;
47 				default:
48 					warning("Unknown option %c",argv[1][i]);
49 				}
50 			}
51 		argc--;
52 		argv++;
53 		}
54 	sargc = argc;
55 	sargv = argv;
56 	if (argc > 1){
57 		fin = fopen(argv[++fptr], "r");		/* open argv[1] */
58 		sargc--;
59 		}
60 	else fin = stdin;
61 	if(fin == NULL)
62 		error ("Can't read input file %s",argc>1?argv[1]:"standard input");
63 	gch();
64 		/* may be gotten: def, subs, sname, schar, ccl, dchar */
65 	get1core();
66 		/* may be gotten: name, left, right, nullstr, parent */
67 	scopy("INITIAL",sp);
68 	sname[0] = sp;
69 	sp += slength("INITIAL") + 1;
70 	sname[1] = 0;
71 	if(yyparse(0)) exit(1);	/* error return code */
72 		/* may be disposed of: def, subs, dchar */
73 	free1core();
74 		/* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */
75 	get2core();
76 	ptail();
77 	mkmatch();
78 # ifdef DEBUG
79 	if(debug) pccl();
80 # endif
81 	sect  = ENDSECTION;
82 	if(tptr>0)cfoll(tptr-1);
83 # ifdef DEBUG
84 	if(debug)pfoll();
85 # endif
86 	cgoto();
87 # ifdef DEBUG
88 	if(debug){
89 		printf("Print %d states:\n",stnum+1);
90 		for(i=0;i<=stnum;i++)stprt(i);
91 		}
92 # endif
93 		/* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, schar, sname */
94 		/* may be gotten: verify, advance, stoff */
95 	free2core();
96 	get3core();
97 	layout();
98 		/* may be disposed of: verify, advance, stoff, nexts, nchar,
99 			gotof, atable, ccpackflg, sfall */
100 # ifdef DEBUG
101 	free3core();
102 # endif
103 	if (ZCH>NCH) cname="/usr/lib/lex/ebcform";
104 	fother = fopen(ratfor?ratname:cname,"r");
105 	if(fother == NULL)
106 		error("Lex driver missing, file %s",ratfor?ratname:cname);
107 	while ( (i=getc(fother)) != EOF)
108 		putc(i,fout);
109 
110 	fclose(fother);
111 	fclose(fout);
112 	if(
113 # ifdef DEBUG
114 		debug   ||
115 # endif
116 			report == 1)statistics();
117 	fclose(stdout);
118 	fclose(stderr);
119 	exit(0);	/* success return code */
120 	}
121 get1core(){
122 	register int i, val;
123 	register char *p;
124 ccptr =	ccl = myalloc(CCLSIZE,sizeof(*ccl));
125 pcptr = pchar = myalloc(pchlen, sizeof(*pchar));
126 	def = (char **) myalloc(DEFSIZE,sizeof(*def));
127 	subs = (char **) myalloc(DEFSIZE,sizeof(*subs));
128 dp =	dchar = myalloc(DEFCHAR,sizeof(*dchar));
129 	sname = (char **) myalloc(STARTSIZE,sizeof(*sname));
130 sp = 	schar = myalloc(STARTCHAR,sizeof(*schar));
131 	if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || schar == 0)
132 		error("Too little core to begin");
133 	}
134 free1core(){
135 	cfree(def,DEFSIZE,sizeof(*def));
136 	cfree(subs,DEFSIZE,sizeof(*subs));
137 	cfree(dchar,DEFCHAR,sizeof(*dchar));
138 	}
139 get2core(){
140 	register int i, val;
141 	register char *p;
142 	gotof = (int *) myalloc(nstates,sizeof(*gotof));
143 	nexts = (int *) myalloc(ntrans,sizeof(*nexts));
144 	nchar = myalloc(ntrans,sizeof(*nchar));
145 	state = (int **) myalloc(nstates,sizeof(*state));
146 	atable = (int *) myalloc(nstates,sizeof(*atable));
147 	sfall = (int *) myalloc(nstates,sizeof(*sfall));
148 	cpackflg = myalloc(nstates,sizeof(*cpackflg));
149 	tmpstat = myalloc(tptr+1,sizeof(*tmpstat));
150 	foll = (int **) myalloc(tptr+1,sizeof(*foll));
151 nxtpos = positions = (int *) myalloc(maxpos,sizeof(*positions));
152 	if(tmpstat == 0 || foll == 0 || positions == 0 ||
153 		gotof == 0 || nexts == 0 || nchar == 0 || state == 0 || atable == 0 || sfall == 0 || cpackflg == 0 )
154 		error("Too little core for state generation");
155 	for(i=0;i<=tptr;i++)foll[i] = 0;
156 	}
157 free2core(){
158 	cfree(positions,maxpos,sizeof(*positions));
159 	cfree(tmpstat,tptr+1,sizeof(*tmpstat));
160 	cfree(foll,tptr+1,sizeof(*foll));
161 	cfree(name,treesize,sizeof(*name));
162 	cfree(left,treesize,sizeof(*left));
163 	cfree(right,treesize,sizeof(*right));
164 	cfree(parent,treesize,sizeof(*parent));
165 	cfree(nullstr,treesize,sizeof(*nullstr));
166 	cfree(state,nstates,sizeof(*state));
167 	cfree(sname,STARTSIZE,sizeof(*sname));
168 	cfree(schar,STARTCHAR,sizeof(*schar));
169 	cfree(ccl,CCLSIZE,sizeof(*ccl));
170 	}
171 get3core(){
172 	register int i, val;
173 	register char *p;
174 	verify = (int *) myalloc(outsize,sizeof(*verify));
175 	advance = (int *) myalloc(outsize,sizeof(*advance));
176 	stoff = (int *) myalloc(stnum+2,sizeof(*stoff));
177 	if(verify == 0 || advance == 0 || stoff == 0)
178 		error("Too little core for final packing");
179 	}
180 # ifdef DEBUG
181 free3core(){
182 	cfree(advance,outsize,sizeof(*advance));
183 	cfree(verify,outsize,sizeof(*verify));
184 	cfree(stoff,stnum+1,sizeof(*stoff));
185 	cfree(gotof,nstates,sizeof(*gotof));
186 	cfree(nexts,ntrans,sizeof(*nexts));
187 	cfree(nchar,ntrans,sizeof(*nchar));
188 	cfree(atable,nstates,sizeof(*atable));
189 	cfree(sfall,nstates,sizeof(*sfall));
190 	cfree(cpackflg,nstates,sizeof(*cpackflg));
191 	}
192 # endif
193 char *myalloc(a,b)
194   int a,b; {
195 	register char *i;
196 	i = calloc(a, b);
197 	if(i==0)
198 		warning("OOPS - calloc returns a 0");
199 	else if(i == (char *)-1){
200 # ifdef DEBUG
201 		warning("calloc returns a -1");
202 # endif
203 		return(0);
204 		}
205 	return(i);
206 	}
207 # ifdef DEBUG
208 buserr(){
209 	fflush(errorf);
210 	fflush(fout);
211 	fflush(stdout);
212 	fprintf(errorf,"Bus error\n");
213 	if(report == 1)statistics();
214 	fflush(errorf);
215 	}
216 segviol(){
217 	fflush(errorf);
218 	fflush(fout);
219 	fflush(stdout);
220 	fprintf(errorf,"Segmentation violation\n");
221 	if(report == 1)statistics();
222 	fflush(errorf);
223 	}
224 # endif
225 
226 yyerror(s)
227 char *s;
228 {
229 	fprintf(stderr, "\"%s\", line %d: %s\n",
230 		fptr > 0 ? sargv[fptr] : "<stdin>", yyline, s);
231 }
232