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