-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.java
350 lines (317 loc) · 11.8 KB
/
Parser.java
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package enshud.s2.parser;
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Parser {
private static final boolean DEBUG = false;
public int line_of_last_true = 1;
String[][] tokens = {
{"0","and","SAND"},
{"1","array","SARRAY"},
{"2","begin","SBEGIN"},
{"3","boolean","SBOOLEAN"},
{"4","char","SCHAR"},
{"5","div","SDIVD"},
{"6","do","SDO"},
{"7","else","SELSE"},
{"8","end","SEND"},
{"9","false","SFALSE"},
{"10","if","SIF"},
{"11","integer","SINTEGER"},
{"12","mod","SMOD"},
{"13","not","SNOT"},
{"14","of","SOF"},
{"15","or","SOR"},
{"16","procedure","SPROCEDURE"},
{"17","program","SPROGRAM"},
{"18","readln","SREADLN"},
{"19","then","STHEN"},
{"20","true","STRUE"},
{"21","var","SVAR"},
{"22","while","SWHILE"},
{"23","writeln","SWRITELN"},
{"24","=","SEQUAL"},
{"25","<>","SNOTEQUAL"},
{"26","<","SLESS"},
{"27","<=","SLESSEQUAL"},
{"28",">=","SGREATEQUAL"},
{"29",">","SGREAT"},
{"30","+","SPLUS"},
{"31","-","SMINUS"},
{"32","*","SSTAR"},
{"33","(","SLPAREN"},
{"34",")","SRPAREN"},
{"35","[","SLBRACKET"},
{"36","]","SRBRACKET"},
{"37",";","SSEMICOLON"},
{"38",":","SCOLON"},
{"39","..","SRANGE"},
{"40",":=","SASSIGN"},
{"41",",","SCOMMA"},
{"42",".","SDOT"},
{"5","/","SDIVD"},
};
int[][] syntax = new int[][]{
{ 17, 43, 37, 101, 133, 42, -1,-1, -1 },
{ 102, 109, -1, -1, -1, -1, -1, -1, -1 },
{ 21, 103, -1, -1, -1, -1, -1, -1, -10 },
{ 104, 38, 105, 37, -1, -1, -1, -1, 100 },
{ 43, -1, -1, -1, -1, -1, -1, -1, 98 },
//105
{ 106, 107, -1, -1, -1, -1, -1, -1, -100 },
{ 3, 4, 11, -1, -1, -1, -1, -1, -100 },
{ 1, 35, 108, 39, 108, 36, 14, 106, -1 },
{ 134, 135, 44, -1, -1, -1, -1, -1, -100 },
{ 110, 37, -1, -1, -1, -1, -1, -1, 0 },
//110
{ 111, 102, 133, -1, -1, -1, -1, -1, -1 },
{ 16, 43, 112, 37, -1, -1, -1, -1, -1 },
{ 33, 113, 34, -1, -1, -1, -1, -1, -10 },
{ 114, 38, 106, -1, -1, -1, -1, -1, 99 },
{ 43, -1, -1, -1, -1, -1, -1, -1, 98 },
//115
{ 116, 37, -1, -1, -1, -1, -1, -1, 0 },
{ 138, 136, 137, 117, -1, -1, -1, -1, -100 },
{ 118, 121, 130, 133, -1, -1, -1, -1, -100 },
{ 119, 40, 123, -1, -1, -1, -1, -1, -1 },
{ 120, 43, -1, -1, -1, -1, -1, -1, -100 },
//120
{ 43, 35, 123, 36, -1, -1, -1, -1, -1 },
{ 139, 43, -1, -1, -1, -1, -1, -1, -100 },
{ 123, -1, -1, -1, -1, -1, -1, -1, 98 },
{ 140, 124, -1, -1, -1, -1, -1, -1, -100 },
{ 143, 144, 142, -1, -1, -1, -1, -1, -100 },
//125
{ 126, 145, -1, -1, -1, -1, -1, -1, -1 },
{ 119, 132, 146, 147, -1, -1, -1, -1, -100 },
{ 24, 25, 27, 28, 29, 26, -1, -1, -100 },
{ 30, 31, 15, -1, -1, -1, -1, -1, -100 },
{ 5, 32, 12, 0, -1, -1, -1, -1, -100 },
//130
{ 148, 149, 18, 23, -1, -1, -1, -1, -100 },
{ 119, -1, -1, -1, -1, -1, -1, -1, 98 },
{ 44, 45, 9, 20, -1, -1, -1, -1, -100 },
//FUKUGOUBUN = begin narabi end - 133
{ 2, 115, 8, -1, -1, -1, -1, -1, -1 },
//EXTRA CASES START HERE from 134
{ 30, 44, -1, -1, -1, -1, -1, -1, -1 },
{ 31, 44, -1, -1, -1, -1, -1, -1, -1 },
{ 10, 123, 19, 133, 7, 133, -1, -1, -1 },
{ 10, 123, 19, 133, -1, -1, -1, -1, -1 },
{ 22, 123, 6, 133, -1, -1, -1, -1, -1 },
{ 43, 33, 122, 34, -1, -1, -1, -1, -1 },
{ 124, 127, 124, -1, -1, -1, -1, -1, -1 },
{ 128, 125, -1, -1, -1, -1, -1, -1, 0 },
{ 125, 141, -1, -1, -1, -1, -1, -1, -1 },
{ 30, 125, 141, -1, -1, -1, -1, -1, -1 },
{ 31, 125, 141, -1, -1, -1, -1, -1, -1 },
{ 129, 126, -1, -1, -1, -1, -1, -1, 0 },
{ 33, 123, 34, -1, -1, -1, -1, -1, -1 },
{ 13, 126, -1, -1, -1, -1, -1, -1, -1 },
{ 18, 33, 131, 34, -1, -1, -1, -1, -1 },
{ 23, 33, 122, 34, -1, -1, -1, -1, -1 },
};
/**
* サンプルmainメソッド.
* 単体テストの対象ではないので自由に改変しても良い.
*/
public void debug(final String message) {
if(DEBUG) {System.out.println(message);}
}
public static void main(final String[] args) {
// normalの確認
//new Parser().run("data/ts/normal01.ts");
//new Parser().run("data/ts/normal15.ts");
// synerrの確認
new Parser().run("data/ts/synerr02.ts");
//new Parser().run("data/ts/synerr02.ts");
}
/**
* TODO
*
* 開発対象となるParser実行メソッド.
* 以下の仕様を満たすこと.
*
* 仕様:
* 第一引数で指定されたtsファイルを読み込み,構文解析を行う.
* 構文が正しい場合は標準出力に"OK"を,正しくない場合は"Syntax error: line"という文字列とともに,
* 最初のエラーを見つけた行の番号を標準エラーに出力すること (例: "Syntax error: line 1").
* 入力ファイル内に複数のエラーが含まれる場合は,最初に見つけたエラーのみを出力すること.
* 入力ファイルが見つからない場合は標準エラーに"File not found"と出力して終了すること.
*
* @param inputFileName 入力tsファイル名
*/
ArrayList<String> program = new ArrayList<String>();
ArrayList<String> line = new ArrayList<String>();
public void run(final String inputFileName)
{
try {
String content = new Scanner(new File(inputFileName)).useDelimiter("\\Z").next();
StringTokenizer lines = new StringTokenizer(content, "\n");
while (lines.hasMoreTokens()) {
StringTokenizer words = new StringTokenizer(lines.nextToken(), "\t") ;
String next=words.nextToken();
next=words.nextToken();
next=words.nextToken();
program.add(next);
next=words.nextToken();
line.add(next);
}
if(search(100,0)>=0) {System.out.println("OK");}
else {System.err.println("Syntax error: line "+line_of_last_true);}
}
catch (FileNotFoundException b) {System.err.println("File not found");}
}
int search(int target, int offset) {
if(target<0) { debug("false "+target+",0"); return -1; }
else if(target<43) { }
else {
debug(target+","+(offset+1));
}
int i,value=0;
if(target<46) { //if Lowest level derivation
if(!(target==Integer.parseInt(program.get(offset)))) {
if(target<43) { debug(tokens[target][1]+","+(offset+1)); }
else { debug("false "+target+","+(offset+1)); }
return -1;
}
line_of_last_true = Integer.parseInt(line.get(offset));
if(target<43) {debug("true "+tokens[target][1]+","+(offset+1));}
else { debug("true "+target+","+(offset+1)); }
return 1;
}
int numberoftimes=0,length=0,lengthlastbranch=0,cumulative=0,flag=0;
switch(syntax[target%100][8]) { //if regular derivation,switch-case of different types of derivations
case -1: //CASE OF A
for(i=0;i<8;i++) {
if(target<46) {value=target;}
else {value=syntax[target%100][i];}
if(value>=0) {
if(lengthlastbranch>-1) {cumulative+=lengthlastbranch;}
lengthlastbranch=search(value,offset+cumulative);
if(lengthlastbranch<0) {
debug("false "+target+","+(offset+1));
return -1;
}
}
}
if(lengthlastbranch>-1) {length=cumulative+lengthlastbranch;}
debug("true "+target+","+(offset+1));
return length;
case -10: //CASE OF [A]
numberoftimes=0;
for(i=0;i<8;i++) {
if(target<46) {value=target;}
else {value=syntax[target%100][i];}
if(lengthlastbranch>-1) {cumulative+=lengthlastbranch;}
lengthlastbranch=search(value,offset+cumulative);
if(lengthlastbranch<0) { break; }
numberoftimes++;
}
length=cumulative;
if(numberoftimes==0 || value==-1) {debug("true "+target+","+(offset+1));
return length;}
debug("false "+target+","+(offset+1));
return -1;
case -100: //CASE OF A|B|C....|Z
for(i=0;i<8;i++) {
if(target<46) {value=target;}
else {value=syntax[target%100][i];}
if(value>=0) {
lengthlastbranch=search(value,offset);
if(lengthlastbranch>-1) {
debug("true "+target+","+(offset+1));
return lengthlastbranch;
}
}
}
debug("false "+target+","+(offset+1));
return -1;
case 0: //CASE OF {A}
numberoftimes=0;
for(i=0;i<Math.max((int)4*(program.size()-program.size()%8),8);i++) {
if(target<46) {value=target;}
else {value=syntax[target%100][i%8];}
if(value>=0) {
if(lengthlastbranch>-1) {cumulative+=lengthlastbranch;}
lengthlastbranch=search(value,offset+cumulative);
if(lengthlastbranch<0) { break; }
}
numberoftimes++;
}
length=cumulative;
if(numberoftimes==0||numberoftimes%8==0) {debug("true "+target+","+(offset+1));
return length;}
debug("false "+target+","+(offset+1));
return -1;
case 100: //CASE OF A{A}
numberoftimes=0;
for(i=0;i<Math.max((int)4*(program.size()-program.size()%8),8);i++) {
if(target<46) {value=target;}
else {value=syntax[target%100][i%8];}
if(value>=0) {
if(lengthlastbranch>-1) {cumulative+=lengthlastbranch;}
if(i>7) {lengthlastbranch=search(value,offset+cumulative);}
else{lengthlastbranch=search(value,offset+cumulative);}
if(lengthlastbranch<0) { break; }
}
numberoftimes++;
}
length=cumulative;
if(numberoftimes%8==0&&numberoftimes>0) {debug("true "+target+","+(offset+1));
return length;}
debug("false "+target+","+(offset+1));
return -1;
case 99: //CASE OF A{;A}
numberoftimes=0;
for(i=0;i<Math.max((int)4*(program.size()-program.size()%9),9);i++) {
if(target<46) {value=target;}
else {value=syntax[target%100][i%9];}
if(i%9==8&&value>=0) {
if(lengthlastbranch>-1) {cumulative+=lengthlastbranch;}
lengthlastbranch=search(37,offset+cumulative);
if(lengthlastbranch<0) { break; }
}
else if(value>=0) {
if(lengthlastbranch>-1) {cumulative+=lengthlastbranch;}
if(i>8) {lengthlastbranch=search(value,offset+cumulative);}
else{lengthlastbranch=search(value,offset+cumulative);}
if(lengthlastbranch<0) { break; }
}
numberoftimes++;
}
length=cumulative;
if(numberoftimes%9==8 ) {debug("true "+target+","+(offset+1));
return length;}
debug("false "+target+","+(offset+1));
return -1;
case 98: //CASE OF A{,A}
numberoftimes=0;
for(i=0;i<Math.max((int)4*(program.size()-program.size()%9),9);i++) {
if(target<46) {value=target;}
else {value=syntax[target%100][i%9];}
if(i%9==8&&value>=0) {
if(lengthlastbranch>-1) {cumulative+=lengthlastbranch;}
lengthlastbranch=search(41,offset+cumulative);
if(lengthlastbranch<0) { break; }
}
else if(value>=0) {
if(lengthlastbranch>-1) {cumulative+=lengthlastbranch;}
if(i>8) {lengthlastbranch=search(value,offset+cumulative);}
else{lengthlastbranch=search(value,offset+cumulative);}
if(lengthlastbranch<0) { break; }
}
numberoftimes++;
}
length=cumulative;
if(numberoftimes%9==8 ) {debug("true "+target+","+(offset+1));
return length;}
debug("false "+target+","+(offset+1));
return -1;
default: debug("false "+target+","+(offset+1));
return -1;
}
}
}