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