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