Repository navigation
Expand file tree
/
Copy pathMiniJavaCompiler.java
More file actions
76 lines (64 loc) · 1.87 KB
/
Copy pathMiniJavaCompiler.java
File metadata and controls
76 lines (64 loc) · 1.87 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
//Mark Klara
//mak241@pitt.edu
//CS 1622 - Project 3
//MiniJavaCompiler.java
import java_cup.runtime.*;
import java.io.*;
import syntaxtree.*;
import visitor.*;
import symboltable.*;
public class MiniJavaCompiler
{
public static void main(String[] args)
{
if(args.length != 1)
{
System.out.println("Usage: java MiniJavaCompiler file.java");
System.exit(1);
}
Symbol parse_tree = null;
try
{
Reader reader = new java.io.InputStreamReader(new java.io.FileInputStream(args[0]), "UTF-8");
Parser parser = new Parser(new Lexer(reader));
parse_tree = parser.parse();
Program program = ((Program) parse_tree.value);
if(program != null)
{
if(parser.errorDetected == false)
{
//Visit the tree starting at root (program) to build the symbol table
BuildSymbolTableVisitor bstVisitor = new BuildSymbolTableVisitor();
bstVisitor.visit(program);
//Get a copy of the symboltable
Scope symbolTable = bstVisitor.getFirstScope();
if(symbolTable == null) //Check for error making symbol table
{
System.err.println("Oh no... the symbol table is null!");
return;
}
//Visit the tree starting at the root to check for undefined variables
UndefinedVariableVisitor undefinedVisitor = new UndefinedVariableVisitor(symbolTable);
undefinedVisitor.visit(program);
//Visit the tree starting at the root to check for type errors
//...TODO
if(bstVisitor.errorDetected | undefinedVisitor.errorDetected) //If any errors occured, do not generate IR
{
return;
}
//Generate IR
IRVisitor intermediateVisitor = new IRVisitor(symbolTable);
intermediateVisitor.visit(program);
}
}
}
catch (java.io.IOException e)
{
System.err.println("Unable to open file: " + args[0]);
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
}