xref: /original-bsd/usr.bin/pascal/src/yyparse.c (revision a9157423)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 #ifndef lint
4 static char sccsid[] = "@(#)yyparse.c 1.3 08/19/83";
5 #endif
6 
7 #include "whoami.h"
8 #include "0.h"
9 #include "tree_ty.h"	/* must be included for yy.h */
10 #include "yy.h"
11 
12 /*
13  * Parser for 'yacc' output.
14  * Specifially Modified for Berkeley Pascal
15  */
16 
17 int	yystate;	/* Current parser state */
18 union semstack *yypv;
19 unsigned yytshifts = 1;	/* Number of "true" shifts */
20 
21 /*
22  * Parse Tables
23  */
24 int	yygo[];
25 int	yypgo[];
26 int	yyr1[];
27 int	yyr2[];
28 int	yyact[];
29 int	yypact[];
30 
31 /*
32  * Parse and parallel semantic stack
33  */
34 union semstack	yyv[MAXDEPTH];
35 int	yys[MAXDEPTH];
36 
37 /*
38  * This routine parses the input stream, and
39  * returns if it accepts, or if an unrecoverable syntax
40  * error is encountered.
41  */
42 yyparse()
43 {
44 	register int *ps, n, *p;
45 	int paniced, *panicps, idfail;
46 
47 #ifdef lint
48 	panicps = (int *) 0;
49 #endif
50 	yystate = 0;
51 	yychar = yylex();
52 	OY.Yychar = -1;
53 	yyshifts = 3;
54 	paniced = 0;
55 	ps = &yys[0]-1;
56 	yypv = &yyv[0]-1;
57 #ifdef PXP
58 	yypw = &yyw[0]-1;
59 #endif
60 
61 stack:
62 	/*
63 	 * Push new state and value.
64 	 */
65 	if (yypv >= &yyv[MAXDEPTH-1]) {
66 		yerror("Parse stack overflow");
67 		pexit(DIED);
68 	}
69 	*++ps = yystate;
70 	*++yypv = yyval;
71 #ifdef PXP
72 	yypw++;
73 #endif
74 newstate:
75 	/*
76 	 * Locate parsing actions for the
77 	 * new parser state.
78 	 */
79 	p = &yyact[ yypact[yystate+1] ];
80 	/*
81 	 * Search the parse actions table
82 	 * for something useful to do.
83 	 * While n is non-positive, it is the negation
84 	 * of the token we are testing for.
85 	 */
86 #ifdef PI
87 	if ((n = *p++) <= 0) {
88 		if (yychar < 0)
89 			yychar = yylex();
90 		do
91 			if ((n += yychar) != 0)
92 				p++;
93 		while ((n = *p++) <= 0);
94 	}
95 #else
96 	while ((n = *p++) <= 0)
97 		if ((n += yychar) != 0)
98 			p++;
99 #endif
100 	switch (n >> 12) {
101 
102 		/*
103 		 * Shift.
104 		 */
105 		case 2:
106 #ifdef PXP
107 			yypw[1].Wseqid = yyseqid;
108 			yypw[1].Wcol = yycol;
109 #endif
110 			OYcopy();
111 			yystate = n & 07777;
112 			yyval.i_entry = yylval;
113 #ifdef PI
114 			yychar = -1;
115 #else
116 			yychar = yylex();
117 #endif
118 			yyshifts++;
119 			yytshifts++;
120 			goto stack;
121 
122 		/*
123 		 * Reduce.
124 		 */
125 		case 3:
126 			n &= 07777;
127 			N = yyr2[n];
128 			if (N == 1 && OY.Yychar == YID && !yyEactr(n,
129 							yypv[0].cptr)) {
130 				idfail = 1;
131 				goto errin;
132 			}
133 			OY.Yychar = -1;
134 			ps -= N;
135 			yypv -= N;
136 #ifdef PXP
137 			yypw -= N;
138 #endif
139 			yyval = yypv[1];
140 			yyactr(n);
141 			/*
142 			 * Use goto table to find next state.
143 			 */
144 			p = &yygo[yypgo[yyr1[n]]];
145 			while (*p != *ps && *p >= 0)
146 				p += 2;
147 			yystate = p[1];
148 			goto stack;
149 
150 		/*
151 		 * Accept.
152 		 */
153 		case 4:
154 			return;
155 
156 		/*
157 		 * Error.
158 		 */
159 		case 1:
160 			idfail = 0;
161 errin:
162 			if ((paniced || yyshifts != 0) && yyrecover(ps, idfail)) {
163 				paniced = 0;
164 				ps = Ps;
165 				yystate = *ps;
166 				goto newstate;
167 			}
168 			/*
169 			 * Find a state where 'error' is a
170 			 * legal shift action.
171 			 */
172 			if (paniced && yyshifts <= 0 && ps >= panicps) {
173 				yypv -= (ps - panicps) + 1;
174 #ifdef PXP
175 				yypw -= (ps - panicps) + 1;
176 #endif
177 				ps = panicps - 1;
178 			}
179 			while (ps >= yys) {
180 				for (p = &yyact[ yypact[*ps+1] ] ; *p <= 0; p += 2)
181 					if (*p == -256) {
182 						panicps = ps;
183 						yystate= p[1] & 07777;
184 						yyOshifts = yyshifts;
185 						yyshifts = 0;
186 						paniced = 1;
187 						goto stack;
188 					}
189 				--ps;
190 				--yypv;
191 #ifdef PXP
192 				--yypw;
193 #endif
194 #ifdef PI
195 				if (OY.Yychar != YID)
196 					syneflg = TRUE;
197 #endif
198 				OY.Yychar = -1;
199 			}
200 			if (yychar == YEOF)
201 				yyunexeof();
202 			if (yystate == 1)
203 				yyexeof();
204 			yerror("Unrecoverable syntax error - QUIT");
205 			return;
206 	}
207 	panic("yyparse");
208 }
209