grammar.y 8.2 KB

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