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