-
Notifications
You must be signed in to change notification settings - Fork 1
/
notes_p1.txt
118 lines (81 loc) · 4.46 KB
/
notes_p1.txt
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
Phase 1:
The goal of phase 1 is to write a type checker for MiniJava. Given a program, the type checker checks at compile time that type mismatch does not happen at run time.
It either approves or rejects the program. The set of rules that the type checker checks are represented as a type system.
We must first create a parser for the MiniJava language and generate a set of syntax tree classes and visitor classes.
To do this, complete the following steps:
$ java -jar /path/to/jtb.jar /path/to/minijava.jj
$ javacc jtb.out.jj
Once this is done, you will have a complete parser for MiniJava and a set of classes used for traversing the syntax tree.
You will also have two different default visitors: DepthFirstVisitor and GJDepthFirst. You should extend these two visitors to type check a MiniJava program.
Your main file should be called Typecheck.java, and if P.java contains a program to be type checked, then:
$ java Typecheck < P.java
should print either Program type checked successfully or Type error.
Untar the following Phase1Tester.tar.gz and read the instructions in the ReadMe.txt file.
Your submission must work with the testing script when run on the department servers.
Make sure you test your final tarball before submitting.
========================================
========================================
SIDE NOTES:
MiniJava is a subset of java that includes the bare minimum of Java: integers, integer arrays, classes, subclasses, and printing integers to standard out.
It does not permit any float types, strings, overloading and overriding methods, or any interfaces. It has a few other restrictions, but those are minor.
Thus, the MiniJava statement System.out.println(...); can only print integers. The MiniJava expression e.length only applies to expressions of type int[].
Missing Operations:
divide, modulo, or, ...
Special cases:
no private or protected methods
System.out.println is a special case
main(String a[]) is a special case, there are no strings
create new visitors based on DepthFirstVisitor and GJDepthFirst
note: really trying to avoid heavily nested try/catch blocks by declaring the exceptions thrown upfront
OUR FILES:
struct/*
toolbox/*
Typecheck.java
visitor/DFPrintVisitor.java
visitor/DFStackTestVisitor.java
visitor/DFStackVisitor.java
visitor/DFStackVisitor2.java
visitor/DFTypeCheckVisitor.java
========================================
========================================
1. INITIAL SETUP (build main class, using the parser, understanding visit and accept )
build the initial skeleton, make Typecheck.java class which has the main method.
public static void main(String[] args)
need to construct a parser and read from a java input file, the file will be ascii characters.
there are 4 different parser constructors in MiniJavaParser.java file, currently using the Reader based constructor
public MiniJavaParser(java.io.Reader stream)
for the type checking the parser has a function Goal() which returns a Goal object.
The goal is the starting place to begin the traversal.
Goal objects implement the Node class and has the accept() methods which take in visitors.
public void accept(visitor.Visitor v)
The public data members of a Goal object are
public MainClass f0;
public NodeListOptional f1;
public NodeToken f2;
if calling the Goal accept() method with a visitor as the argument, the Visitor visit() method is then called on a reference to the Goal object.
Goal goal = parser.Goal();
goal.accept(visitor);
accept(visitor.Visitor v) // in goal
{
v.visit(this);
}
visit(Goal n) // in visitor
{
n.f0.accept(this);
n.f1.accept(this);
n.f2.accept(this);
}
First it visits MainClass, since that must exist.
then it will visit any other classes that exist in the NodeListOptional.
In the NodeToken class the internal data member that holds the string representation of the token is "tokenImage"
TO get the String containing the token use the toString() method.
----------------------------------------
2. BUILDING THE SYMBOL TABLE
Building the symbol tables
Type: Map<String,Map<String,Struct>>
DFStackVisitor builds initial table with some of the fields left empty
DFStackVisitor2 fills in the missing information and completes the symbol table
----------------------------------------
3. TYPECHECKING
DFTypeCheckVisitor will complete the type checking using the completed symbol table and the helper functions in the toolbox.
----------------------------------------