2015-06-19 09:02:31 +03:00
|
|
|
/* $NetBSD: grammar.y,v 1.12 2015/06/19 06:02:31 dholland Exp $ */
|
1995-03-21 18:03:38 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1995-03-21 18:03:38 +03:00
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 13:36:50 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
%{
|
1997-10-10 06:07:01 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-21 18:03:38 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)grammar.y 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
2015-06-19 09:02:31 +03:00
|
|
|
__RCSID("$NetBSD: grammar.y,v 1.12 2015/06/19 06:02:31 dholland Exp $");
|
1995-03-21 18:03:38 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2015-06-19 09:02:31 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "def.h"
|
|
|
|
#include "struct.h"
|
|
|
|
#include "extern.h"
|
|
|
|
#include "tunable.h"
|
|
|
|
|
2009-08-12 08:48:03 +04:00
|
|
|
int line = 1;
|
|
|
|
|
|
|
|
static int errors = 0;
|
|
|
|
|
|
|
|
static int yyerror(const char *);
|
1993-03-21 12:45:37 +03:00
|
|
|
%}
|
|
|
|
|
|
|
|
%%
|
|
|
|
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 ';'
|
|
|
|
{
|
1999-07-21 17:10:47 +04:00
|
|
|
if (sp->width != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
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) {
|
|
|
|
if (sp->beacon == NULL)
|
2014-03-23 02:09:14 +04:00
|
|
|
sp->beacon = malloc((sp->num_beacons
|
1993-03-21 12:45:37 +03:00
|
|
|
+ REALLOC) * sizeof (BEACON));
|
|
|
|
else
|
2014-03-23 02:09:14 +04:00
|
|
|
sp->beacon = realloc(sp->beacon,
|
1993-03-21 12:45:37 +03:00
|
|
|
(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) {
|
|
|
|
if (sp->exit == NULL)
|
2014-03-23 02:09:14 +04:00
|
|
|
sp->exit = malloc((sp->num_exits +
|
1993-03-21 12:45:37 +03:00
|
|
|
REALLOC) * sizeof (EXIT));
|
|
|
|
else
|
2014-03-23 02:09:14 +04:00
|
|
|
sp->exit = realloc(sp->exit,
|
1993-03-21 12:45:37 +03:00
|
|
|
(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) {
|
|
|
|
if (sp->airport == NULL)
|
2014-03-23 02:09:14 +04:00
|
|
|
sp->airport = malloc((sp->num_airports
|
1993-03-21 12:45:37 +03:00
|
|
|
+ REALLOC) * sizeof(AIRPORT));
|
|
|
|
else
|
2014-03-23 02:09:14 +04:00
|
|
|
sp->airport = realloc(sp->airport,
|
1993-03-21 12:45:37 +03:00
|
|
|
(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);
|
|
|
|
sp->num_airports++;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
Lline_list:
|
|
|
|
Lline Lline_list
|
|
|
|
{}
|
|
|
|
| Lline
|
|
|
|
{}
|
|
|
|
;
|
|
|
|
|
|
|
|
Lline:
|
|
|
|
'[' '(' ConstOp ConstOp ')' '(' ConstOp ConstOp ')' ']'
|
|
|
|
{
|
|
|
|
if (sp->num_lines % REALLOC == 0) {
|
|
|
|
if (sp->line == NULL)
|
2014-03-23 02:09:14 +04:00
|
|
|
sp->line = malloc((sp->num_lines +
|
1993-03-21 12:45:37 +03:00
|
|
|
REALLOC) * sizeof (LINE));
|
|
|
|
else
|
2014-03-23 02:09:14 +04:00
|
|
|
sp->line = realloc(sp->line,
|
1993-03-21 12:45:37 +03:00
|
|
|
(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++;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
%%
|
|
|
|
|
2009-08-12 08:48:03 +04:00
|
|
|
static void
|
2005-07-01 04:48:34 +04:00
|
|
|
check_edge(int x, int y)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
if (!(x == 0) && !(x == sp->width - 1) &&
|
|
|
|
!(y == 0) && !(y == sp->height - 1))
|
|
|
|
yyerror("edge value not on edge.");
|
|
|
|
}
|
|
|
|
|
2009-08-12 08:48:03 +04:00
|
|
|
static void
|
2005-07-01 04:48:34 +04:00
|
|
|
check_point(int x, int y)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
2009-08-12 08:48:03 +04:00
|
|
|
static void
|
2005-07-01 04:48:34 +04:00
|
|
|
check_linepoint(int x, int y)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
if (x < 0 || x >= sp->width)
|
|
|
|
yyerror("X value out of range.");
|
|
|
|
if (y < 0 || y >= sp->height)
|
|
|
|
yyerror("Y value out of range.");
|
|
|
|
}
|
|
|
|
|
2009-08-12 08:48:03 +04:00
|
|
|
static void
|
2005-07-01 04:48:34 +04:00
|
|
|
check_line(int px1, int py1, int px2, int py2)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int d1, d2;
|
|
|
|
|
2005-07-01 04:48:34 +04:00
|
|
|
check_linepoint(px1, py1);
|
|
|
|
check_linepoint(px2, py2);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-07-01 04:48:34 +04:00
|
|
|
d1 = ABS(px2 - px1);
|
|
|
|
d2 = ABS(py2 - py1);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (!(d1 == d2) && !(d1 == 0) && !(d2 == 0))
|
|
|
|
yyerror("Bad line endpoints.");
|
|
|
|
}
|
|
|
|
|
2009-08-12 08:48:03 +04:00
|
|
|
static int
|
2005-07-01 04:48:34 +04:00
|
|
|
yyerror(const char *s)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-07-01 04:48:34 +04:00
|
|
|
fprintf(stderr, "\"%s\": line %d: %s\n", filename, line, s);
|
1993-03-21 12:45:37 +03:00
|
|
|
errors++;
|
|
|
|
|
|
|
|
return (errors);
|
|
|
|
}
|
|
|
|
|
2009-08-12 08:48:03 +04:00
|
|
|
static void
|
2005-07-01 04:48:34 +04:00
|
|
|
check_edir(int x, int y, int dir)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
2009-08-12 08:48:03 +04:00
|
|
|
static int
|
2005-07-01 04:48:34 +04:00
|
|
|
checkdefs(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-07-01 04:48:34 +04:00
|
|
|
int error = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (sp->width == 0) {
|
|
|
|
yyerror("'width' undefined.");
|
2005-07-01 04:48:34 +04:00
|
|
|
error++;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (sp->height == 0) {
|
|
|
|
yyerror("'height' undefined.");
|
2005-07-01 04:48:34 +04:00
|
|
|
error++;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (sp->update_secs == 0) {
|
|
|
|
yyerror("'update' undefined.");
|
2005-07-01 04:48:34 +04:00
|
|
|
error++;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (sp->newplane_time == 0) {
|
|
|
|
yyerror("'newplane' undefined.");
|
2005-07-01 04:48:34 +04:00
|
|
|
error++;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2005-07-01 04:48:34 +04:00
|
|
|
if (error)
|
1993-03-21 12:45:37 +03:00
|
|
|
return (-1);
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
}
|