grammar.y revision 1.13 1 1.13 nia /* $NetBSD: grammar.y,v 1.13 2021/10/29 11:44:22 nia Exp $ */
2 1.3 cgd
3 1.1 cgd /*-
4 1.3 cgd * Copyright (c) 1990, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Ed James.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.8 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.1 cgd /*
36 1.1 cgd * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
37 1.1 cgd *
38 1.1 cgd * Copy permission is hereby granted provided that this notice is
39 1.1 cgd * retained on all partial or complete copies.
40 1.1 cgd *
41 1.1 cgd * For more info on this and all of my stuff, mail edjames (at) berkeley.edu.
42 1.1 cgd */
43 1.1 cgd
44 1.1 cgd %token <ival> HeightOp
45 1.1 cgd %token <ival> WidthOp
46 1.1 cgd %token <ival> UpdateOp
47 1.1 cgd %token <ival> NewplaneOp
48 1.1 cgd %token <cval> DirOp
49 1.1 cgd %token <ival> ConstOp
50 1.1 cgd %token <ival> LineOp
51 1.1 cgd %token <ival> AirportOp
52 1.1 cgd %token <ival> BeaconOp
53 1.1 cgd %token <ival> ExitOp
54 1.1 cgd %union {
55 1.1 cgd int ival;
56 1.1 cgd char cval;
57 1.1 cgd }
58 1.1 cgd
59 1.1 cgd %{
60 1.4 lukem #include <sys/cdefs.h>
61 1.1 cgd #ifndef lint
62 1.3 cgd #if 0
63 1.3 cgd static char sccsid[] = "@(#)grammar.y 8.1 (Berkeley) 5/31/93";
64 1.3 cgd #else
65 1.13 nia __RCSID("$NetBSD: grammar.y,v 1.13 2021/10/29 11:44:22 nia Exp $");
66 1.3 cgd #endif
67 1.1 cgd #endif /* not lint */
68 1.1 cgd
69 1.12 dholland #include <stdio.h>
70 1.12 dholland
71 1.12 dholland #include "def.h"
72 1.12 dholland #include "struct.h"
73 1.12 dholland #include "extern.h"
74 1.12 dholland #include "tunable.h"
75 1.12 dholland
76 1.10 dholland int line = 1;
77 1.10 dholland
78 1.10 dholland static int errors = 0;
79 1.10 dholland
80 1.10 dholland static int yyerror(const char *);
81 1.1 cgd %}
82 1.1 cgd
83 1.1 cgd %%
84 1.1 cgd file:
85 1.1 cgd bunch_of_defs { if (checkdefs() < 0) return (errors); } bunch_of_lines
86 1.1 cgd {
87 1.1 cgd if (sp->num_exits + sp->num_airports < 2)
88 1.1 cgd yyerror("Need at least 2 airports and/or exits.");
89 1.1 cgd return (errors);
90 1.1 cgd }
91 1.1 cgd ;
92 1.1 cgd
93 1.1 cgd bunch_of_defs:
94 1.1 cgd def bunch_of_defs
95 1.1 cgd | def
96 1.1 cgd ;
97 1.1 cgd
98 1.1 cgd def:
99 1.1 cgd udef
100 1.1 cgd | ndef
101 1.1 cgd | wdef
102 1.1 cgd | hdef
103 1.1 cgd ;
104 1.1 cgd
105 1.1 cgd udef:
106 1.1 cgd UpdateOp '=' ConstOp ';'
107 1.1 cgd {
108 1.1 cgd if (sp->update_secs != 0)
109 1.1 cgd return (yyerror("Redefinition of 'update'."));
110 1.1 cgd else if ($3 < 1)
111 1.1 cgd return (yyerror("'update' is too small."));
112 1.1 cgd else
113 1.1 cgd sp->update_secs = $3;
114 1.1 cgd }
115 1.1 cgd ;
116 1.1 cgd
117 1.1 cgd ndef:
118 1.1 cgd NewplaneOp '=' ConstOp ';'
119 1.1 cgd {
120 1.1 cgd if (sp->newplane_time != 0)
121 1.1 cgd return (yyerror("Redefinition of 'newplane'."));
122 1.1 cgd else if ($3 < 1)
123 1.1 cgd return (yyerror("'newplane' is too small."));
124 1.1 cgd else
125 1.1 cgd sp->newplane_time = $3;
126 1.1 cgd }
127 1.1 cgd ;
128 1.1 cgd
129 1.1 cgd hdef:
130 1.1 cgd HeightOp '=' ConstOp ';'
131 1.1 cgd {
132 1.1 cgd if (sp->height != 0)
133 1.1 cgd return (yyerror("Redefinition of 'height'."));
134 1.1 cgd else if ($3 < 3)
135 1.1 cgd return (yyerror("'height' is too small."));
136 1.1 cgd else
137 1.1 cgd sp->height = $3;
138 1.1 cgd }
139 1.1 cgd ;
140 1.1 cgd
141 1.1 cgd wdef:
142 1.1 cgd WidthOp '=' ConstOp ';'
143 1.1 cgd {
144 1.5 hubertf if (sp->width != 0)
145 1.1 cgd return (yyerror("Redefinition of 'width'."));
146 1.1 cgd else if ($3 < 3)
147 1.1 cgd return (yyerror("'width' is too small."));
148 1.1 cgd else
149 1.1 cgd sp->width = $3;
150 1.1 cgd }
151 1.1 cgd ;
152 1.1 cgd
153 1.1 cgd bunch_of_lines:
154 1.1 cgd line bunch_of_lines
155 1.1 cgd {}
156 1.1 cgd | line
157 1.1 cgd {}
158 1.1 cgd ;
159 1.1 cgd
160 1.1 cgd line:
161 1.1 cgd BeaconOp ':' Bpoint_list ';'
162 1.1 cgd {}
163 1.1 cgd | ExitOp ':' Epoint_list ';'
164 1.1 cgd {}
165 1.1 cgd | LineOp ':' Lline_list ';'
166 1.1 cgd {}
167 1.1 cgd | AirportOp ':' Apoint_list ';'
168 1.1 cgd {}
169 1.1 cgd ;
170 1.1 cgd
171 1.1 cgd Bpoint_list:
172 1.1 cgd Bpoint Bpoint_list
173 1.1 cgd {}
174 1.1 cgd | Bpoint
175 1.1 cgd {}
176 1.1 cgd ;
177 1.1 cgd
178 1.1 cgd Bpoint:
179 1.1 cgd '(' ConstOp ConstOp ')'
180 1.1 cgd {
181 1.1 cgd if (sp->num_beacons % REALLOC == 0) {
182 1.13 nia if (reallocarr(&sp->beacon,
183 1.13 nia sp->num_beacons + REALLOC, sizeof(BEACON)) != 0)
184 1.1 cgd return (yyerror("No memory available."));
185 1.1 cgd }
186 1.1 cgd sp->beacon[sp->num_beacons].x = $2;
187 1.1 cgd sp->beacon[sp->num_beacons].y = $3;
188 1.1 cgd check_point($2, $3);
189 1.1 cgd sp->num_beacons++;
190 1.1 cgd }
191 1.1 cgd ;
192 1.1 cgd
193 1.1 cgd Epoint_list:
194 1.1 cgd Epoint Epoint_list
195 1.1 cgd {}
196 1.1 cgd | Epoint
197 1.1 cgd {}
198 1.1 cgd ;
199 1.1 cgd
200 1.1 cgd Epoint:
201 1.1 cgd '(' ConstOp ConstOp DirOp ')'
202 1.1 cgd {
203 1.1 cgd int dir;
204 1.1 cgd
205 1.1 cgd if (sp->num_exits % REALLOC == 0) {
206 1.1 cgd if (sp->exit == NULL)
207 1.11 dholland sp->exit = malloc((sp->num_exits +
208 1.1 cgd REALLOC) * sizeof (EXIT));
209 1.1 cgd else
210 1.11 dholland sp->exit = realloc(sp->exit,
211 1.1 cgd (sp->num_exits + REALLOC) *
212 1.1 cgd sizeof (EXIT));
213 1.1 cgd if (sp->exit == NULL)
214 1.1 cgd return (yyerror("No memory available."));
215 1.1 cgd }
216 1.1 cgd dir = dir_no($4);
217 1.1 cgd sp->exit[sp->num_exits].x = $2;
218 1.1 cgd sp->exit[sp->num_exits].y = $3;
219 1.1 cgd sp->exit[sp->num_exits].dir = dir;
220 1.1 cgd check_edge($2, $3);
221 1.1 cgd check_edir($2, $3, dir);
222 1.1 cgd sp->num_exits++;
223 1.1 cgd }
224 1.1 cgd ;
225 1.1 cgd
226 1.1 cgd Apoint_list:
227 1.1 cgd Apoint Apoint_list
228 1.1 cgd {}
229 1.1 cgd | Apoint
230 1.1 cgd {}
231 1.1 cgd ;
232 1.1 cgd
233 1.1 cgd Apoint:
234 1.1 cgd '(' ConstOp ConstOp DirOp ')'
235 1.1 cgd {
236 1.1 cgd int dir;
237 1.1 cgd
238 1.1 cgd if (sp->num_airports % REALLOC == 0) {
239 1.1 cgd if (sp->airport == NULL)
240 1.11 dholland sp->airport = malloc((sp->num_airports
241 1.1 cgd + REALLOC) * sizeof(AIRPORT));
242 1.1 cgd else
243 1.11 dholland sp->airport = realloc(sp->airport,
244 1.1 cgd (sp->num_airports + REALLOC) *
245 1.1 cgd sizeof(AIRPORT));
246 1.1 cgd if (sp->airport == NULL)
247 1.1 cgd return (yyerror("No memory available."));
248 1.1 cgd }
249 1.1 cgd dir = dir_no($4);
250 1.1 cgd sp->airport[sp->num_airports].x = $2;
251 1.1 cgd sp->airport[sp->num_airports].y = $3;
252 1.1 cgd sp->airport[sp->num_airports].dir = dir;
253 1.1 cgd check_point($2, $3);
254 1.1 cgd sp->num_airports++;
255 1.1 cgd }
256 1.1 cgd ;
257 1.1 cgd
258 1.1 cgd Lline_list:
259 1.1 cgd Lline Lline_list
260 1.1 cgd {}
261 1.1 cgd | Lline
262 1.1 cgd {}
263 1.1 cgd ;
264 1.1 cgd
265 1.1 cgd Lline:
266 1.1 cgd '[' '(' ConstOp ConstOp ')' '(' ConstOp ConstOp ')' ']'
267 1.1 cgd {
268 1.1 cgd if (sp->num_lines % REALLOC == 0) {
269 1.1 cgd if (sp->line == NULL)
270 1.11 dholland sp->line = malloc((sp->num_lines +
271 1.1 cgd REALLOC) * sizeof (LINE));
272 1.1 cgd else
273 1.11 dholland sp->line = realloc(sp->line,
274 1.1 cgd (sp->num_lines + REALLOC) *
275 1.1 cgd sizeof (LINE));
276 1.1 cgd if (sp->line == NULL)
277 1.1 cgd return (yyerror("No memory available."));
278 1.1 cgd }
279 1.1 cgd sp->line[sp->num_lines].p1.x = $3;
280 1.1 cgd sp->line[sp->num_lines].p1.y = $4;
281 1.1 cgd sp->line[sp->num_lines].p2.x = $7;
282 1.1 cgd sp->line[sp->num_lines].p2.y = $8;
283 1.1 cgd check_line($3, $4, $7, $8);
284 1.1 cgd sp->num_lines++;
285 1.1 cgd }
286 1.1 cgd ;
287 1.1 cgd %%
288 1.1 cgd
289 1.10 dholland static void
290 1.9 jmc check_edge(int x, int y)
291 1.1 cgd {
292 1.1 cgd if (!(x == 0) && !(x == sp->width - 1) &&
293 1.1 cgd !(y == 0) && !(y == sp->height - 1))
294 1.1 cgd yyerror("edge value not on edge.");
295 1.1 cgd }
296 1.1 cgd
297 1.10 dholland static void
298 1.9 jmc check_point(int x, int y)
299 1.1 cgd {
300 1.1 cgd if (x < 1 || x >= sp->width - 1)
301 1.1 cgd yyerror("X value out of range.");
302 1.1 cgd if (y < 1 || y >= sp->height - 1)
303 1.1 cgd yyerror("Y value out of range.");
304 1.1 cgd }
305 1.1 cgd
306 1.10 dholland static void
307 1.9 jmc check_linepoint(int x, int y)
308 1.1 cgd {
309 1.1 cgd if (x < 0 || x >= sp->width)
310 1.1 cgd yyerror("X value out of range.");
311 1.1 cgd if (y < 0 || y >= sp->height)
312 1.1 cgd yyerror("Y value out of range.");
313 1.1 cgd }
314 1.1 cgd
315 1.10 dholland static void
316 1.9 jmc check_line(int px1, int py1, int px2, int py2)
317 1.1 cgd {
318 1.1 cgd int d1, d2;
319 1.1 cgd
320 1.9 jmc check_linepoint(px1, py1);
321 1.9 jmc check_linepoint(px2, py2);
322 1.1 cgd
323 1.9 jmc d1 = ABS(px2 - px1);
324 1.9 jmc d2 = ABS(py2 - py1);
325 1.1 cgd
326 1.1 cgd if (!(d1 == d2) && !(d1 == 0) && !(d2 == 0))
327 1.1 cgd yyerror("Bad line endpoints.");
328 1.1 cgd }
329 1.1 cgd
330 1.10 dholland static int
331 1.9 jmc yyerror(const char *s)
332 1.1 cgd {
333 1.9 jmc fprintf(stderr, "\"%s\": line %d: %s\n", filename, line, s);
334 1.1 cgd errors++;
335 1.1 cgd
336 1.1 cgd return (errors);
337 1.1 cgd }
338 1.1 cgd
339 1.10 dholland static void
340 1.9 jmc check_edir(int x, int y, int dir)
341 1.1 cgd {
342 1.1 cgd int bad = 0;
343 1.1 cgd
344 1.1 cgd if (x == sp->width - 1)
345 1.1 cgd x = 2;
346 1.1 cgd else if (x != 0)
347 1.1 cgd x = 1;
348 1.1 cgd if (y == sp->height - 1)
349 1.1 cgd y = 2;
350 1.1 cgd else if (y != 0)
351 1.1 cgd y = 1;
352 1.1 cgd
353 1.1 cgd switch (x * 10 + y) {
354 1.1 cgd case 00: if (dir != 3) bad++; break;
355 1.1 cgd case 01: if (dir < 1 || dir > 3) bad++; break;
356 1.1 cgd case 02: if (dir != 1) bad++; break;
357 1.1 cgd case 10: if (dir < 3 || dir > 5) bad++; break;
358 1.1 cgd case 11: break;
359 1.1 cgd case 12: if (dir > 1 && dir < 7) bad++; break;
360 1.1 cgd case 20: if (dir != 5) bad++; break;
361 1.1 cgd case 21: if (dir < 5) bad++; break;
362 1.1 cgd case 22: if (dir != 7) bad++; break;
363 1.1 cgd default:
364 1.1 cgd yyerror("Unknown value in checkdir! Get help!");
365 1.1 cgd break;
366 1.1 cgd }
367 1.1 cgd if (bad)
368 1.1 cgd yyerror("Bad direction for entrance at exit.");
369 1.1 cgd }
370 1.1 cgd
371 1.10 dholland static int
372 1.9 jmc checkdefs(void)
373 1.1 cgd {
374 1.9 jmc int error = 0;
375 1.1 cgd
376 1.1 cgd if (sp->width == 0) {
377 1.1 cgd yyerror("'width' undefined.");
378 1.9 jmc error++;
379 1.1 cgd }
380 1.1 cgd if (sp->height == 0) {
381 1.1 cgd yyerror("'height' undefined.");
382 1.9 jmc error++;
383 1.1 cgd }
384 1.1 cgd if (sp->update_secs == 0) {
385 1.1 cgd yyerror("'update' undefined.");
386 1.9 jmc error++;
387 1.1 cgd }
388 1.1 cgd if (sp->newplane_time == 0) {
389 1.1 cgd yyerror("'newplane' undefined.");
390 1.9 jmc error++;
391 1.1 cgd }
392 1.9 jmc if (error)
393 1.1 cgd return (-1);
394 1.1 cgd else
395 1.1 cgd return (0);
396 1.1 cgd }
397