Xshogi: build scanner separately from parser.
[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 int lines = 1, cols = 1;
93
94 %}
95
96 %start goal
97 %token PROMOTE DROPS PIECE SQUARE NUMBER COMMENT COLON 
98 %token BLACK_WINS WHITE_WINS DRAW
99       
100 %union { int val; char* string; }
101
102 %%         
103
104  goal:
105    comment_list move_elem move_list
106  ;                           
107
108  comment_list:
109   | comment_list COMMENT
110         { fprintf(stderr, "%s\n", $<string>2); } 
111  ;
112
113  move_list:
114   | move_list move_elem
115  ;
116
117  move_elem:
118     { strcpy(token, "number");   } number 
119     { strcpy(token, "promoted"); } promoted 
120     { strcpy(token, "move");     } move
121  ;
122
123  number:
124   | NUMBER { strcpy(token, "colon"); } COLON
125  ;
126
127  promoted:
128   | PROMOTE
129  ;
130
131  move:
132    SQUARE  { from_x = '9' - $<string>1[0]; 
133              from_y = 'i' - $<string>1[1];
134              strcpy(currentMoveString,$<string>1);
135              strcpy(token, "square"); }
136    SQUARE  { to_x = '9' - $<string>3[0]; 
137              to_y = 'i' - $<string>3[1];
138              strcat(currentMoveString,$<string>3); }
139    { move_type = NormalMove; }
140    promotion
141     { 
142       SendToProgram(currentMoveString, toFirstProgFP);
143       strcpy(moveList[currentMove], currentMoveString);
144       MakeMove(&move_type, from_x, from_y, to_x, to_y);
145     }
146   |
147    PIECE   { piece = $<string>1[0]; 
148              strcpy(currentMoveString,$<string>1); 
149              strcpy(token,"'*'"); }
150    DROPS   { strcat(currentMoveString,"*"); 
151              strcpy(token, "square"); }
152    SQUARE  { to_x = '9' - $<string>5[0]; 
153              to_y = 'i' - $<string>5[1];
154              strcat(currentMoveString,$<string>5); }
155     { 
156         move_type = (BlackOnMove(currentMove) ? BlackDrop : WhiteDrop);
157
158         switch ( piece ) 
159         {
160         case 'P': from_x = 81; break;
161         case 'L': from_x = 82; break;
162         case 'N': from_x = 83; break;
163         case 'S': from_x = 84; break;
164         case 'G': from_x = 85; break;
165         case 'B': from_x = 86; break;
166         case 'R': from_x = 87; break;
167         case 'K': from_x = 88; break;
168         default:  from_x = -1;
169         };
170         
171         from_y = from_x;
172         SendToProgram(currentMoveString, toFirstProgFP);
173         strcpy(moveList[currentMove], currentMoveString);
174         MakeMove(&move_type, from_x, from_y, to_x, to_y);
175     }
176   | BLACK_WINS 
177     { 
178                 loaded_game_finished = 1;
179                 DisplayMessage("Black wins", False);
180         }
181   | WHITE_WINS 
182     { 
183                 loaded_game_finished = 1;
184                 DisplayMessage("White wins", False);
185         }
186   | DRAW
187     { 
188                 loaded_game_finished = 1;
189                 DisplayMessage("Draw", False);
190         }
191  ;         
192
193  promotion:
194    | PROMOTE 
195      { move_type = (BlackOnMove(currentMove) 
196                                         ? BlackPromotion : WhitePromotion); 
197        strcat(currentMoveString,"+"); }
198  ;
199
200 %%
201  
202
203 static void yyerror(char *errmsg)
204 {                               
205     if (strlen(token) > 0) 
206     {
207         fprintf(stderr, "parse error line %d column %d : %s expected\n",
208                 lines, cols, token); 
209         token[0] = '\0';
210     } 
211     else
212     {
213         fprintf(stderr,"parse error line %d column %d : %s\n",
214                 lines, cols, errmsg); 
215     }
216
217     exit(-1); 
218 }
219        
220 extern FILE *yyin;
221
222 void parseGameFile()
223
224   yyin = gameFileFP;
225   outfile = stderr;
226   yyparse();
227 }
228