forked from Luno82o/Scanner_bonus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.java
More file actions
113 lines (98 loc) · 3.22 KB
/
Copy pathScanner.java
File metadata and controls
113 lines (98 loc) · 3.22 KB
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
import java.io.*;
import java.util.*;
public class Scanner {
// static ArrayList<String> tokenBuf = new ArrayList<String>();
Token tokens = new Token();
public void readTxt(String filename) {
try {
//讀取檔案
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String inputLine;
//逐行讀取
while (br.ready()) {
inputLine = br.readLine();
System.out.println(inputLine);
splitToken(inputLine);
}
fr.close();
}catch (Exception e) {
System.out.println("[Error] "+filename+" can't be opened.\n");
e.printStackTrace();
}
}
public void splitToken(String inputLine) {
int token_start = 0, token_end = 0;
// System.out.println("inputLine.length: "+inputLine.length());
while(token_start < inputLine.length()) {
for(int i = token_start; i < inputLine.length(); i++) {
// System.out.println(inputLine.charAt(i));
// System.out.println("i = "+i);
if(!Character.isLetterOrDigit(inputLine.charAt(i))) {
token_end = i;
break;
}
}
// System.out.println("token_start:"+token_start);
// System.out.println("token_end:"+token_end);
if(token_end-token_start == 0) {
char buf;
buf = inputLine.charAt(token_start);
if(buf!=' ') {
// System.out.println(buf);
tokens.addTokenBuf(Character.toString(buf));
}
token_start = token_end + 1;
} else {
char buf[] = new char[(token_end-token_start) + 1];
inputLine.getChars(token_start, token_end, buf, 0);
// System.out.println(buf);
tokens.addTokenBuf(String.valueOf(buf));
token_start = token_end;
}
// System.out.println(tokenBuf);
}
// switch(token) {
// case "include":
//
// break;
// case "main":
// break;
// case "char":
// break;
// case "int":
// break;
// case "float":
// break;
// case "if":
// break;
// case "else":
// break;
// case "elseif":
// break;
// case "for":
// break;
// case "while":
// break;
// case "do":
// break;
// case "return":
// break;
// case "switch":
// break;
// case "case":
// break;
// case "printf":
// break;
// case "scanf":
// break;
// default:
// }
//
//
// }else {
// System.out.println("no");
// }
}
}
//System.out.println(valueName.getClass().getSimpleName()); //取得變數的type