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