lab7_compilers -

  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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* Created by natalia on 05.04.14.
*/
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.util.ArrayList;
class Reader {
private File openedFile_;
private BufferedReader reader_;
private ArrayList<String> readedText_;
public Reader(String path) {
openedFile_ = new File(path);
try {
reader_ = new BufferedReader(new InputStreamReader(new FileInputStream(openedFile_), "UTF-8"));
} catch (UnsupportedEncodingException exc) {
System.out.println(" UTF-8 exception. " + exc.getMessage());
} catch (FileNotFoundException exc) {
System.out.println("File Not Found. " + exc.getMessage());
}
readedText_ = new ArrayList<String>();
}
public ArrayList<String> Read() {
String currentString;
try {
while((currentString = reader_.readLine()) != null) {
readedText_.add(currentString);
}
} catch (IOException exc) {
System.out.println("IOException while reading file. " + exc.getMessage());
}
return new ArrayList<String>(readedText_);
}
}
class Parse {
private String axiom;
private List<String> nTerm;
private List<String> Term;
HashMap<String, List<String[]>> lRule_rRule;// = new HashMap<String, List<String>>();
public Parse() {
nTerm = new LinkedList<String>();
Term = new LinkedList<String>();
lRule_rRule = new HashMap<String, List<String[]>>();
String s = "bla";
//if(s.equals("bla"))
// System.out.println("yeee ! start with bla");
//else
// System.out.println("oop");
}
public void addNTerm(String Nterms) {
Nterms = Nterms.substring(0, Nterms.length()-1);
String [] terms = Nterms.split(" ") ;
for(int i = 1; i < terms.length; i++) {
nTerm.add(terms[i]);
// System.out.println("nterms: " + terms[i]);
}
//nTerm.add(Nterm);
}
public void addTerm(String T) {
T = T.substring(0, T.length()-1);
String [] terms = T.split(" ") ;
for(int i = 1; i < terms.length; i++) {
Term.add(terms[i].split("\"")[1]);
System.out.println("terms: " + terms[i].split("\"")[1]);
}
//System.out.println("!!! " + F("*hhh"));
}
public void addAxiom(String a) {
a = a.substring(0,a.length()-1);
String toAdd = a.split(" ")[1];
axiom = toAdd;
nTerm.add(toAdd);
System.out.println("axiom: " + axiom);
}
public void addRule(String lr, String rr) {
String []rRules = rr.split("\n");
System.out.println("rules: " + lr + " - " + rr);
lRule_rRule.put(lr, new LinkedList<String[]>());
for(int i = 0; i < rRules.length; i++) {
String [] s = rRules[i].split(" ");
String [] news = new String[s.length-1];
for(int j = 1; j < s.length; j++) {
if(s[j].charAt(0) == '\"')
s[j] = s[j].split("\"")[1];
news[j - 1] = s[j];
}
//System.out.println(" !"+s[j]+"! ");
lRule_rRule.get(lr).add(news);
//System.out.println("part_of_rule: " + news[0]);
}
}
private List<String> F(String s) {
if(s.length() < 1)
return null;
List<String> result = new LinkedList<String>();
boolean notTerm = true;
for(String t: Term)
if (s.startsWith(t)) {
result.add(t);
notTerm = false;
}
if(notTerm) {
if(s.equals("$EPS"))
result.add("$EPS");
else {
String nt = "";
for(String t: nTerm) {
String f = s.substring(0, t.length());
if(t.equals(f))
nt = f;
}
if(FIRST.get(nt) != null) {
if (!FIRST.get(nt).contains("$EPS")) {
return FIRST.get(nt);
} else {
List<String> copy = new LinkedList<String>();
for(String l: FIRST.get(nt))
copy.add(l);
copy.remove("$EPS");
return Union(copy, F(s.substring(nt.length(), s.length())));
}
} else
return null;
}
}
return result;
}
HashMap<String, List<String>> FIRST = new HashMap<String, List<String>>();
public void createFirst() {
System.out.println("StartFirst");
for(String nt: nTerm)
FIRST.put(nt, new LinkedList<String>());
boolean isChanging = true;
while (isChanging) {
isChanging = false;
for (String lR : nTerm)
for (String[] rR : lRule_rRule.get(lR)) {
String Fu = "";
for (String a : rR)
Fu += a;
List<String> cF = F(Fu);
System.out.println("FU: " + cF + "for: " + Fu);
if (cF != null)
if (!cF.isEmpty()) {
//isChanging = true;
List<String> oldF = FIRST.get(lR);
List<String> newF = Union(oldF, cF);
for(String l: newF) {
if(!oldF.contains(l)) {
FIRST.put(lR, newF);
isChanging = true;
}
}
}
}
}
for(String l: nTerm)
System.out.println("nt: "+l + " f: " + FIRST.get(l));
}
private List<String> Union(List<String> a, List<String> b) {
List<String> union = new LinkedList<String>();
if(a != null)
for(String l: a) {
union.add(l);
}
if(b != null)
for(String l: b) {
if(!union.contains(l))
union.add(l);
}
return union;
}
HashMap<String, List<String>> FOLLOW = new HashMap<String, List<String>>();
public void countFollow() {
for(String nt: nTerm)
FOLLOW.put(nt, new LinkedList<String>());
FOLLOW.get(axiom).add("$");
for(String nt: nTerm) {
for(String []rR: lRule_rRule.get(nt)) {
for (int i = 0; i < rR.length; i++) {
if(nTerm.contains(rR[i])) {
String tail = "";
for(int j = i + 1; j < rR.length; j++)
tail += rR[j];
List<String> oldF = FOLLOW.get(rR[i]);
FOLLOW.put(rR[i], Union(oldF, F(tail)));//???
}
}
}
}
boolean isChanged = true;
while(isChanged) {
isChanged = false;
for (String l: nTerm) {
}
}
for(String l: nTerm)
System.out.println("folnt: "+l + " f: " + FOLLOW.get(l));
}
public void print() {
System.out.println("terms: ");
for(String l: Term)
System.out.println("-"+l+"-");
System.out.println("nterms: ");
for(String l: nTerm)
System.out.println("-"+l+"-");
System.out.println("rules: ");
for(String l: lRule_rRule.keySet()) {
System.out.println("="+l+"=");
for(String []e: lRule_rRule.get(l)) {
System.out.println("[");
for (String a: e)
System.out.println("-"+a+"-");
System.out.println("]");
}
}
}
}
public class lex {
static private int strNum = 0;
static private Parse parse = new Parse();
private void print(String s, int pos, int mend) {
// System.out.println("IDENT ("+strNum+","+pos+")-("+strNum+","+(pos+mend)+"): "+s);
}
private static void findLex2(String s, Pattern p) {
int pos = 0;
Matcher m;
while(s.length() != 0){
m = p.matcher(s);
if(m.find()) {
if (m.group(1) != null) {
// System.out.println("RULE II ("+strNum+","+pos+")-("+strNum+","+
// (pos+m.end())+"): "+m.group(1));
parse.addRule(m.group(2), m.group(3));
}
/*for(int i = 0; i < 5; i++) {
if(m.group(i) != null)
System.out.println(i+ " g: " + m.group(i));
}*/
pos += m.end();
s = s.substring(m.end());
//System.out.println(s);
//for(int i = 1; i< 7;i++)
// System.out.println("g: " +m.group(i));
} else {
pos++;
if(s.charAt(0) != ' ') {
System.out.println("ERROR at (" + strNum + "," + pos + ")");
}
s = s.substring(1);
}
}
strNum++;
}
private static void findLex(String s, Pattern p) {
int pos = 0;
Matcher m;
while(s.length() != 0){
m = p.matcher(s);
if(m.find()) {
if (m.group(1) != null) {
// System.out.println("AXIOM ("+strNum+","+pos+")-("+strNum+","+
// (pos+m.end())+"): "+m.group(1));
parse.addAxiom(m.group(1));
} else if(m.group(5) != null) {
// System.out.println("TERM ("+strNum+","+pos+")-("+strNum+","+
// (pos+m.end())+"): "+m.group(5));
parse.addTerm(m.group(5));
} else if(m.group(3) != null) {
// System.out.println("NTERM ("+strNum+","+pos+")-("+strNum+","+
// (pos+m.end())+"): "+m.group(3));
parse.addNTerm(m.group(3));
} else {
// System.out.println("RULES-to-be-continued ("+strNum+","+pos+")-("+strNum+","+
// (pos+m.end())+"): "+m.group(7));
findLex2(m.group(7), Pattern.compile("\\$RULE (([A-Z]\\'{0,1}) =(([( [A-Z]\\'{0,1})( \"[a-z]\"| \"\\*\"| \"\\+\"| \"\\(\"| \"\\)\")]+\\n| \\$EPS\\n)+))"));
}
pos += m.end();
s = s.substring(m.end());
//System.out.println(s);
//for(int i = 1; i< 7;i++)
// System.out.println("g: " +m.group(i));
} else {
pos++;
if(s.charAt(0) != ' ') {
System.out.println("ERROR at (" + strNum + "," + pos + ")");
}
s = s.substring(1);
}
}
strNum++;
}
public static void splitLexems(String rules) {
}
public static void main(String args[])
{
// Текст для сопоставления
String text = "$AXIOM E\n" +
"$TERM";
// Регулярные выражения
String ident = "^\\p{L}.{0,8}\\p{L}";//"^[A-Za-z]+|^[(][0-9]+[)]";+";//
//String number = //"^0|^[1-9][0-9]*";
//String oper = //"^[(][)]|^[:][=]|^[:]";
String strlit = "^\'([^\\\\]|\\\\n|\\\\\'|\\\\[0-9A-Fa-f]{4,4}|\\p{L})*\'";//|[[\\][n]]*|[[\\][0-9A-Fa-f]{,4}]*|[.]*][\']\'";
String keyword = "^z|^forward|^for";
String pattern = "("+keyword+")|("+strlit+")|("+ident+")";
String nt = "[A-Z]\\'{0,1}";
String t = "\"[a-z]\"| \"\\*\"| \"\\+\"| \"\\(\"| \"\\)\"";
String axiom = "\\$AXIOM ([A-Z]\\\'{0,1})\\n";
String nterm = "\\$NTERM( [A-Z]\\'{0,1})+\\n";
String term = "\\$TERM( \"[a-z]\"| \"\\*\"| \"\\+\"| \"\\(\"| \"\\)\")+\\n";
String rule = "\\$RULE ([A-Z]\\'{0,1} =([( [A-Z]\\'{0,1})( \"[a-z]\"| \"\\*\"| \"\\+\"| \"\\(\"| \"\\)\")]+\\n| \\$EPS\\n)+)";
pattern = "("+axiom+")|("+nterm+")|("+term+")|(("+rule+")+)";
//String
// Компиляция регулярного выражения
Pattern p = Pattern.compile(pattern);
Reader reader = new Reader("test.txt");
ArrayList<String> mas = reader.Read();
//System.out.println(reader1.Read());
// Сопоставление текста с регулярным выражением
String program = "";
for(String s: mas)
program += (s+'\n');
findLex(program, p);
parse.print();
parse.createFirst();
parse.countFollow();
for (String s : mas) {
//System.out.println();
//findLex(s+'\n', p);
}
}
}