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