compilers - lab5

  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
package compilers_lab5;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
public class Compilers_lab5 {
private static final int[][] table = new int[][]{
/* f o r a n g e a-z 0-9 X */
/* state 0 */{1, 4, 5, 4, 4, 4, 4, 4, 10, 11},
/* state 1 */ {4, 2, 4, 4, 4, 4, 4, 4, 4, 11},
/* state 2 */ {4, 4, 3, 4, 4, 4, 4, 4, 4, 11},
/* state 3 */ {4, 4, 4, 4, 4, 4, 4, 4, 4, 11},
/* state 4 */ {4, 4, 4, 4, 4, 4, 4, 4, 4, 11},
/* state 5 */ {4, 4, 4, 6, 4, 4, 4, 4, 4, 11},
/* state 6 */ {4, 4, 4, 4, 7, 4, 4, 4, 4, 11},
/* state 7 */ {4, 4, 4, 4, 4, 8, 4, 4, 4, 11},
/* state 8 */ {4, 4, 4, 4, 4, 4, 9, 4, 4, 11},
/* state 9 */ {4, 4, 4, 4, 4, 4, 4, 4, 4, 11},
/* state 10 */ {11, 11, 11, 11, 11, 11, 11, 11, 10, 11},
/* state 11 */ {11, 11, 11, 11, 11, 11, 11, 11, 11, 11}
};
public enum DomainTag {
END, ERROR, KEYW, IDENT, NUMBER
};
private static final DomainTag[] states = new DomainTag[]{
/* state 0 */DomainTag.END,
/* state 1 */ DomainTag.IDENT,
/* state 2 */ DomainTag.IDENT,
/* state 3 */ DomainTag.KEYW,
/* state 4 */ DomainTag.IDENT,
/* state 5 */ DomainTag.IDENT,
/* state 6 */ DomainTag.IDENT,
/* state 7 */ DomainTag.IDENT,
/* state 8 */ DomainTag.IDENT,
/* state 9 */ DomainTag.KEYW,
/* state 10*/ DomainTag.NUMBER,
/* state 11*/ DomainTag.ERROR,};
private static String getText(String path) {
File file = new File(path);
String result = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
while (reader.ready()) {
result += reader.readLine();
result += '\n';
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
HashMap<Character, Integer> hashtable = new HashMap<Character, Integer>();
for (char i = 0; i < 256; i++) {
hashtable.put(i, 9);
}
for (char i = 'a'; i <= 'z'; i++) {
hashtable.put(i, 7);
}
for (char i = '0'; i <= '9'; i++) {
hashtable.put(i, 8);
}
hashtable.put('f', 0);
hashtable.put('o', 1);
hashtable.put('r', 2);
hashtable.put('a', 3);
hashtable.put('n', 4);
hashtable.put('g', 5);
hashtable.put('e', 6);
String program = getText("C:\\Users\\Марина\\Documents\\NetBeansProjects\\JavaApplication3\\test\\f");
Position cur = new Position(program);
while (cur.Cp() != (char) -1) {
while (cur.IsWhiteSpace()) {
cur = cur.Inc();
}
int state = 0;
String attr = "";
Position start = cur;
while (!cur.IsWhiteSpace() && !cur.IsNewLine() && cur.Cp() != -1) {
state = table[state][hashtable.get(cur.Cp())];
attr += Character.toString(cur.Cp());
cur = cur.Inc();
}
Position end = cur;
System.out.println(states[state].toString() + " (" + start.toString() + '-'
+ end.toString() + ") " + attr);
}
}
}
package compilers_lab5;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class Position implements Comparable<Position> {
private String text;
private int line, pos, index;
public Position(String text) {
this.text = text;
line = pos = 1;
index = 0;
}
private Position(String text, int line, int pos, int index) {
this.text = text;
this.line = line;
this.pos = pos;
this.index = index;
}
public int Line() {
return line;
}
public int Pos() {
return pos;
}
public int Index() {
return index;
}
@Override
public int compareTo(Position other) {
return Integer.compare(index, other.index);
}
@Override
public String toString() {
return "(" + line + "," + pos + ")";
}
public char Cp() {
if (index == text.length()) {
return (char) -1;
} else {
return text.charAt(index);
}
}
public boolean IsWhiteSpace() {
return index != text.length() && Character.isWhitespace(Cp());
}
public boolean IsLetter() {
return index != text.length() && Character.isLetter(Cp());
}
public boolean IsLetterOrDigit() {
return index != text.length() && Character.isLetterOrDigit(Cp());
}
public boolean IsDigit() {
return index != text.length() && Character.isDigit(Cp());
}
public boolean IsNewLine() {
if (index == text.length()) {
return true;
}
if (Cp() == '\r' && this.Inc().index < text.length()) {
return this.Inc().Cp() == '\n';
}
return Cp() == '\n';
}
public Position Inc() {
int indexNew = index, lineNew = line, posNew = pos;
if (index < text.length()) {
if (IsNewLine()) {
if (text.charAt(index) == '\r') {
indexNew++;
}
lineNew++;
posNew = 1;
} else {
posNew++;
}
indexNew++;
}
return new Position(text, lineNew, posNew, indexNew);
}
}