0d2784b680f74a659cf543bf64033205b2ac665e
[gnushogi.git] / xshogi / parser.y
1 %{
2
3 /*
4  * FILE: parser.y
5  *
6  *     Board parser for xshogi.
7  *
8  * ------------------------------------------------------------------------
9  * xshogi is based on XBoard -- an Xt/Athena user interface for GNU Chess.
10  *
11  * Original authors:                                Dan Sears, Chris Sears
12  * Enhancements (Version 2.0 and following):        Tim Mann
13  * Modifications to XShogi (Version 1.0):           Matthias Mutz
14  * Enhancements to XShogi (Version 1.1):            Matthias Mutz
15  * Modified implementation of ISS mode for XShogi:  Matthias Mutz
16  * Current maintainer:                              Michael C. Vanier
17  *
18  * XShogi borrows some of its piece bitmaps from CRANES Shogi.
19  *
20  * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.
21  * Enhancements Copyright 1992 Free Software Foundation, Inc.
22  * Enhancements for XShogi Copyright 1993, 1994, 1995 Matthias Mutz
23  * Copyright 1998, 1999 Michael C. Vanier and the Free Software Foundation
24  *
25  * The following terms apply to Digital Equipment Corporation's copyright
26  * interest in XBoard:
27  * ------------------------------------------------------------------------
28  * All Rights Reserved
29  *
30  * Permission to use, copy, modify, and distribute this software and its
31  * documentation for any purpose and without fee is hereby granted,
32  * provided that the above copyright notice appear in all copies and that
33  * both that copyright notice and this permission notice appear in
34  * supporting documentation, and that the name of Digital not be
35  * used in advertising or publicity pertaining to distribution of the
36  * software without specific, written prior permission.
37  *
38  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44  * SOFTWARE.
45  * ------------------------------------------------------------------------
46  *
47  * This file is part of GNU shogi.
48  *
49  * GNU shogi is free software; you can redistribute it and/or modify
50  * it under the terms of the GNU General Public License as published by
51  * the Free Software Foundation.
52  *
53  * GNU shogi is distributed in the hope that it will be useful,
54  * but WITHOUT ANY WARRANTY; without even the implied warranty of
55  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
56  * GNU General Public License for more details.
57  *
58  * You should have received a copy of the GNU General Public License
59  * along with GNU shogi; see the file COPYING.  If not, write to
60  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
61  *
62  * ------------------------------------------------------------------------
63  *
64  */
65  
66 #include "xshogi.h"
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70                   
71 enum { False, True };
72
73 static void yyerror();
74            
75 static ShogiMove move_type;
76 static int  from_x, from_y, to_x, to_y;
77 static char piece;
78
79 char currentMoveString[MSG_SIZ];
80 static char token[20];
81
82 FILE *outfile = (FILE *)0;
83
84 extern FILE *gameFileFP, *toFirstProgFP;
85 extern int   currentMove;
86 extern char  moveList[MAX_MOVES][MOVE_LEN];
87
88 extern void SendToProgram(char *message, FILE *fp);
89 extern void MakeMove(ShogiMove *move_type, int from_x, int from_y, 
90                      int to_x, int to_y);
91
92 %}
93
94 %start goal
95 %token PROMOTE DROPS PIECE SQUARE NUMBER COMMENT COLON 
96 %token BLACK_WINS WHITE_WINS DRAW
97       
98 %union { int val; char* string; }
99
100 %%         
101
102  goal:
103    comment_list move_elem move_list
104  ;                           
105
106  comment_list:
107   | comment_list COMMENT
108         { fprintf(stderr, "%s\n", $<string>2); } 
109  ;
110
111  move_list:
112   | move_list move_elem
113  ;
114
115  move_elem:
116     { strcpy(token, "number");   } number 
117     { strcpy(token, "promoted"); } promoted 
118     { strcpy(token, "move");     } move
119  ;
120
121  number:
122   | NUMBER { strcpy(token, "colon"); } COLON
123  ;
124
125  promoted:
126   | PROMOTE
127  ;
128
129  move:
130    SQUARE  { from_x = '9' - $<string>1[0]; 
131              from_y = 'i' - $<string>1[1];
132              strcpy(currentMoveString,$<string>1);
133              strcpy(token, "square"); }
134    SQUARE  { to_x = '9' - $<string>3[0]; 
135              to_y = 'i' - $<string>3[1];
136              strcat(currentMoveString,$<string>3); }
137    { move_type = NormalMove; }
138    promotion
139     { 
140       SendToProgram(currentMoveString, toFirstProgFP);
141       strcpy(moveList[currentMove], currentMoveString);
142       MakeMove(&move_type, from_x, from_y, to_x, to_y);
143     }
144   |
145    PIECE   { piece = $<string>1[0]; 
146              strcpy(currentMoveString,$<string>1); 
147              strcpy(token,"'*'"); }
148    DROPS   { strcat(currentMoveString,"*"); 
149              strcpy(token, "square"); }
150    SQUARE  { to_x = '9' - $<string>5[0]; 
151              to_y = 'i' - $<string>5[1];
152              strcat(currentMoveString,$<string>5); }
153     { 
154         move_type = (BlackOnMove(currentMove) ? BlackDrop : WhiteDrop);
155
156         switch ( piece ) 
157         {
158         case 'P': from_x = 81; break;
159         case 'L': from_x = 82; break;
160         case 'N': from_x = 83; break;
161         case 'S': from_x = 84; break;
162         case 'G': from_x = 85; break;
163         case 'B': from_x = 86; break;
164         case 'R': from_x = 87; break;
165         case 'K': from_x = 88; break;
166         default:  from_x = -1;
167         };
168         
169         from_y = from_x;
170         SendToProgram(currentMoveString, toFirstProgFP);
171         strcpy(moveList[currentMove], currentMoveString);
172         MakeMove(&move_type, from_x, from_y, to_x, to_y);
173     }
174   | BLACK_WINS 
175     { 
176                 loaded_game_finished = 1;
177                 DisplayMessage("Black wins", False);
178         }
179   | WHITE_WINS 
180     { 
181                 loaded_game_finished = 1;
182                 DisplayMessage("White wins", False);
183         }
184   | DRAW
185     { 
186                 loaded_game_finished = 1;
187                 DisplayMessage("Draw", False);
188         }
189  ;         
190
191  promotion:
192    | PROMOTE 
193      { move_type = (BlackOnMove(currentMove) 
194                                         ? BlackPromotion : WhitePromotion); 
195        strcat(currentMoveString,"+"); }
196  ;
197
198 %%
199  
200
201
202 #include "scanner.c"
203
204
205 static void yyerror(char *errmsg)
206 {                               
207     if (strlen(token) > 0) 
208     {
209         fprintf(stderr, "parse error line %d column %d : %s expected\n",
210                 lines, cols, token); 
211         token[0] = '\0';
212     } 
213     else
214     {
215         fprintf(stderr,"parse error line %d column %d : %s\n",
216                 lines, cols, errmsg); 
217     }
218
219     exit(-1); 
220 }
221        
222
223 void parseGameFile()
224
225   yyin = gameFileFP;
226   outfile = stderr;
227   yyparse();
228 }
229