grammar.y revision 1.12 1 1.12 dholland /* $NetBSD: grammar.y,v 1.12 2015/06/19 06:02:31 dholland 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.12 dholland __RCSID("$NetBSD: grammar.y,v 1.12 2015/06/19 06:02:31 dholland 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.1 cgd if (sp->beacon == NULL)
183 1.11 dholland sp->beacon = malloc((sp->num_beacons
184 1.1 cgd + REALLOC) * sizeof (BEACON));
185 1.1 cgd else
186 1.11 dholland sp->beacon = realloc(sp->beacon,
187 1.1 cgd (sp->num_beacons + REALLOC) *
188 1.1 cgd sizeof (BEACON));
189 1.1 cgd if (sp->beacon == NULL)
190 1.1 cgd return (yyerror("No memory available."));
191 1.1 cgd }
192 1.1 cgd sp->beacon[sp->num_beacons].x = $2;
193 1.1 cgd sp->beacon[sp->num_beacons].y = $3;
194 1.1 cgd check_point($2, $3);
195 1.1 cgd sp->num_beacons++;
196 1.1 cgd }
197 1.1 cgd ;
198 1.1 cgd
199 1.1 cgd Epoint_list:
200 1.1 cgd Epoint Epoint_list
201 1.1 cgd {}
202 1.1 cgd | Epoint
203 1.1 cgd {}
204 1.1 cgd ;
205 1.1 cgd
206 1.1 cgd Epoint:
207 1.1 cgd '(' ConstOp ConstOp DirOp ')'
208 1.1 cgd {
209 1.1 cgd int dir;
210 1.1 cgd
211 1.1 cgd if (sp->num_exits % REALLOC == 0) {
212 1.1 cgd if (sp->exit == NULL)
213 1.11 dholland sp->exit = malloc((sp->num_exits +
214 1.1 cgd REALLOC) * sizeof (EXIT));
215 1.1 cgd else
216 1.11 dholland sp->exit = realloc(sp->exit,
217 1.1 cgd (sp->num_exits + REALLOC) *
218 1.1 cgd sizeof (EXIT));
219 1.1 cgd if (sp->exit == NULL)
220 1.1 cgd return (yyerror("No memory available."));
221 1.1 cgd }
222 1.1 cgd dir = dir_no($4);
223 1.1 cgd sp->exit[sp->num_exits].x = $2;
224 1.1 cgd sp->exit[sp->num_exits].y = $3;
225 1.1 cgd sp->exit[sp->num_exits].dir = dir;
226 1.1 cgd check_edge($2, $3);
227 1.1 cgd check_edir($2, $3, dir);
228 1.1 cgd sp->num_exits++;
229 1.1 cgd }
230 1.1 cgd ;
231 1.1 cgd
232 1.1 cgd Apoint_list:
233 1.1 cgd Apoint Apoint_list
234 1.1 cgd {}
235 1.1 cgd | Apoint
236 1.1 cgd {}
237 1.1 cgd ;
238 1.1 cgd
239 1.1 cgd Apoint:
240 1.1 cgd '(' ConstOp ConstOp DirOp ')'
241 1.1 cgd {
242 1.1 cgd int dir;
243 1.1 cgd
244 1.1 cgd if (sp->num_airports % REALLOC == 0) {
245 1.1 cgd if (sp->airport == NULL)
246 1.11 dholland sp->airport = malloc((sp->num_airports
247 1.1 cgd + REALLOC) * sizeof(AIRPORT));
248 1.1 cgd else
249 1.11 dholland sp->airport = realloc(sp->airport,
250 1.1 cgd (sp->num_airports + REALLOC) *
251 1.1 cgd sizeof(AIRPORT));
252 1.1 cgd if (sp->airport == NULL)
253 1.1 cgd return (yyerror("No memory available."));
254 1.1 cgd }
255 1.1 cgd dir = dir_no($4);
256 1.1 cgd sp->airport[sp->num_airports].x = $2;
257 1.1 cgd sp->airport[sp->num_airports].y = $3;
258 1.1 cgd sp->airport[sp->num_airports].dir = dir;
259 1.1 cgd check_point($2, $3);
260 1.1 cgd sp->num_airports++;
261 1.1 cgd }
262 1.1 cgd ;
263 1.1 cgd
264 1.1 cgd Lline_list:
265 1.1 cgd Lline Lline_list
266 1.1 cgd {}
267 1.1 cgd | Lline
268 1.1 cgd {}
269 1.1 cgd ;
270 1.1 cgd
271 1.1 cgd Lline:
272 1.1 cgd '[' '(' ConstOp ConstOp ')' '(' ConstOp ConstOp ')' ']'
273 1.1 cgd {
274 1.1 cgd if (sp->num_lines % REALLOC == 0) {
275 1.1 cgd if (sp->line == NULL)
276 1.11 dholland sp->line = malloc((sp->num_lines +
277 1.1 cgd REALLOC) * sizeof (LINE));
278 1.1 cgd else
279 1.11 dholland sp->line = realloc(sp->line,
280 1.1 cgd (sp->num_lines + REALLOC) *
281 1.1 cgd sizeof (LINE));
282 1.1 cgd if (sp->line == NULL)
283 1.1 cgd return (yyerror("No memory available."));
284 1.1 cgd }
285 1.1 cgd sp->line[sp->num_lines].p1.x = $3;
286 1.1 cgd sp->line[sp->num_lines].p1.y = $4;
287 1.1 cgd sp->line[sp->num_lines].p2.x = $7;
288 1.1 cgd sp->line[sp->num_lines].p2.y = $8;
289 1.1 cgd check_line($3, $4, $7, $8);
290 1.1 cgd sp->num_lines++;
291 1.1 cgd }
292 1.1 cgd ;
293 1.1 cgd %%
294 1.1 cgd
295 1.10 dholland static void
296 1.9 jmc check_edge(int x, int y)
297 1.1 cgd {
298 1.1 cgd if (!(x == 0) && !(x == sp->width - 1) &&
299 1.1 cgd !(y == 0) && !(y == sp->height - 1))
300 1.1 cgd yyerror("edge value not on edge.");
301 1.1 cgd }
302 1.1 cgd
303 1.10 dholland static void
304 1.9 jmc check_point(int x, int y)
305 1.1 cgd {
306 1.1 cgd if (x < 1 || x >= sp->width - 1)
307 1.1 cgd yyerror("X value out of range.");
308 1.1 cgd if (y < 1 || y >= sp->height - 1)
309 1.1 cgd yyerror("Y value out of range.");
310 1.1 cgd }
311 1.1 cgd
312 1.10 dholland static void
313 1.9 jmc check_linepoint(int x, int y)
314 1.1 cgd {
315 1.1 cgd if (x < 0 || x >= sp->width)
316 1.1 cgd yyerror("X value out of range.");
317 1.1 cgd if (y < 0 || y >= sp->height)
318 1.1 cgd yyerror("Y value out of range.");
319 1.1 cgd }
320 1.1 cgd
321 1.10 dholland static void
322 1.9 jmc check_line(int px1, int py1, int px2, int py2)
323 1.1 cgd {
324 1.1 cgd int d1, d2;
325 1.1 cgd
326 1.9 jmc check_linepoint(px1, py1);
327 1.9 jmc check_linepoint(px2, py2);
328 1.1 cgd
329 1.9 jmc d1 = ABS(px2 - px1);
330 1.9 jmc d2 = ABS(py2 - py1);
331 1.1 cgd
332 1.1 cgd if (!(d1 == d2) && !(d1 == 0) && !(d2 == 0))
333 1.1 cgd yyerror("Bad line endpoints.");
334 1.1 cgd }
335 1.1 cgd
336 1.10 dholland static int
337 1.9 jmc yyerror(const char *s)
338 1.1 cgd {
339 1.9 jmc fprintf(stderr, "\"%s\": line %d: %s\n", filename, line, s);
340 1.1 cgd errors++;
341 1.1 cgd
342 1.1 cgd return (errors);
343 1.1 cgd }
344 1.1 cgd
345 1.10 dholland static void
346 1.9 jmc check_edir(int x, int y, int dir)
347 1.1 cgd {
348 1.1 cgd int bad = 0;
349 1.1 cgd
350 1.1 cgd if (x == sp->width - 1)
351 1.1 cgd x = 2;
352 1.1 cgd else if (x != 0)
353 1.1 cgd x = 1;
354 1.1 cgd if (y == sp->height - 1)
355 1.1 cgd y = 2;
356 1.1 cgd else if (y != 0)
357 1.1 cgd y = 1;
358 1.1 cgd
359 1.1 cgd switch (x * 10 + y) {
360 1.1 cgd case 00: if (dir != 3) bad++; break;
361 1.1 cgd case 01: if (dir < 1 || dir > 3) bad++; break;
362 1.1 cgd case 02: if (dir != 1) bad++; break;
363 1.1 cgd case 10: if (dir < 3 || dir > 5) bad++; break;
364 1.1 cgd case 11: break;
365 1.1 cgd case 12: if (dir > 1 && dir < 7) bad++; break;
366 1.1 cgd case 20: if (dir != 5) bad++; break;
367 1.1 cgd case 21: if (dir < 5) bad++; break;
368 1.1 cgd case 22: if (dir != 7) bad++; break;
369 1.1 cgd default:
370 1.1 cgd yyerror("Unknown value in checkdir! Get help!");
371 1.1 cgd break;
372 1.1 cgd }
373 1.1 cgd if (bad)
374 1.1 cgd yyerror("Bad direction for entrance at exit.");
375 1.1 cgd }
376 1.1 cgd
377 1.10 dholland static int
378 1.9 jmc checkdefs(void)
379 1.1 cgd {
380 1.9 jmc int error = 0;
381 1.1 cgd
382 1.1 cgd if (sp->width == 0) {
383 1.1 cgd yyerror("'width' undefined.");
384 1.9 jmc error++;
385 1.1 cgd }
386 1.1 cgd if (sp->height == 0) {
387 1.1 cgd yyerror("'height' undefined.");
388 1.9 jmc error++;
389 1.1 cgd }
390 1.1 cgd if (sp->update_secs == 0) {
391 1.1 cgd yyerror("'update' undefined.");
392 1.9 jmc error++;
393 1.1 cgd }
394 1.1 cgd if (sp->newplane_time == 0) {
395 1.1 cgd yyerror("'newplane' undefined.");
396 1.9 jmc error++;
397 1.1 cgd }
398 1.9 jmc if (error)
399 1.1 cgd return (-1);
400 1.1 cgd else
401 1.1 cgd return (0);
402 1.1 cgd }
403