PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gibbon-java-fibs-parser.y

#
Happy | 369 lines | 327 code | 42 blank | 0 comment | 0 complexity | 4c80de29f86983fbc45c14e35f0ab761 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0
  1. /*
  2. * This file is part of gibbon.
  3. * Gibbon is a Gtk+ frontend for the First Internet Backgammon Server FIBS.
  4. * Copyright (C) 2009-2012 Guido Flohr, http://guido-flohr.net/.
  5. *
  6. * gibbon is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * gibbon is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with gibbon. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * Parser for the internal format of JavaFIBS. This grammar describes that
  21. * format only loosely. This is sufficient for parsing because we will
  22. * ignore all redundant date, while building the syntax tree.
  23. */
  24. %{
  25. #ifdef HAVE_CONFIG_H
  26. # include "config.h"
  27. #endif
  28. #include <glib.h>
  29. #include <glib/gi18n.h>
  30. #include "gibbon-java-fibs-parser.h"
  31. #include "gibbon-java-fibs-reader-priv.h"
  32. #define reader gibbon_java_fibs_lexer_get_extra(scanner)
  33. /*
  34. * Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  35. * as well as gratuitiously global symbol names, so we can have multiple
  36. * yacc generated parsers in the same program. Note that these are only
  37. * the variables produced by yacc. If other parser generators (bison,
  38. * byacc, etc) produce additional global names that conflict at link time,
  39. * then those parser generators need to be fixed instead of adding those
  40. * names to this list.
  41. */
  42. #define yymaxdepth gibbon_java_fibs_parser_maxdepth
  43. #define yyparse(s) gibbon_java_fibs_parser_parse(s)
  44. #define yylex gibbon_java_fibs_lexer_lex
  45. extern int gibbon_java_fibs_lexer_lex (YYSTYPE * lvalp, void *scanner);
  46. #define yyerror gibbon_java_fibs_reader_yyerror
  47. #define yylval gibbon_java_fibs_parser_lval
  48. #define yychar gibbon_java_fibs_parser_char
  49. #define yydebug gibbon_java_fibs_parser_debug
  50. #define yypact gibbon_java_fibs_parser_pact
  51. #define yyr1 gibbon_java_fibs_parser_r1
  52. #define yyr2 gibbon_java_fibs_parser_r2
  53. #define yydef gibbon_java_fibs_parser_def
  54. #define yychk gibbon_java_fibs_parser_chk
  55. #define yypgo gibbon_java_fibs_parser_pgo
  56. #define yyact gibbon_java_fibs_parser_act
  57. #define yyexca gibbon_java_fibs_parser_exca
  58. #define yyerrflag gibbon_java_fibs_parser_errflag
  59. #define yynerrs gibbon_java_fibs_parser_nerrs
  60. #define yyps gibbon_java_fibs_parser_ps
  61. #define yypv gibbon_java_fibs_parser_pv
  62. #define yys gibbon_java_fibs_parser_s
  63. #define yy_yys gibbon_java_fibs_parser_yys
  64. #define yystate gibbon_java_fibs_parser_state
  65. #define yytmp gibbon_java_fibs_parser_tmp
  66. #define yyv gibbon_java_fibs_parser_v
  67. #define yy_yyv gibbon_java_fibs_parser_yyv
  68. #define yyval gibbon_java_fibs_parser_val
  69. #define yylloc gibbon_java_fibs_parser_lloc
  70. #define yyreds gibbon_java_fibs_parser_reds /* With YYDEBUG defined */
  71. #define yytoks gibbon_java_fibs_parser_toks /* With YYDEBUG defined */
  72. #define yylhs gibbon_java_fibs_parser_yylhs
  73. #define yylen gibbon_java_fibs_parser_yylen
  74. #define yydefred gibbon_java_fibs_parser_yydefred
  75. #define yysdgoto gibbon_java_fibs_parser_yydgoto
  76. #define yysindex gibbon_java_fibs_parser_yysindex
  77. #define yyrindex gibbon_java_fibs_parser_yyrindex
  78. #define yygindex gibbon_java_fibs_parser_yygindex
  79. #define yytable gibbon_java_fibs_parser_yytable
  80. #define yycheck gibbon_java_fibs_parser_yycheck
  81. #define YYDEBUG 42
  82. static guint gibbon_java_fibs_parser_encode_movement (guint64 from, guint64 to);
  83. %}
  84. %union {
  85. guint64 num;
  86. gchar *name;
  87. }
  88. %token PROLOG
  89. %token COLON
  90. %token HYPHEN
  91. %token <num> INTEGER
  92. %token <name> PLAYER
  93. %token ROLL
  94. %token MOVE
  95. %token CUBE
  96. %token RESIGN
  97. %token TAKE
  98. %token DROP
  99. %token START_OF_GAME
  100. %token WIN_GAME
  101. %token START_OF_MATCH
  102. %token WIN_MATCH
  103. %token REJECT_RESIGN
  104. %token OPPONENTS
  105. %token SCORE
  106. %token BAR
  107. %token OFF
  108. %type <num>point
  109. %type <num>movement
  110. %type <num>movements
  111. %pure-parser
  112. %lex-param {void *scanner}
  113. %parse-param {void *scanner}
  114. %%
  115. java_fibs_file
  116. : /* empty */
  117. {
  118. yyerror (scanner,
  119. _("Empty file!"));
  120. YYABORT;
  121. }
  122. | { yydebug = 0; }
  123. PROLOG match { gibbon_java_fibs_reader_free_names (reader); }
  124. ;
  125. match
  126. : START_OF_MATCH COLON PLAYER
  127. {
  128. _gibbon_java_fibs_reader_set_black (reader, $3);
  129. }
  130. COLON INTEGER
  131. {
  132. _gibbon_java_fibs_reader_set_match_length (reader, $6);
  133. }
  134. playing
  135. ;
  136. playing
  137. : games win_match
  138. | games incomplete_game
  139. ;
  140. games
  141. : /* empty */
  142. | games game
  143. ;
  144. game
  145. : start_of_game opponents actions win_game score
  146. | start_of_game opponents actions win_game
  147. ;
  148. incomplete_game
  149. : start_of_game opponents actions
  150. ;
  151. start_of_game
  152. : START_OF_GAME COLON PLAYER COLON
  153. {
  154. _gibbon_java_fibs_reader_set_black (reader, $3);
  155. if (!_gibbon_java_fibs_reader_add_game (reader))
  156. YYABORT;
  157. }
  158. ;
  159. opponents
  160. : OPPONENTS COLON PLAYER
  161. {
  162. _gibbon_java_fibs_reader_set_white (reader, $3);
  163. }
  164. COLON PLAYER
  165. {
  166. _gibbon_java_fibs_reader_set_black (reader, $6);
  167. }
  168. ;
  169. actions
  170. : /* empty */
  171. | actions action { gibbon_java_fibs_reader_free_names (reader); }
  172. ;
  173. action
  174. : roll | move | cube | drop | take
  175. | resign | reject_resign
  176. ;
  177. roll
  178. : ROLL COLON PLAYER COLON INTEGER INTEGER
  179. {
  180. if (!_gibbon_java_fibs_reader_roll (reader, $3, $5, $6))
  181. YYABORT;
  182. }
  183. ;
  184. move
  185. : MOVE COLON PLAYER COLON movements
  186. {
  187. if (!_gibbon_java_fibs_reader_move (reader, $3, $5))
  188. YYABORT;
  189. }
  190. /*
  191. * Sometimes, JavaFIBS appends a gratuitous "xyz-moves ...". We just
  192. * match that exception without being too strict about the exact
  193. * semantics.
  194. */
  195. | MOVE COLON PLAYER COLON movements PLAYER HYPHEN PLAYER garbage
  196. {
  197. if (!_gibbon_java_fibs_reader_move (reader, $3, $5))
  198. YYABORT;
  199. }
  200. ;
  201. movements
  202. : /* empty */
  203. {
  204. $$ = 0;
  205. }
  206. | movement /* $$ = $1 */
  207. | movement movement
  208. {
  209. $$ = $1 << 16 | $2;
  210. }
  211. | movement movement movement
  212. {
  213. $$ = $1 << 32 | $2 << 16 | $3;
  214. }
  215. | movement movement movement movement
  216. {
  217. $$ = $1 << 48 | $2 << 32 | $3 << 16 | $4;
  218. }
  219. ;
  220. garbage
  221. : /* empty */
  222. | garbage point
  223. | garbage HYPHEN
  224. ;
  225. movement
  226. : point HYPHEN point
  227. {
  228. if ($1 == $3) {
  229. yyerror (scanner,
  230. _("Start and end point are equal!"));
  231. YYABORT;
  232. }
  233. $$ = gibbon_java_fibs_parser_encode_movement ($1, $3);
  234. }
  235. ;
  236. point
  237. : INTEGER
  238. {
  239. if (!$$ || $$ > 24) {
  240. yyerror (scanner,
  241. _("Point out of range (1-24)!"));
  242. YYABORT;
  243. }
  244. }
  245. | BAR { $$ = 25; }
  246. | OFF { $$ = 0; }
  247. ;
  248. cube
  249. : CUBE COLON PLAYER COLON
  250. {
  251. if (!_gibbon_java_fibs_reader_double (reader, $3))
  252. YYABORT;
  253. }
  254. ;
  255. drop
  256. : DROP COLON PLAYER COLON
  257. {
  258. if (!_gibbon_java_fibs_reader_drop (reader, $3))
  259. YYABORT;
  260. }
  261. ;
  262. take
  263. : TAKE COLON PLAYER COLON
  264. {
  265. if (!_gibbon_java_fibs_reader_take (reader, $3))
  266. YYABORT;
  267. }
  268. ;
  269. win_game
  270. : WIN_GAME COLON PLAYER COLON INTEGER
  271. {
  272. if (!_gibbon_java_fibs_reader_win_game (reader, $3, $5))
  273. YYABORT;
  274. }
  275. ;
  276. score
  277. : SCORE COLON PLAYER HYPHEN INTEGER COLON PLAYER HYPHEN INTEGER
  278. {
  279. if (!_gibbon_java_fibs_reader_score (reader,
  280. $3, $5,
  281. $7, $9))
  282. YYABORT;
  283. }
  284. ;
  285. resign
  286. : RESIGN COLON PLAYER COLON INTEGER
  287. {
  288. if (!_gibbon_java_fibs_reader_resign (reader, $3, $5))
  289. YYABORT;
  290. }
  291. ;
  292. reject_resign
  293. : REJECT_RESIGN COLON PLAYER COLON
  294. {
  295. if (!_gibbon_java_fibs_reader_reject_resign (reader,
  296. $3))
  297. YYABORT;
  298. }
  299. ;
  300. win_match
  301. : WIN_MATCH COLON PLAYER COLON INTEGER
  302. ;
  303. %%
  304. static guint
  305. gibbon_java_fibs_parser_encode_movement (guint64 from, guint64 to)
  306. {
  307. /*
  308. * This is the first normalization step for JavaFIBS moves.
  309. * Depending on the direction of the move we translate 25 and 0 to
  310. * bar and off.
  311. */
  312. if (from >= 25 && to <= 6)
  313. /* Come in from bar. */
  314. from = 0;
  315. if (to == 0 && from > 6)
  316. /* Bear-off. */
  317. to = 25;
  318. /*
  319. * And now we make sure that we move in descending direction. That
  320. * corresponds to white's move direction in our internal notion.
  321. */
  322. if (from < to) {
  323. from = -from + 25;
  324. to = -to + 25;
  325. }
  326. return (from << 8 | to);
  327. }