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