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