-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCD.java
More file actions
executable file
·201 lines (147 loc) · 4.77 KB
/
CD.java
File metadata and controls
executable file
·201 lines (147 loc) · 4.77 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
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
/**
* CD18 Compiler
*
* Assignmen 3: Parser
*
* @author Zaw Moe Htat (c3193528@uon.edu.au)
*/
import scanner.Scanner;
import scanner.ScannerOutput;
import codegen.*;
import model.*;
import parser.*;
import java.io.*;
import java.util.*;
public class CD {
public static void main(String[] args) {
// Flag for lexical error
boolean lexicalSuccess = true;
// Check the arguments
if (args.length > 0) {
// Scanner output handler
ScannerOutput scannerOutput = new ScannerOutput();
// Scanner to scan the program
Scanner scanner = null;
// Start scanning the program
try {
// Load the CD18 source file
scanner = new Scanner(args[0]);
System.out.println();
System.out.println("Tokens - Lexical Analyser");
System.out.println("--------------------------------");
// Scan line by line
while(scanner.readLine() != null) {
// Scan word by word in the line
while(scanner.scanNext()) {
// Check if the stat changed
if (scanner.isStateChanged() == true) {
// Check if there is an error
if (scanner.getErrorHandler().isEmpty() == false) {
// Print lexical error
scannerOutput.printError(scanner.getToken(), scanner.getErrorHandler());
lexicalSuccess = false;
} else {
// Print tokens out
scannerOutput.print(scanner.getToken());
}
}
}
}
scannerOutput.end();
} catch(IOException e) {
e.printStackTrace();
}
System.out.println("================================================================================");
// Program Listing
LinkedList<String> programListing = scanner.getProgramListing();
/*System.out.println();
System.out.println("Program Listing");
System.out.println("----------------------");*/
try {
PrintWriter programListingWriter = new PrintWriter("programlisting.txt", "UTF-8");
ListIterator programListingIterator = programListing.listIterator();
while(programListingIterator.hasNext()) {
programListingWriter.println(programListingIterator.next());
}
programListingWriter.close();
} catch(IOException e) {
e.printStackTrace();
}
//System.out.println("================================================================================");
// List of tokens from lexical analyser
LinkedList<Token> tokens = scanner.getAllTokens();
// If lexical errors are free
if (lexicalSuccess == true) {
// Prepare to parse the list of token
Parser parser = new Parser(tokens);
// Parse the tokens list
TreeNode treeNode = parser.parse();
// Parser header
System.out.println();
System.out.println("Syntax Tree - Syntactic Analyser");
System.out.println("--------------------------------------");
// Parser output handler
ParserOutput parserOutput = new ParserOutput();
// Print syntax tree
parserOutput.print(treeNode);
// Syntax error handler
LinkedList<ErrorHandler> errorHandler = parser.getErrorHandler();
if (errorHandler.size() > 0) {
System.out.println("\n");
parserOutput.printError(errorHandler);
}
// Semantic error
LinkedList<ErrorHandler> semanticErrorHandler = parser.getSemanticErrorHandler();
if (semanticErrorHandler.size() > 0) {
System.out.println("\n");
parserOutput.printSemanticError(semanticErrorHandler);
}
// Number of errors
int numErr = errorHandler.size() + semanticErrorHandler.size();
if (numErr > 0) {
System.out.println();
System.out.println();
System.out.println("Found " + numErr + " errors");
} else {
System.out.println();
System.out.println();
System.out.println("No errors found");
// Code Generator
CodeGenerator codegen = new CodeGenerator();
codegen.generate(treeNode);
String fDirChunk[] = args[0].split("/");
String fnameChunk[] = fDirChunk[fDirChunk.length - 1].split("\\.");
String fname = fnameChunk[0] + ".mod";
try {
PrintWriter writer = new PrintWriter(fname, "UTF-8");
ListIterator l = codegen.getInstructions().listIterator();
writer.println(codegen.getInstructions().size());
int numLine = 0;
while(l.hasNext()) {
if (numLine < (codegen.getInstructions().size() - 1)) {
writer.println(l.next());
} else {
writer.print(l.next());
}
numLine++;
}
writer.close();
} catch(IOException e) {
e.printStackTrace();
}
System.out.println();
System.out.println(fname + " compiled successfuly");
}
} else {
System.out.println();
System.out.println("Lexical errors.");
}
} else {
System.out.println();
System.out.println("Source file is missing.");
System.out.println();
}
// Just a new line.
System.out.println("\n");
}
}