Repository navigation
Expand file tree
/
Copy pathScanner.java
More file actions
93 lines (81 loc) · 3.05 KB
/
Copy pathScanner.java
File metadata and controls
93 lines (81 loc) · 3.05 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
import java.io.IOException;
import java.util.*;
import error.ErrorMessage;
import scanner.DFA;
import scanner.Regex2NFA;
import scanner.RegexUtil;
public class Scanner {
/*moved up from FileReader*/
private final FileReader reader;
private Character inputSym; // the current character on the input
private final StringBuilder substring;
private static Map<String, DFA> matcher;
private int lastToken = -1;
private String lastValue;
private void Next() throws IOException {
inputSym = reader.getSym();
} // advance to the next character
// Error == 0, EOF == 255
/* symmetrical to the changed FileReader class */
public int getSym() throws IOException, ErrorMessage {
if (substring.length() == 0) {
lastToken = -1;
}
if (inputSym == null) {
lastToken = 255;
substring.setLength(0);
}
while (inputSym != null) {
if (!RegexUtil.alphabet.contains(inputSym)) {
throw new ErrorMessage("Lexical Analysis", "Valid character", "Input character " + inputSym + " not in alphabet!");
}
substring.append(inputSym);
int localToken = -1;
boolean match = false;
for (String reg : matcher.keySet()) {
boolean result = matcher.get(reg).search(substring.toString());
match |= result;
if (result) {
if (localToken == -1 || localToken == 61) {
localToken = RegexUtil.regex2id.get(reg);
}
}
}
if (lastToken == 254) { // comment
while (inputSym != null && inputSym != '\n') {
Next();
}
Next();
substring.setLength(0);
return getSym();
}
if (match) {
Next();
lastToken = localToken;
lastValue = substring.toString();
} else if (lastToken != -1) {
substring.setLength(0);
return lastToken;
} else if (substring.length() == 1 && (inputSym == '\t' || inputSym == ' ' || inputSym == '\n' || inputSym == '\r')) {
Next();
substring.setLength(0);
return getSym();
} else {
Next();
}
}
return lastToken;
} // return current and advance to the next token on the input
public String getLastValue() {
return lastValue;
}
public Scanner(String fileName) throws IOException, ErrorMessage {
reader = new FileReader(fileName);
substring = new StringBuilder();
matcher = new HashMap<>();
for (String reg : RegexUtil.regex2id.keySet()) {
matcher.put(reg, Regex2NFA.regex2NFA(reg).NFA2DFA());
}
Next();
} // constructor: open file and scan the first token into 'sym'
}