File: parser.y

package info (click to toggle)
xshogi 1.4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,832 kB
  • sloc: ansic: 6,199; sh: 1,373; makefile: 251; yacc: 137; lex: 109
file content (231 lines) | stat: -rw-r--r-- 6,758 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
%{

/*
 * FILE: parser.y
 *
 *     Board parser for xshogi.
 *
 * ------------------------------------------------------------------------
 * xshogi is based on XBoard -- an Xt/Athena user interface for GNU Chess.
 *
 * Original authors:                                Dan Sears, Chris Sears
 * Enhancements (Version 2.0 and following):        Tim Mann
 * Modifications to XShogi (Version 1.0):           Matthias Mutz
 * Enhancements to XShogi (Version 1.1):            Matthias Mutz
 * Modified implementation of ISS mode for XShogi:  Matthias Mutz
 * Current maintainer:                              Michael C. Vanier
 *
 * XShogi borrows some of its piece bitmaps from CRANES Shogi.
 *
 * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.
 * Enhancements Copyright 1992 Free Software Foundation, Inc.
 * Enhancements for XShogi Copyright 1993, 1994, 1995 Matthias Mutz
 * Copyright 1998, 1999 Michael C. Vanier and the Free Software Foundation
 *
 * The following terms apply to Digital Equipment Corporation's copyright
 * interest in XBoard:
 * ------------------------------------------------------------------------
 * All Rights Reserved
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of Digital not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 *
 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 * ------------------------------------------------------------------------
 *
 * This file is part of GNU shogi.
 *
 * GNU shogi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * GNU shogi is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU shogi; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * ------------------------------------------------------------------------
 *
 */
 
#include "xshogi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern void DisplayMessage(char *message, int toRemotePlayer);
extern int yylex (void);

enum { False, True };

static void yyerror(char *errmsg);
           
static ShogiMove move_type;
static int  from_x, from_y, to_x, to_y;
static char piece;

char currentMoveString[MSG_SIZ];
static char token[20];

FILE *outfile = (FILE *)0;

extern FILE *gameFileFP, *toFirstProgFP;
extern int   currentMove;
extern char  moveList[MAX_MOVES][MOVE_LEN];

extern void SendToProgram(char *message, FILE *fp);
extern void MakeMove(ShogiMove *move_type, int from_x, int from_y, 
                     int to_x, int to_y);

int lines = 1, cols = 1;

%}

%start goal
%token PROMOTE DROPS PIECE SQUARE NUMBER COMMENT COLON 
%token BLACK_WINS WHITE_WINS DRAW
      
%union { int val; char* string; }

%%         

 goal:
   comment_list move_elem move_list
 ;                           

 comment_list:
  | comment_list COMMENT
        { fprintf(stderr, "%s\n", $<string>2); } 
 ;

 move_list:
  | move_list move_elem
 ;

 move_elem:
    { strcpy(token, "number");   } number 
    { strcpy(token, "promoted"); } promoted 
    { strcpy(token, "move");     } move
 ;

 number:
  | NUMBER { strcpy(token, "colon"); } COLON
 ;

 promoted:
  | PROMOTE
 ;

 move:
   SQUARE  { from_x = '9' - $<string>1[0]; 
             from_y = 'i' - $<string>1[1];
             strcpy(currentMoveString,$<string>1);
             strcpy(token, "square"); }
   SQUARE  { to_x = '9' - $<string>3[0]; 
             to_y = 'i' - $<string>3[1];
             strcat(currentMoveString,$<string>3); }
   { move_type = NormalMove; }
   promotion
    { 
      SendToProgram(currentMoveString, toFirstProgFP);
      strcpy(moveList[currentMove], currentMoveString);
      MakeMove(&move_type, from_x, from_y, to_x, to_y);
    }
  |
   PIECE   { piece = $<string>1[0]; 
             strcpy(currentMoveString,$<string>1); 
             strcpy(token,"'*'"); }
   DROPS   { strcat(currentMoveString,"*"); 
             strcpy(token, "square"); }
   SQUARE  { to_x = '9' - $<string>5[0]; 
             to_y = 'i' - $<string>5[1];
             strcat(currentMoveString,$<string>5); }
    { 
        move_type = (BlackOnMove(currentMove) ? BlackDrop : WhiteDrop);

        switch ( piece ) 
        {
        case 'P': from_x = 81; break;
        case 'L': from_x = 82; break;
        case 'N': from_x = 83; break;
        case 'S': from_x = 84; break;
        case 'G': from_x = 85; break;
        case 'B': from_x = 86; break;
        case 'R': from_x = 87; break;
        case 'K': from_x = 88; break;
        default:  from_x = -1;
        };
        
        from_y = from_x;
        SendToProgram(currentMoveString, toFirstProgFP);
        strcpy(moveList[currentMove], currentMoveString);
        MakeMove(&move_type, from_x, from_y, to_x, to_y);
    }
  | BLACK_WINS 
    { 
		loaded_game_finished = 1;
		DisplayMessage("Black wins", False);
	}
  | WHITE_WINS 
    { 
		loaded_game_finished = 1;
		DisplayMessage("White wins", False);
	}
  | DRAW
    { 
		loaded_game_finished = 1;
		DisplayMessage("Draw", False);
	}
 ;         

 promotion:
   | PROMOTE 
     { move_type = (BlackOnMove(currentMove) 
					? BlackPromotion : WhitePromotion); 
       strcat(currentMoveString,"+"); }
 ;

%%
 

static void yyerror(char *errmsg)
{                               
    if (strlen(token) > 0) 
    {
        fprintf(stderr, "parse error line %d column %d : %s expected\n",
                lines, cols, token); 
        token[0] = '\0';
    } 
    else
    {
        fprintf(stderr,"parse error line %d column %d : %s\n",
                lines, cols, errmsg); 
    }

    exit(-1); 
}
       
extern FILE *yyin;

void parseGameFile()
{ 
  yyin = gameFileFP;
  outfile = stderr;
  yyparse();
}