grammar.y revision 1.11 1 1.11 dholland /* $NetBSD: grammar.y,v 1.11 2014/03/22 22:09:14 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.1 cgd #include "include.h"
61 1.1 cgd
62 1.4 lukem #include <sys/cdefs.h>
63 1.1 cgd #ifndef lint
64 1.3 cgd #if 0
65 1.3 cgd static char sccsid[] = "@(#)grammar.y 8.1 (Berkeley) 5/31/93";
66 1.3 cgd #else
67 1.11 dholland __RCSID("$NetBSD: grammar.y,v 1.11 2014/03/22 22:09:14 dholland Exp $");
68 1.3 cgd #endif
69 1.1 cgd #endif /* not lint */
70 1.1 cgd
71 1.10 dholland int line = 1;
72 1.10 dholland
73 1.10 dholland static int errors = 0;
74 1.10 dholland
75 1.10 dholland static int yyerror(const char *);
76 1.1 cgd %}
77 1.1 cgd
78 1.1 cgd %%
79 1.1 cgd file:
80 1.1 cgd bunch_of_defs { if (checkdefs() < 0) return (errors); } bunch_of_lines
81 1.1 cgd {
82 1.1 cgd if (sp->num_exits + sp->num_airports < 2)
83 1.1 cgd yyerror("Need at least 2 airports and/or exits.");
84 1.1 cgd return (errors);
85 1.1 cgd }
86 1.1 cgd ;
87 1.1 cgd
88 1.1 cgd bunch_of_defs:
89 1.1 cgd def bunch_of_defs
90 1.1 cgd | def
91 1.1 cgd ;
92 1.1 cgd
93 1.1 cgd def:
94 1.1 cgd udef
95 1.1 cgd | ndef
96 1.1 cgd | wdef
97 1.1 cgd | hdef
98 1.1 cgd ;
99 1.1 cgd
100 1.1 cgd udef:
101 1.1 cgd UpdateOp '=' ConstOp ';'
102 1.1 cgd {
103 1.1 cgd if (sp->update_secs != 0)
104 1.1 cgd return (yyerror("Redefinition of 'update'."));
105 1.1 cgd else if ($3 < 1)
106 1.1 cgd return (yyerror("'update' is too small."));
107 1.1 cgd else
108 1.1 cgd sp->update_secs = $3;
109 1.1 cgd }
110 1.1 cgd ;
111 1.1 cgd
112 1.1 cgd ndef:
113 1.1 cgd NewplaneOp '=' ConstOp ';'
114 1.1 cgd {
115 1.1 cgd if (sp->newplane_time != 0)
116 1.1 cgd return (yyerror("Redefinition of 'newplane'."));
117 1.1 cgd else if ($3 < 1)
118 1.1 cgd return (yyerror("'newplane' is too small."));
119 1.1 cgd else
120 1.1 cgd sp->newplane_time = $3;
121 1.1 cgd }
122 1.1 cgd ;
123 1.1 cgd
124 1.1 cgd hdef:
125 1.1 cgd HeightOp '=' ConstOp ';'
126 1.1 cgd {
127 1.1 cgd if (sp->height != 0)
128 1.1 cgd return (yyerror("Redefinition of 'height'."));
129 1.1 cgd else if ($3 < 3)
130 1.1 cgd return (yyerror("'height' is too small."));
131 1.1 cgd else
132 1.1 cgd sp->height = $3;
133 1.1 cgd }
134 1.1 cgd ;
135 1.1 cgd
136 1.1 cgd wdef:
137 1.1 cgd WidthOp '=' ConstOp ';'
138 1.1 cgd {
139 1.5 hubertf if (sp->width != 0)
140 1.1 cgd return (yyerror("Redefinition of 'width'."));
141 1.1 cgd else if ($3 < 3)
142 1.1 cgd return (yyerror("'width' is too small."));
143 1.1 cgd else
144 1.1 cgd sp->width = $3;
145 1.1 cgd }
146 1.1 cgd ;
147 1.1 cgd
148 1.1 cgd bunch_of_lines:
149 1.1 cgd line bunch_of_lines
150 1.1 cgd {}
151 1.1 cgd | line
152 1.1 cgd {}
153 1.1 cgd ;
154 1.1 cgd
155 1.1 cgd line:
156 1.1 cgd BeaconOp ':' Bpoint_list ';'
157 1.1 cgd {}
158 1.1 cgd | ExitOp ':' Epoint_list ';'
159 1.1 cgd {}
160 1.1 cgd | LineOp ':' Lline_list ';'
161 1.1 cgd {}
162 1.1 cgd | AirportOp ':' Apoint_list ';'
163 1.1 cgd {}
164 1.1 cgd ;
165 1.1 cgd
166 1.1 cgd Bpoint_list:
167 1.1 cgd Bpoint Bpoint_list
168 1.1 cgd {}
169 1.1 cgd | Bpoint
170 1.1 cgd {}
171 1.1 cgd ;
172 1.1 cgd
173 1.1 cgd Bpoint:
174 1.1 cgd '(' ConstOp ConstOp ')'
175 1.1 cgd {
176 1.1 cgd if (sp->num_beacons % REALLOC == 0) {
177 1.1 cgd if (sp->beacon == NULL)
178 1.11 dholland sp->beacon = malloc((sp->num_beacons
179 1.1 cgd + REALLOC) * sizeof (BEACON));
180 1.1 cgd else
181 1.11 dholland sp->beacon = realloc(sp->beacon,
182 1.1 cgd (sp->num_beacons + REALLOC) *
183 1.1 cgd sizeof (BEACON));
184 1.1 cgd if (sp->beacon == NULL)
185 1.1 cgd return (yyerror("No memory available."));
186 1.1 cgd }
187 1.1 cgd sp->beacon[sp->num_beacons].x = $2;
188 1.1 cgd sp->beacon[sp->num_beacons].y = $3;
189 1.1 cgd check_point($2, $3);
190 1.1 cgd sp->num_beacons++;
191 1.1 cgd }
192 1.1 cgd ;
193 1.1 cgd
194 1.1 cgd Epoint_list:
195 1.1 cgd Epoint Epoint_list
196 1.1 cgd {}
197 1.1 cgd | Epoint
198 1.1 cgd {}
199 1.1 cgd ;
200 1.1 cgd
201 1.1 cgd Epoint:
202 1.1 cgd '(' ConstOp ConstOp DirOp ')'
203 1.1 cgd {
204 1.1 cgd int dir;
205 1.1 cgd
206 1.1 cgd if (sp->num_exits % REALLOC == 0) {
207 1.1 cgd if (sp->exit == NULL)
208 1.11 dholland sp->exit = malloc((sp->num_exits +
209 1.1 cgd REALLOC) * sizeof (EXIT));
210 1.1 cgd else
211 1.11 dholland sp->exit = realloc(sp->exit,
212 1.1 cgd (sp->num_exits + REALLOC) *
213 1.1 cgd sizeof (EXIT));
214 1.1 cgd if (sp->exit == NULL)
215 1.1 cgd return (yyerror("No memory available."));
216 1.1 cgd }
217 1.1 cgd dir = dir_no($4);
218 1.1 cgd sp->exit[sp->num_exits].x = $2;
219 1.1 cgd sp->exit[sp->num_exits].y = $3;
220 1.1 cgd sp->exit[sp->num_exits].dir = dir;
221 1.1 cgd check_edge($2, $3);
222 1.1 cgd check_edir($2, $3, dir);
223 1.1 cgd sp->num_exits++;
224 1.1 cgd }
225 1.1 cgd ;
226 1.1 cgd
227 1.1 cgd Apoint_list:
228 1.1 cgd Apoint Apoint_list
229 1.1 cgd {}
230 1.1 cgd | Apoint
231 1.1 cgd {}
232 1.1 cgd ;
233 1.1 cgd
234 1.1 cgd Apoint:
235 1.1 cgd '(' ConstOp ConstOp DirOp ')'
236 1.1 cgd {
237 1.1 cgd int dir;
238 1.1 cgd
239 1.1 cgd if (sp->num_airports % REALLOC == 0) {
240 1.1 cgd if (sp->airport == NULL)
241 1.11 dholland sp->airport = malloc((sp->num_airports
242 1.1 cgd + REALLOC) * sizeof(AIRPORT));
243 1.1 cgd else
244 1.11 dholland sp->airport = realloc(sp->airport,
245 1.1 cgd (sp->num_airports + REALLOC) *
246 1.1 cgd sizeof(AIRPORT));
247 1.1 cgd if (sp->airport == NULL)
248 1.1 cgd return (yyerror("No memory available."));
249 1.1 cgd }
250 1.1 cgd dir = dir_no($4);
251 1.1 cgd sp->airport[sp->num_airports].x = $2;
252 1.1 cgd sp->airport[sp->num_airports].y = $3;
253 1.1 cgd sp->airport[sp->num_airports].dir = dir;
254 1.1 cgd check_point($2, $3);
255 1.1 cgd sp->num_airports++;
256 1.1 cgd }
257 1.1 cgd ;
258 1.1 cgd
259 1.1 cgd Lline_list:
260 1.1 cgd Lline Lline_list
261 1.1 cgd {}
262 1.1 cgd | Lline
263 1.1 cgd {}
264 1.1 cgd ;
265 1.1 cgd
266 1.1 cgd Lline:
267 1.1 cgd '[' '(' ConstOp ConstOp ')' '(' ConstOp ConstOp ')' ']'
268 1.1 cgd {
269 1.1 cgd if (sp->num_lines % REALLOC == 0) {
270 1.1 cgd if (sp->line == NULL)
271 1.11 dholland sp->line = malloc((sp->num_lines +
272 1.1 cgd REALLOC) * sizeof (LINE));
273 1.1 cgd else
274 1.11 dholland sp->line = realloc(sp->line,
275 1.1 cgd (sp->num_lines + REALLOC) *
276 1.1 cgd sizeof (LINE));
277 1.1 cgd if (sp->line == NULL)
278 1.1 cgd return (yyerror("No memory available."));
279 1.1 cgd }
280 1.1 cgd sp->line[sp->num_lines].p1.x = $3;
281 1.1 cgd sp->line[sp->num_lines].p1.y = $4;
282 1.1 cgd sp->line[sp->num_lines].p2.x = $7;
283 1.1 cgd sp->line[sp->num_lines].p2.y = $8;
284 1.1 cgd check_line($3, $4, $7, $8);
285 1.1 cgd sp->num_lines++;
286 1.1 cgd }
287 1.1 cgd ;
288 1.1 cgd %%
289 1.1 cgd
290 1.10 dholland static void
291 1.9 jmc check_edge(int x, int y)
292 1.1 cgd {
293 1.1 cgd if (!(x == 0) && !(x == sp->width - 1) &&
294 1.1 cgd !(y == 0) && !(y == sp->height - 1))
295 1.1 cgd yyerror("edge value not on edge.");
296 1.1 cgd }
297 1.1 cgd
298 1.10 dholland static void
299 1.9 jmc check_point(int x, int y)
300 1.1 cgd {
301 1.1 cgd if (x < 1 || x >= sp->width - 1)
302 1.1 cgd yyerror("X value out of range.");
303 1.1 cgd if (y < 1 || y >= sp->height - 1)
304 1.1 cgd yyerror("Y value out of range.");
305 1.1 cgd }
306 1.1 cgd
307 1.10 dholland static void
308 1.9 jmc check_linepoint(int x, int y)
309 1.1 cgd {
310 1.1 cgd if (x < 0 || x >= sp->width)
311 1.1 cgd yyerror("X value out of range.");
312 1.1 cgd if (y < 0 || y >= sp->height)
313 1.1 cgd yyerror("Y value out of range.");
314 1.1 cgd }
315 1.1 cgd
316 1.10 dholland static void
317 1.9 jmc check_line(int px1, int py1, int px2, int py2)
318 1.1 cgd {
319 1.1 cgd int d1, d2;
320 1.1 cgd
321 1.9 jmc check_linepoint(px1, py1);
322 1.9 jmc check_linepoint(px2, py2);
323 1.1 cgd
324 1.9 jmc d1 = ABS(px2 - px1);
325 1.9 jmc d2 = ABS(py2 - py1);
326 1.1 cgd
327 1.1 cgd if (!(d1 == d2) && !(d1 == 0) && !(d2 == 0))
328 1.1 cgd yyerror("Bad line endpoints.");
329 1.1 cgd }
330 1.1 cgd
331 1.10 dholland static int
332 1.9 jmc yyerror(const char *s)
333 1.1 cgd {
334 1.9 jmc fprintf(stderr, "\"%s\": line %d: %s\n", filename, line, s);
335 1.1 cgd errors++;
336 1.1 cgd
337 1.1 cgd return (errors);
338 1.1 cgd }
339 1.1 cgd
340 1.10 dholland static void
341 1.9 jmc check_edir(int x, int y, int dir)
342 1.1 cgd {
343 1.1 cgd int bad = 0;
344 1.1 cgd
345 1.1 cgd if (x == sp->width - 1)
346 1.1 cgd x = 2;
347 1.1 cgd else if (x != 0)
348 1.1 cgd x = 1;
349 1.1 cgd if (y == sp->height - 1)
350 1.1 cgd y = 2;
351 1.1 cgd else if (y != 0)
352 1.1 cgd y = 1;
353 1.1 cgd
354 1.1 cgd switch (x * 10 + y) {
355 1.1 cgd case 00: if (dir != 3) bad++; break;
356 1.1 cgd case 01: if (dir < 1 || dir > 3) bad++; break;
357 1.1 cgd case 02: if (dir != 1) bad++; break;
358 1.1 cgd case 10: if (dir < 3 || dir > 5) bad++; break;
359 1.1 cgd case 11: break;
360 1.1 cgd case 12: if (dir > 1 && dir < 7) bad++; break;
361 1.1 cgd case 20: if (dir != 5) bad++; break;
362 1.1 cgd case 21: if (dir < 5) bad++; break;
363 1.1 cgd case 22: if (dir != 7) bad++; break;
364 1.1 cgd default:
365 1.1 cgd yyerror("Unknown value in checkdir! Get help!");
366 1.1 cgd break;
367 1.1 cgd }
368 1.1 cgd if (bad)
369 1.1 cgd yyerror("Bad direction for entrance at exit.");
370 1.1 cgd }
371 1.1 cgd
372 1.10 dholland static int
373 1.9 jmc checkdefs(void)
374 1.1 cgd {
375 1.9 jmc int error = 0;
376 1.1 cgd
377 1.1 cgd if (sp->width == 0) {
378 1.1 cgd yyerror("'width' undefined.");
379 1.9 jmc error++;
380 1.1 cgd }
381 1.1 cgd if (sp->height == 0) {
382 1.1 cgd yyerror("'height' undefined.");
383 1.9 jmc error++;
384 1.1 cgd }
385 1.1 cgd if (sp->update_secs == 0) {
386 1.1 cgd yyerror("'update' undefined.");
387 1.9 jmc error++;
388 1.1 cgd }
389 1.1 cgd if (sp->newplane_time == 0) {
390 1.1 cgd yyerror("'newplane' undefined.");
391 1.9 jmc error++;
392 1.1 cgd }
393 1.9 jmc if (error)
394 1.1 cgd return (-1);
395 1.1 cgd else
396 1.1 cgd return (0);
397 1.1 cgd }
398