-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
74 lines (63 loc) · 2.32 KB
/
Main.java
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
import java.io.*;
import java_cup.runtime.*;
/****
* Main program to run the minim compiler.
*
* There should be 2 command-line arguments:
* 1. the input file containing the minim source
* 2. the output file into which the MIPS code generated by the compiler
* should be printed
*
* The program opens the two files, creates a scanner and a parser, and
* calls the parser. If the parse is successful, then it will call name
* analysis and type checking routines. If there is no error at the end,
* it will generate MIPS code to the output file.
****/
public class P6 {
public static void main(String[] args)
throws IOException // may be thrown by the scanner
{
// check for command-line args
if (args.length != 2) {
System.err.println("please supply name of source (minim) file " +
"and name of file for target (MIPS).");
System.exit(-1);
}
// open input file
FileReader inFile = null;
try {
inFile = new FileReader(args[0]);
} catch (FileNotFoundException ex) {
System.err.println("file " + args[0] + " not found");
System.exit(-1);
}
// open output file
PrintWriter outFile = null;
try {
Codegen.p = new PrintWriter(args[1]);
} catch (FileNotFoundException ex) {
System.err.println("file " + args[1] +
" could not be opened for writing");
System.exit(-1);
}
parser P = new parser(new Yylex(inFile));
Symbol root = null; // the parser will return a Symbol whose value
// field is the translation of the root nonterminal
// (i.e., of the nonterminal "program")
try {
root = P.parse(); // do the parse
} catch (Exception ex){
System.err.println("exception occured during parse: " + ex);
System.exit(-1);
}
((ProgramNode)root.value).nameAnalysis(); // perform name analysis
if (!ErrMsg.getErr()) { // if no errors, do type checking
((ProgramNode)root.value).typeCheck();
}
if (!ErrMsg.getErr()) { // if no errors, do code generation
((ProgramNode)root.value).codeGen();
}
Codegen.p.close();
return;
}
}