Компиляторы 4 лаба (7) Reader & Main

 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
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.util.ArrayList;
import java.io.File;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.lang.String;
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_);
}
}
public class main {
public static void main(String args[]) {
String program;
try {
File f = new File("C:/Users/Denis/file.txt");
char[] CB = new char[(int) f.length()];
Reader reader = new InputStreamReader(new FileInputStream(f), "UTF-8");
reader.read(CB);
program = new String(CB);
} catch (Exception ex) {
System.out.println(ex);
return;
}
Compiler comp = new Compiler();
Scanner scan = new Scanner(program, comp);
Token t;
while( (t = scan.NextToken()).Tag() != DomainTag.END_OF_PROGRAM){
System.out.print(t.Tag() + " ");
System.out.print(t.Coords() + " ");
switch (t.Tag()){
case IDENT:
System.out.print((((IdentToken) t).Code) + "\n");
break;
case OPER:
System.out.print(((OperToken)t).Num + "\n");
break;
case NUMBER:
System.out.print(((NumberToken)t).Value + "\n");
break;
}
}
comp.OutputMessages();
}
}