-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyScanner.java
252 lines (192 loc) · 8.42 KB
/
MyScanner.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
/*
Author: Liam O'Shea
Date: June 06 2019
File: MyScanner.java
Description: The following program implements a scanner which reads from standard input and outputs a token
stream to standard output.
*/
import java.util.ArrayList;
import java.util.Scanner;
public class MyScanner {
private static Scanner in = new Scanner(System.in); //Scanner used only to read individual lines from standard input.
//Implementation of token stream done by scanner written in this file.
private static String line, token = ""; //line holds a line of input from standard in.
//token is a string which is used to construct tokens with state machine.
private static char chars[]; //line will be converted to character array for easier manipulation
//List of all operators and delimiters
private static char opDelim[] = {'!','%','&','|','*','-','/','+','=','#','<','>','@','(',')','[',']','{','}',','};
//Keep track of line, index (col). and state.
private static int lineNum = 1, state = 0, i;
public static ArrayList<Token> getTokens(){
/*
Description of states in state machine.
State Description Final
0 Initial No
1 Operator/Delim Yes
2 StringLiteralInQuotes No //Concludes after final "
3 IntegerLiteral Yes
4 Symbol/Keyword Yes
*/
int stringInitial = 0; //Used to keep track of initial column of tokens greater than 1 character.
newCharArray(); //Updates array to represent next line;
ArrayList<Token> tokens = new ArrayList<Token>();
//Main program loop
while(true){
//Initial State
if(state == 0){
//Check if we have ended the line, print token if necessary
if(i >= chars.length){
if(token.length() > 0){
//System.out.println("line " + lineNum + " col " + (i) + " : " + chars[i-1]);
//Create token and add to ArrayList. Adding char to "" creates String.
tokens.add(new Token(lineNum, i, "" + chars[i-1]));
token = "";
}
//Get next line if available
if(in.hasNext()) {
newCharArray();
lineNum++;
}
}
//Check if we have an empty line, if so skip it
//The character ~ is used a symbol to represent an empty line in this implementation.
if(chars[i] == '~'){
lineNum++;
newCharArray();
}
//Deal with leading spaces
if(chars[i] == ' '){
i++;
}
//Operators and Delimiters
else if(containsOpDe(chars[i])){
//System.out.println("line " + lineNum + " col " + (i+1) + " : " + chars[i]);
tokens.add(new Token(lineNum, i+1, "" + chars[i]));
i++;
}
//String Literals
else if(chars[i] == '"'){
token += '"';
stringInitial = i;
i++;
state = 2;
}
//Integer Literals
//ASCII: 0->9 == 48->57
else if(chars[i] >= 48 && chars[i] <= 57){
token += chars[i];
stringInitial = i;
i++;
state = 3;
}
//Symbols/Keywords -- Conditions A-Z, a-z, _ respectively
else if((chars[i] >= 65 && chars[i] <= 90) || (chars[i] >= 97 && chars[i] <= 122) || chars[i] == 95){
token += chars[i];
stringInitial = i;
i++;
state = 4;
}
}
//State 2 - String Literal (Inside Quotations)
if (state == 2){
//If inside quotations, add any character to token.
if(chars[i] != '"'){
token += chars[i];
i++;
}
//Detect end quotations, print token.
else if(chars[i] == '"'){
token += chars[i];
i++;
//Output token information
//System.out.println("line " + lineNum + " col " + (stringInitial + 1) + " : " + token);
tokens.add(new Token(lineNum, stringInitial + 1, token));
token = "";
state = 0;
}
}
//State 3 - Integer Literal
if (state == 3){
//Check if we have ended the line, print token if necessary
if(i >= chars.length){
if(token.length() > 0){
//System.out.println("line " + lineNum + " col " + (stringInitial + 1) + " : " + token);
tokens.add(new Token(lineNum, stringInitial + 1, token));
token = "";
}
//Retrieve next line if available.
if(in.hasNext()) {
newCharArray();
lineNum++;
state = 0;
}
}
//Add to token if character is a numeral
else if (chars[i] >= 48 && chars[i] <= 57){
token += chars[i];
i++;
}
//Terminate if not a number
else{
//Output token information
//System.out.println("line " + lineNum + " col " + (stringInitial + 1) + " : " + token);
tokens.add(new Token(lineNum, stringInitial + 1, token));
token = "";
state = 0;
}
}
//State 4 - Symbol/Keywords
if (state == 4){
//Check if we have ended the line, print token if necessary
if(i >= chars.length){
if(token.length() > 0){
//System.out.println("line " + lineNum + " col " + (stringInitial + 1) + " : " + token);
tokens.add(new Token(lineNum, stringInitial + 1, token));
token = "";
}
//Get next line if available
if(in.hasNext()) {
newCharArray();
lineNum++;
state = 0;
}
}
//Symbols/Keywords -- Conditions A-Z, a-z, 0-9, _ respectively
else if((chars[i] >= 65 && chars[i] <= 90) || (chars[i] >= 97 && chars[i] <= 122) ||
(chars[i] >= 48 && chars[i] <= 57) || chars[i] == 95){
token += chars[i];
i++;
}
else{
//Output token information
//System.out.println("line " + lineNum + " col " + (stringInitial + 1) + " : " + token);
tokens.add(new Token(lineNum, stringInitial + 1, token));
token = "";
state = 0;
}
}
//Exit condition
if(!in.hasNext() && token.length() == 0 && i >= line.length()) break;
}
return tokens;
}
//containsOpDe(char) returns true if input is contained in list of operators and delimiters.
public static boolean containsOpDe(char input){
for(int j = 0; j < opDelim.length; j++){
if (input == opDelim[j]) return true;
}
return false;
}
//newCharArray() retrieves the next line from standard in and returns a character array of that line.
public static char[] newCharArray(){
line = in.nextLine();
//Check to see if line is empty. If so set char to ~ symbol to indicate it is a line to be skipped.
if(line.length() > 0) {
chars = line.toCharArray();
} else{
chars = "~".toCharArray();
}
i = 0;
return chars;
}
}