diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/AST.java b/miniJava/src/miniJava/AbstractSyntaxTrees/AST.java
index f7f5194..9303c82 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/AST.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/AST.java
@@ -9,19 +9,19 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class AST {
- public AST (SourcePosition posn) {
- this.posn = posn;
- }
-
- public String toString() {
- String fullClassName = this.getClass().getName();
- String cn = fullClassName.substring(1 + fullClassName.lastIndexOf('.'));
- if (ASTDisplay.showPosition)
- cn = cn + " " + posn.toString();
- return cn;
- }
+ public AST(SourcePosition posn) {
+ this.posn = posn;
+ }
- public abstract R visit(Visitor v, A o);
+ public String toString() {
+ String fullClassName = this.getClass().getName();
+ String cn = fullClassName.substring(1 + fullClassName.lastIndexOf('.'));
+ if (ASTDisplay.showPosition)
+ cn = cn + " " + posn.toString();
+ return cn;
+ }
- public SourcePosition posn;
+ public abstract R visit(Visitor v, A o);
+
+ public SourcePosition posn;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/ASTDisplay.java b/miniJava/src/miniJava/AbstractSyntaxTrees/ASTDisplay.java
index be328a3..e0daf6b 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/ASTDisplay.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/ASTDisplay.java
@@ -16,338 +16,343 @@ package miniJava.AbstractSyntaxTrees;
*
* implements Visitor
*/
-public class ASTDisplay implements Visitor {
-
+public class ASTDisplay implements Visitor {
+
public static boolean showPosition = false;
-
- /**
- * print text representation of AST to stdout
- * @param ast root node of AST
- */
- public void showTree(AST ast){
- System.out.println("======= AST Display =========================");
- ast.visit(this, "");
- System.out.println("=============================================");
- }
-
- // methods to format output
-
- /**
- * display arbitrary text for a node
- * @param prefix spacing to indicate depth in AST
- * @param text preformatted node display
- */
- private void show(String prefix, String text) {
- System.out.println(prefix + text);
- }
-
- /**
- * display AST node by name
- * @param prefix spacing to indicate depth in AST
- * @param node AST node, will be shown by name
- */
- private void show(String prefix, AST node) {
- System.out.println(prefix + node.toString());
- }
-
- /**
- * quote a string
- * @param text string to quote
- */
- private String quote(String text) {
- return ("\"" + text + "\"");
- }
-
- /**
- * increase depth in AST
- * @param prefix current spacing to indicate depth in AST
- * @return new spacing
- */
- private String indent(String prefix) {
- return prefix + " ";
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * print text representation of AST to stdout
+ *
+ * @param ast
+ * root node of AST
+ */
+ public void showTree(AST ast) {
+ System.out.println("======= AST Display =========================");
+ ast.visit(this, "");
+ System.out.println("=============================================");
+ }
+
+ // methods to format output
+
+ /**
+ * display arbitrary text for a node
+ *
+ * @param prefix
+ * spacing to indicate depth in AST
+ * @param text
+ * preformatted node display
+ */
+ private void show(String prefix, String text) {
+ System.out.println(prefix + text);
+ }
+
+ /**
+ * display AST node by name
+ *
+ * @param prefix
+ * spacing to indicate depth in AST
+ * @param node
+ * AST node, will be shown by name
+ */
+ private void show(String prefix, AST node) {
+ System.out.println(prefix + node.toString());
+ }
+
+ /**
+ * quote a string
+ *
+ * @param text
+ * string to quote
+ */
+ private String quote(String text) {
+ return ("\"" + text + "\"");
+ }
+
+ /**
+ * increase depth in AST
+ *
+ * @param prefix
+ * current spacing to indicate depth in AST
+ * @return new spacing
+ */
+ private String indent(String prefix) {
+ return prefix + " ";
+ }
+
+ // /////////////////////////////////////////////////////////////////////////////
//
// PACKAGE
//
- ///////////////////////////////////////////////////////////////////////////////
+ // /////////////////////////////////////////////////////////////////////////////
- public Object visitPackage(Package prog, String arg){
- show(arg, prog);
- ClassDeclList cl = prog.classDeclList;
- show(arg," ClassDeclList [" + cl.size() + "]");
- String pfx = arg + " . ";
- for (ClassDecl c: prog.classDeclList){
- c.visit(this, pfx);
- }
- return null;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////
+ public Object visitPackage(Package prog, String arg) {
+ show(arg, prog);
+ ClassDeclList cl = prog.classDeclList;
+ show(arg, " ClassDeclList [" + cl.size() + "]");
+ String pfx = arg + " . ";
+ for (ClassDecl c : prog.classDeclList) {
+ c.visit(this, pfx);
+ }
+ return null;
+ }
+
+ // /////////////////////////////////////////////////////////////////////////////
//
// DECLARATIONS
//
- ///////////////////////////////////////////////////////////////////////////////
-
- public Object visitClassDecl(ClassDecl clas, String arg){
- show(arg, clas);
- show(indent(arg), quote(clas.name) + " classname");
- show(arg," FieldDeclList [" + clas.fieldDeclList.size() + "]");
- String pfx = arg + " . ";
- for (FieldDecl f: clas.fieldDeclList)
- f.visit(this, pfx);
- show(arg," MethodDeclList [" + clas.methodDeclList.size() + "]");
- for (MethodDecl m: clas.methodDeclList)
- m.visit(this, pfx);
- return null;
- }
-
- public Object visitFieldDecl(FieldDecl f, String arg){
- show(arg, "(" + (f.isPrivate ? "private": "public")
- + (f.isStatic ? " static) " :") ") + f.toString());
- f.type.visit(this, indent(arg));
- show(indent(arg), quote(f.name) + " fieldname");
- return null;
- }
-
- public Object visitMethodDecl(MethodDecl m, String arg){
- show(arg, "(" + (m.isPrivate ? "private": "public")
- + (m.isStatic ? " static) " :") ") + m.toString());
- m.type.visit(this, indent(arg));
- show(indent(arg), quote(m.name) + " methodname");
- ParameterDeclList pdl = m.parameterDeclList;
- show(arg, " ParameterDeclList [" + pdl.size() + "]");
- String pfx = ((String) arg) + " . ";
- for (ParameterDecl pd: pdl) {
- pd.visit(this, pfx);
- }
- StatementList sl = m.statementList;
- show(arg, " StmtList [" + sl.size() + "]");
- for (Statement s: sl) {
- s.visit(this, pfx);
- }
- if (m.returnExp != null) {
- m.returnExp.visit(this, indent(arg));
- }
- return null;
- }
-
- public Object visitParameterDecl(ParameterDecl pd, String arg){
- show(arg, pd);
- pd.type.visit(this, indent(arg));
- show(indent(arg), quote(pd.name) + "parametername ");
- return null;
- }
-
- public Object visitVarDecl(VarDecl vd, String arg){
- show(arg, vd);
- vd.type.visit(this, indent(arg));
- show(indent(arg), quote(vd.name) + " varname");
- return null;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////
+ // /////////////////////////////////////////////////////////////////////////////
+
+ public Object visitClassDecl(ClassDecl clas, String arg) {
+ show(arg, clas);
+ show(indent(arg), quote(clas.name) + " classname");
+ show(arg, " FieldDeclList [" + clas.fieldDeclList.size() + "]");
+ String pfx = arg + " . ";
+ for (FieldDecl f : clas.fieldDeclList)
+ f.visit(this, pfx);
+ show(arg, " MethodDeclList [" + clas.methodDeclList.size() + "]");
+ for (MethodDecl m : clas.methodDeclList)
+ m.visit(this, pfx);
+ return null;
+ }
+
+ public Object visitFieldDecl(FieldDecl f, String arg) {
+ show(arg, "(" + (f.isPrivate ? "private" : "public")
+ + (f.isStatic ? " static) " : ") ") + f.toString());
+ f.type.visit(this, indent(arg));
+ show(indent(arg), quote(f.name) + " fieldname");
+ return null;
+ }
+
+ public Object visitMethodDecl(MethodDecl m, String arg) {
+ show(arg, "(" + (m.isPrivate ? "private" : "public")
+ + (m.isStatic ? " static) " : ") ") + m.toString());
+ m.type.visit(this, indent(arg));
+ show(indent(arg), quote(m.name) + " methodname");
+ ParameterDeclList pdl = m.parameterDeclList;
+ show(arg, " ParameterDeclList [" + pdl.size() + "]");
+ String pfx = ((String) arg) + " . ";
+ for (ParameterDecl pd : pdl) {
+ pd.visit(this, pfx);
+ }
+ StatementList sl = m.statementList;
+ show(arg, " StmtList [" + sl.size() + "]");
+ for (Statement s : sl) {
+ s.visit(this, pfx);
+ }
+ if (m.returnExp != null) {
+ m.returnExp.visit(this, indent(arg));
+ }
+ return null;
+ }
+
+ public Object visitParameterDecl(ParameterDecl pd, String arg) {
+ show(arg, pd);
+ pd.type.visit(this, indent(arg));
+ show(indent(arg), quote(pd.name) + "parametername ");
+ return null;
+ }
+
+ public Object visitVarDecl(VarDecl vd, String arg) {
+ show(arg, vd);
+ vd.type.visit(this, indent(arg));
+ show(indent(arg), quote(vd.name) + " varname");
+ return null;
+ }
+
+ // /////////////////////////////////////////////////////////////////////////////
//
// TYPES
//
- ///////////////////////////////////////////////////////////////////////////////
-
- public Object visitBaseType(BaseType type, String arg){
- show(arg, type.typeKind + " " + type.toString());
- return null;
- }
-
- public Object visitClassType(ClassType type, String arg){
- show(arg, type);
- show(indent(arg), quote(type.className.spelling) + " classname");
- return null;
- }
-
- public Object visitArrayType(ArrayType type, String arg){
- show(arg, type);
- type.eltType.visit(this, indent(arg));
- return null;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////
+ // /////////////////////////////////////////////////////////////////////////////
+
+ public Object visitBaseType(BaseType type, String arg) {
+ show(arg, type.typeKind + " " + type.toString());
+ return null;
+ }
+
+ public Object visitClassType(ClassType type, String arg) {
+ show(arg, type);
+ show(indent(arg), quote(type.className.spelling) + " classname");
+ return null;
+ }
+
+ public Object visitArrayType(ArrayType type, String arg) {
+ show(arg, type);
+ type.eltType.visit(this, indent(arg));
+ return null;
+ }
+
+ // /////////////////////////////////////////////////////////////////////////////
//
// STATEMENTS
//
- ///////////////////////////////////////////////////////////////////////////////
+ // /////////////////////////////////////////////////////////////////////////////
- public Object visitBlockStmt(BlockStmt stmt, String arg){
- show(arg, stmt);
- StatementList sl = stmt.sl;
- show(arg," StatementList [" + sl.size() + "]");
- String pfx = arg + " . ";
- for (Statement s: sl) {
- s.visit(this, pfx);
- }
- return null;
- }
-
- public Object visitVardeclStmt(VarDeclStmt stmt, String arg){
- show(arg, stmt);
- stmt.varDecl.visit(this, indent(arg));
- stmt.initExp.visit(this, indent(arg));
- return null;
- }
-
- public Object visitAssignStmt(AssignStmt stmt, String arg){
- show(arg,stmt);
- stmt.ref.visit(this, indent(arg));
- stmt.val.visit(this, indent(arg));
- return null;
- }
-
- public Object visitCallStmt(CallStmt stmt, String arg){
- show(arg,stmt);
- stmt.methodRef.visit(this, indent(arg));
- ExprList al = stmt.argList;
- show(arg," ExprList [" + al.size() + "]");
- String pfx = arg + " . ";
- for (Expression e: al) {
- e.visit(this, pfx);
- }
- return null;
- }
-
- public Object visitIfStmt(IfStmt stmt, String arg){
- show(arg,stmt);
- stmt.cond.visit(this, indent(arg));
- stmt.thenStmt.visit(this, indent(arg));
- if (stmt.elseStmt != null)
- stmt.elseStmt.visit(this, indent(arg));
- return null;
- }
-
- public Object visitWhileStmt(WhileStmt stmt, String arg){
- show(arg, stmt);
- stmt.cond.visit(this, indent(arg));
- stmt.body.visit(this, indent(arg));
- return null;
- }
-
+ public Object visitBlockStmt(BlockStmt stmt, String arg) {
+ show(arg, stmt);
+ StatementList sl = stmt.sl;
+ show(arg, " StatementList [" + sl.size() + "]");
+ String pfx = arg + " . ";
+ for (Statement s : sl) {
+ s.visit(this, pfx);
+ }
+ return null;
+ }
- ///////////////////////////////////////////////////////////////////////////////
+ public Object visitVardeclStmt(VarDeclStmt stmt, String arg) {
+ show(arg, stmt);
+ stmt.varDecl.visit(this, indent(arg));
+ stmt.initExp.visit(this, indent(arg));
+ return null;
+ }
+
+ public Object visitAssignStmt(AssignStmt stmt, String arg) {
+ show(arg, stmt);
+ stmt.ref.visit(this, indent(arg));
+ stmt.val.visit(this, indent(arg));
+ return null;
+ }
+
+ public Object visitCallStmt(CallStmt stmt, String arg) {
+ show(arg, stmt);
+ stmt.methodRef.visit(this, indent(arg));
+ ExprList al = stmt.argList;
+ show(arg, " ExprList [" + al.size() + "]");
+ String pfx = arg + " . ";
+ for (Expression e : al) {
+ e.visit(this, pfx);
+ }
+ return null;
+ }
+
+ public Object visitIfStmt(IfStmt stmt, String arg) {
+ show(arg, stmt);
+ stmt.cond.visit(this, indent(arg));
+ stmt.thenStmt.visit(this, indent(arg));
+ if (stmt.elseStmt != null)
+ stmt.elseStmt.visit(this, indent(arg));
+ return null;
+ }
+
+ public Object visitWhileStmt(WhileStmt stmt, String arg) {
+ show(arg, stmt);
+ stmt.cond.visit(this, indent(arg));
+ stmt.body.visit(this, indent(arg));
+ return null;
+ }
+
+ // /////////////////////////////////////////////////////////////////////////////
//
// EXPRESSIONS
//
- ///////////////////////////////////////////////////////////////////////////////
+ // /////////////////////////////////////////////////////////////////////////////
- public Object visitUnaryExpr(UnaryExpr expr, String arg){
- show(arg, expr);
- expr.operator.visit(this, indent(arg));
- expr.expr.visit(this, indent(indent(arg)));
- return null;
- }
-
- public Object visitBinaryExpr(BinaryExpr expr, String arg){
- show(arg, expr);
- expr.operator.visit(this, indent(arg));
- expr.left.visit(this, indent(indent(arg)));
- expr.right.visit(this, indent(indent(arg)));
- return null;
- }
-
- public Object visitRefExpr(RefExpr expr, String arg){
- show(arg, expr);
- expr.ref.visit(this, indent(arg));
- return null;
- }
-
- public Object visitCallExpr(CallExpr expr, String arg){
- show(arg, expr);
- expr.functionRef.visit(this, indent(arg));
- ExprList al = expr.argList;
- show(arg," ExprList + [" + al.size() + "]");
- String pfx = arg + " . ";
- for (Expression e: al) {
- e.visit(this, pfx);
- }
- return null;
- }
-
- public Object visitLiteralExpr(LiteralExpr expr, String arg){
- show(arg, expr);
- expr.literal.visit(this, indent(arg));
- return null;
- }
-
- public Object visitNewArrayExpr(NewArrayExpr expr, String arg){
- show(arg, expr);
- expr.eltType.visit(this, indent(arg));
- expr.sizeExpr.visit(this, indent(arg));
- return null;
- }
-
- public Object visitNewObjectExpr(NewObjectExpr expr, String arg){
- show(arg, expr);
- expr.classtype.visit(this, indent(arg));
- return null;
- }
-
+ public Object visitUnaryExpr(UnaryExpr expr, String arg) {
+ show(arg, expr);
+ expr.operator.visit(this, indent(arg));
+ expr.expr.visit(this, indent(indent(arg)));
+ return null;
+ }
- ///////////////////////////////////////////////////////////////////////////////
+ public Object visitBinaryExpr(BinaryExpr expr, String arg) {
+ show(arg, expr);
+ expr.operator.visit(this, indent(arg));
+ expr.left.visit(this, indent(indent(arg)));
+ expr.right.visit(this, indent(indent(arg)));
+ return null;
+ }
+
+ public Object visitRefExpr(RefExpr expr, String arg) {
+ show(arg, expr);
+ expr.ref.visit(this, indent(arg));
+ return null;
+ }
+
+ public Object visitCallExpr(CallExpr expr, String arg) {
+ show(arg, expr);
+ expr.functionRef.visit(this, indent(arg));
+ ExprList al = expr.argList;
+ show(arg, " ExprList + [" + al.size() + "]");
+ String pfx = arg + " . ";
+ for (Expression e : al) {
+ e.visit(this, pfx);
+ }
+ return null;
+ }
+
+ public Object visitLiteralExpr(LiteralExpr expr, String arg) {
+ show(arg, expr);
+ expr.literal.visit(this, indent(arg));
+ return null;
+ }
+
+ public Object visitNewArrayExpr(NewArrayExpr expr, String arg) {
+ show(arg, expr);
+ expr.eltType.visit(this, indent(arg));
+ expr.sizeExpr.visit(this, indent(arg));
+ return null;
+ }
+
+ public Object visitNewObjectExpr(NewObjectExpr expr, String arg) {
+ show(arg, expr);
+ expr.classtype.visit(this, indent(arg));
+ return null;
+ }
+
+ // /////////////////////////////////////////////////////////////////////////////
//
// REFERENCES
//
- ///////////////////////////////////////////////////////////////////////////////
-
- public Object visitQualifiedRef(QualifiedRef qr, String arg) {
- show(arg, qr);
- qr.id.visit(this, indent(arg));
- qr.ref.visit(this, indent(arg));
- return null;
- }
-
- public Object visitIndexedRef(IndexedRef ir, String arg) {
- show(arg, ir);
- ir.indexExpr.visit(this, indent(arg));
- ir.ref.visit(this, indent(arg));
- return null;
- }
-
- public Object visitIdRef(IdRef ref, String arg) {
- show(arg,ref);
- ref.id.visit(this, indent(arg));
- return null;
- }
-
- public Object visitThisRef(ThisRef ref, String arg) {
- show(arg,ref);
- return null;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////
+ // /////////////////////////////////////////////////////////////////////////////
+
+ public Object visitQualifiedRef(QualifiedRef qr, String arg) {
+ show(arg, qr);
+ qr.id.visit(this, indent(arg));
+ qr.ref.visit(this, indent(arg));
+ return null;
+ }
+
+ public Object visitIndexedRef(IndexedRef ir, String arg) {
+ show(arg, ir);
+ ir.indexExpr.visit(this, indent(arg));
+ ir.ref.visit(this, indent(arg));
+ return null;
+ }
+
+ public Object visitIdRef(IdRef ref, String arg) {
+ show(arg, ref);
+ ref.id.visit(this, indent(arg));
+ return null;
+ }
+
+ public Object visitThisRef(ThisRef ref, String arg) {
+ show(arg, ref);
+ return null;
+ }
+
+ // /////////////////////////////////////////////////////////////////////////////
//
// TERMINALS
//
- ///////////////////////////////////////////////////////////////////////////////
-
- public Object visitIdentifier(Identifier id, String arg){
- show(arg, quote(id.spelling) + " " + id.toString());
- return null;
- }
-
- public Object visitOperator(Operator op, String arg){
- show(arg, quote(op.spelling) + " " + op.toString());
- return null;
- }
-
- public Object visitIntLiteral(IntLiteral num, String arg){
- show(arg, quote(num.spelling) + " " + num.toString());
- return null;
- }
-
- public Object visitBooleanLiteral(BooleanLiteral bool, String arg){
- show(arg, quote(bool.spelling) + " " + bool.toString());
- return null;
- }
+ // /////////////////////////////////////////////////////////////////////////////
+
+ public Object visitIdentifier(Identifier id, String arg) {
+ show(arg, quote(id.spelling) + " " + id.toString());
+ return null;
+ }
+
+ public Object visitOperator(Operator op, String arg) {
+ show(arg, quote(op.spelling) + " " + op.toString());
+ return null;
+ }
+
+ public Object visitIntLiteral(IntLiteral num, String arg) {
+ show(arg, quote(num.spelling) + " " + num.toString());
+ return null;
+ }
+
+ public Object visitBooleanLiteral(BooleanLiteral bool, String arg) {
+ show(arg, quote(bool.spelling) + " " + bool.toString());
+ return null;
+ }
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/ArrayType.java b/miniJava/src/miniJava/AbstractSyntaxTrees/ArrayType.java
index d8a5358..bee4008 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/ArrayType.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/ArrayType.java
@@ -10,15 +10,14 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
public class ArrayType extends Type {
- public ArrayType(Type eltType, SourcePosition posn){
- super(TypeKind.ARRAY, posn);
- this.eltType = eltType;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitArrayType(this, o);
- }
-
- public Type eltType;
+ public ArrayType(Type eltType, SourcePosition posn) {
+ super(TypeKind.ARRAY, posn);
+ this.eltType = eltType;
}
+ public R visit(Visitor v, A o) {
+ return v.visitArrayType(this, o);
+ }
+
+ public Type eltType;
+}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/AssignStmt.java b/miniJava/src/miniJava/AbstractSyntaxTrees/AssignStmt.java
index 1a6db99..cc78e3f 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/AssignStmt.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/AssignStmt.java
@@ -7,18 +7,17 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class AssignStmt extends Statement
-{
- public AssignStmt(Reference r, Expression e, SourcePosition posn){
- super(posn);
- ref = r;
- val = e;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitAssignStmt(this, o);
- }
-
- public Reference ref;
- public Expression val;
+public class AssignStmt extends Statement {
+ public AssignStmt(Reference r, Expression e, SourcePosition posn) {
+ super(posn);
+ ref = r;
+ val = e;
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitAssignStmt(this, o);
+ }
+
+ public Reference ref;
+ public Expression val;
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/BaseType.java b/miniJava/src/miniJava/AbstractSyntaxTrees/BaseType.java
index 38ad09f..d39fe5b 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/BaseType.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/BaseType.java
@@ -7,13 +7,12 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class BaseType extends Type
-{
- public BaseType(TypeKind t, SourcePosition posn){
- super(t, posn);
- }
-
- public R visit(Visitor v, A o) {
- return v.visitBaseType(this, o);
- }
+public class BaseType extends Type {
+ public BaseType(TypeKind t, SourcePosition posn) {
+ super(t, posn);
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitBaseType(this, o);
+ }
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/BinaryExpr.java b/miniJava/src/miniJava/AbstractSyntaxTrees/BinaryExpr.java
index 91fcc28..4488c19 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/BinaryExpr.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/BinaryExpr.java
@@ -7,20 +7,20 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class BinaryExpr extends Expression
-{
- public BinaryExpr(Operator o, Expression e1, Expression e2, SourcePosition posn){
- super(posn);
- operator = o;
- left = e1;
- right = e2;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitBinaryExpr(this, o);
- }
-
- public Operator operator;
- public Expression left;
- public Expression right;
+public class BinaryExpr extends Expression {
+ public BinaryExpr(Operator o, Expression e1, Expression e2,
+ SourcePosition posn) {
+ super(posn);
+ operator = o;
+ left = e1;
+ right = e2;
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitBinaryExpr(this, o);
+ }
+
+ public Operator operator;
+ public Expression left;
+ public Expression right;
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/BlockStmt.java b/miniJava/src/miniJava/AbstractSyntaxTrees/BlockStmt.java
index 933909c..1a4e2d9 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/BlockStmt.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/BlockStmt.java
@@ -7,16 +7,15 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class BlockStmt extends Statement
-{
- public BlockStmt(StatementList sl, SourcePosition posn){
- super(posn);
- this.sl = sl;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitBlockStmt(this, o);
- }
-
- public StatementList sl;
+public class BlockStmt extends Statement {
+ public BlockStmt(StatementList sl, SourcePosition posn) {
+ super(posn);
+ this.sl = sl;
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitBlockStmt(this, o);
+ }
+
+ public StatementList sl;
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/BooleanLiteral.java b/miniJava/src/miniJava/AbstractSyntaxTrees/BooleanLiteral.java
index 47b5f3f..d0b4302 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/BooleanLiteral.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/BooleanLiteral.java
@@ -9,11 +9,11 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
public class BooleanLiteral extends Literal {
- public BooleanLiteral(String spelling, SourcePosition posn) {
- super (spelling,posn);
- }
-
- public R visit(Visitor v, A o) {
- return v.visitBooleanLiteral(this, o);
- }
+ public BooleanLiteral(String spelling, SourcePosition posn) {
+ super(spelling, posn);
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitBooleanLiteral(this, o);
+ }
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/CallExpr.java b/miniJava/src/miniJava/AbstractSyntaxTrees/CallExpr.java
index 1e17bbb..5aaa41f 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/CallExpr.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/CallExpr.java
@@ -7,18 +7,17 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class CallExpr extends Expression
-{
- public CallExpr(Reference f, ExprList el, SourcePosition posn){
- super(posn);
- functionRef = f;
- argList = el;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitCallExpr(this, o);
- }
-
- public Reference functionRef;
- public ExprList argList;
+public class CallExpr extends Expression {
+ public CallExpr(Reference f, ExprList el, SourcePosition posn) {
+ super(posn);
+ functionRef = f;
+ argList = el;
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitCallExpr(this, o);
+ }
+
+ public Reference functionRef;
+ public ExprList argList;
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/CallStmt.java b/miniJava/src/miniJava/AbstractSyntaxTrees/CallStmt.java
index a8aa63e..966f984 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/CallStmt.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/CallStmt.java
@@ -7,18 +7,17 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class CallStmt extends Statement
-{
- public CallStmt(Reference m, ExprList el, SourcePosition posn){
- super(posn);
- methodRef = m;
- argList = el;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitCallStmt(this, o);
- }
-
- public Reference methodRef;
- public ExprList argList;
+public class CallStmt extends Statement {
+ public CallStmt(Reference m, ExprList el, SourcePosition posn) {
+ super(posn);
+ methodRef = m;
+ argList = el;
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitCallStmt(this, o);
+ }
+
+ public Reference methodRef;
+ public ExprList argList;
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/ClassDecl.java b/miniJava/src/miniJava/AbstractSyntaxTrees/ClassDecl.java
index bd776a8..39a96be 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/ClassDecl.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/ClassDecl.java
@@ -5,20 +5,23 @@
*/
package miniJava.AbstractSyntaxTrees;
-import miniJava.SyntacticAnalyzer.SourcePosition;
+import miniJava.SyntacticAnalyzer.SourcePosition;
public class ClassDecl extends Declaration {
- public ClassDecl(String cn, FieldDeclList fdl, MethodDeclList mdl, SourcePosition posn) {
- super(cn, null, posn);
- fieldDeclList = fdl;
- methodDeclList = mdl;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitClassDecl(this, o);
- }
-
- public FieldDeclList fieldDeclList;
- public MethodDeclList methodDeclList;
+ public ClassDecl(String cn, FieldDeclList fdl, MethodDeclList mdl, SourcePosition posn) {
+ super(cn, null, posn);
+ fieldDeclList = fdl;
+ methodDeclList = mdl;
+
+ Identifier ident = new Identifier(cn, posn);
+ type = new ClassType(ident, posn);
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitClassDecl(this, o);
+ }
+
+ public FieldDeclList fieldDeclList;
+ public MethodDeclList methodDeclList;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/ClassDeclList.java b/miniJava/src/miniJava/AbstractSyntaxTrees/ClassDeclList.java
index 489de35..64dd757 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/ClassDeclList.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/ClassDeclList.java
@@ -7,17 +7,16 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*;
-public class ClassDeclList implements Iterable
-{
+public class ClassDeclList implements Iterable {
public ClassDeclList() {
classDeclList = new ArrayList();
- }
+ }
- public void add(ClassDecl cd){
+ public void add(ClassDecl cd) {
classDeclList.add(cd);
}
- public ClassDecl get(int i){
+ public ClassDecl get(int i) {
return classDeclList.get(i);
}
@@ -31,4 +30,3 @@ public class ClassDeclList implements Iterable
private List classDeclList;
}
-
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/ClassType.java b/miniJava/src/miniJava/AbstractSyntaxTrees/ClassType.java
index 980d07f..0c2c312 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/ClassType.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/ClassType.java
@@ -7,16 +7,15 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class ClassType extends Type
-{
- public ClassType(Identifier cn, SourcePosition posn){
- super(TypeKind.CLASS, posn);
- className = cn;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitClassType(this, o);
- }
+public class ClassType extends Type {
+ public ClassType(Identifier cn, SourcePosition posn) {
+ super(TypeKind.CLASS, posn);
+ className = cn;
+ }
- public Identifier className;
+ public R visit(Visitor v, A o) {
+ return v.visitClassType(this, o);
+ }
+
+ public Identifier className;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Declaration.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Declaration.java
index cf5c22d..8859aa4 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Declaration.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Declaration.java
@@ -8,22 +8,23 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Declaration extends AST {
-
+
public Declaration(String name, Type type, SourcePosition posn) {
super(posn);
this.name = name;
this.type = type;
}
-
+
@Override
public String toString() {
- if(posn != null) {
- return this.name + "(Line: " + posn.line + ", Column: " + posn.col + ")";
+ if (posn != null) {
+ return this.name + "(Line: " + posn.line + ", Column: " + posn.col
+ + ")";
} else {
return super.toString();
}
}
-
+
public String name;
public Type type;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Declarators.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Declarators.java
index 4e0cce2..3f10afb 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Declarators.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Declarators.java
@@ -3,16 +3,17 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class Declarators {
-
- public Declarators(boolean isPrivate, boolean isStatic, Type mt, SourcePosition posn) {
+
+ public Declarators(boolean isPrivate, boolean isStatic, Type mt,
+ SourcePosition posn) {
this.isPrivate = isPrivate;
this.isStatic = isStatic;
this.mt = mt;
this.posn = posn;
}
-
+
public boolean isPrivate;
- public boolean isStatic;
- public Type mt;
- public SourcePosition posn;
+ public boolean isStatic;
+ public Type mt;
+ public SourcePosition posn;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/ExprList.java b/miniJava/src/miniJava/AbstractSyntaxTrees/ExprList.java
index 1a55c0d..3aa9cf8 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/ExprList.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/ExprList.java
@@ -7,27 +7,26 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*;
-public class ExprList implements Iterable
-{
- public ExprList() {
- elist = new ArrayList();
- }
-
- public void add(Expression e){
- elist.add(e);
- }
-
- public Expression get(int i){
- return elist.get(i);
- }
-
- public int size() {
- return elist.size();
- }
-
- public Iterator iterator() {
- return elist.iterator();
- }
-
- private List elist;
+public class ExprList implements Iterable {
+ public ExprList() {
+ elist = new ArrayList();
+ }
+
+ public void add(Expression e) {
+ elist.add(e);
+ }
+
+ public Expression get(int i) {
+ return elist.get(i);
+ }
+
+ public int size() {
+ return elist.size();
+ }
+
+ public Iterator iterator() {
+ return elist.iterator();
+ }
+
+ private List elist;
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Expression.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Expression.java
index 2c33a62..7086815 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Expression.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Expression.java
@@ -5,12 +5,12 @@
*/
package miniJava.AbstractSyntaxTrees;
-import miniJava.SyntacticAnalyzer.SourcePosition;
+import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Expression extends AST {
- public Expression(SourcePosition posn) {
- super (posn);
- }
-
+ public Expression(SourcePosition posn) {
+ super(posn);
+ }
+
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/FieldDecl.java b/miniJava/src/miniJava/AbstractSyntaxTrees/FieldDecl.java
index 5802447..12a11f7 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/FieldDecl.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/FieldDecl.java
@@ -8,17 +8,17 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class FieldDecl extends MemberDecl {
-
- public FieldDecl(boolean isPrivate, boolean isStatic, Type t, String name, SourcePosition posn){
- super(isPrivate, isStatic, t, name, posn);
- }
-
- public FieldDecl(MemberDecl md, SourcePosition posn) {
- super(md,posn);
- }
-
- public R visit(Visitor v, A o) {
- return v.visitFieldDecl(this, o);
- }
-}
+ public FieldDecl(boolean isPrivate, boolean isStatic, Type t, String name,
+ SourcePosition posn) {
+ super(isPrivate, isStatic, t, name, posn);
+ }
+
+ public FieldDecl(MemberDecl md, SourcePosition posn) {
+ super(md, posn);
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitFieldDecl(this, o);
+ }
+}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/FieldDeclList.java b/miniJava/src/miniJava/AbstractSyntaxTrees/FieldDeclList.java
index 18b0843..a2a476b 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/FieldDeclList.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/FieldDeclList.java
@@ -7,22 +7,21 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*;
-public class FieldDeclList implements Iterable
-{
+public class FieldDeclList implements Iterable {
public FieldDeclList() {
fieldDeclList = new ArrayList();
- }
-
+ }
+
public FieldDeclList(FieldDecl f) {
fieldDeclList = new ArrayList();
fieldDeclList.add(f);
- }
+ }
- public void add(FieldDecl cd){
+ public void add(FieldDecl cd) {
fieldDeclList.add(cd);
}
- public FieldDecl get(int i){
+ public FieldDecl get(int i) {
return fieldDeclList.get(i);
}
@@ -36,4 +35,3 @@ public class FieldDeclList implements Iterable
private List fieldDeclList;
}
-
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/IdRef.java b/miniJava/src/miniJava/AbstractSyntaxTrees/IdRef.java
index bf62d7d..cc3e555 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/IdRef.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/IdRef.java
@@ -8,13 +8,13 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class IdRef extends Reference {
-
- public IdRef(Identifier id, SourcePosition posn){
+
+ public IdRef(Identifier id, SourcePosition posn) {
super(posn);
this.id = id;
}
-
- public R visit(Visitor v, A o) {
+
+ public R visit(Visitor v, A o) {
return v.visitIdRef(this, o);
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Identifier.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Identifier.java
index 54e98ac..1d0c0a5 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Identifier.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Identifier.java
@@ -8,14 +8,14 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class Identifier extends Terminal {
-
- public Identifier (String s, SourcePosition posn) {
- super (s,posn);
- }
- public R visit(Visitor v, A o) {
- return v.visitIdentifier(this, o);
- }
+ public Identifier(String s, SourcePosition posn) {
+ super(s, posn);
+ }
- public Declaration decl = null;
+ public R visit(Visitor v, A o) {
+ return v.visitIdentifier(this, o);
+ }
+
+ public Declaration decl = null;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/IfStmt.java b/miniJava/src/miniJava/AbstractSyntaxTrees/IfStmt.java
index b22c31f..7ceb5aa 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/IfStmt.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/IfStmt.java
@@ -7,27 +7,26 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class IfStmt extends Statement
-{
- public IfStmt(Expression b, Statement t, Statement e, SourcePosition posn){
- super(posn);
- cond = b;
- thenStmt = t;
- elseStmt = e;
- }
-
- public IfStmt(Expression b, Statement t, SourcePosition posn){
- super(posn);
- cond = b;
- thenStmt = t;
- elseStmt = null;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitIfStmt(this, o);
- }
-
- public Expression cond;
- public Statement thenStmt;
- public Statement elseStmt;
+public class IfStmt extends Statement {
+ public IfStmt(Expression b, Statement t, Statement e, SourcePosition posn) {
+ super(posn);
+ cond = b;
+ thenStmt = t;
+ elseStmt = e;
+ }
+
+ public IfStmt(Expression b, Statement t, SourcePosition posn) {
+ super(posn);
+ cond = b;
+ thenStmt = t;
+ elseStmt = null;
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitIfStmt(this, o);
+ }
+
+ public Expression cond;
+ public Statement thenStmt;
+ public Statement elseStmt;
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/IndexedRef.java b/miniJava/src/miniJava/AbstractSyntaxTrees/IndexedRef.java
index c226b3f..a52a014 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/IndexedRef.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/IndexedRef.java
@@ -8,17 +8,17 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class IndexedRef extends Reference {
-
- public IndexedRef(Reference ref, Expression expr, SourcePosition posn){
+
+ public IndexedRef(Reference ref, Expression expr, SourcePosition posn) {
super(posn);
this.ref = ref;
this.indexExpr = expr;
}
- public R visit(Visitor v, A o){
+ public R visit(Visitor v, A o) {
return v.visitIndexedRef(this, o);
}
-
+
public Reference ref;
public Expression indexExpr;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/IntLiteral.java b/miniJava/src/miniJava/AbstractSyntaxTrees/IntLiteral.java
index 7926b9a..2e0e0d1 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/IntLiteral.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/IntLiteral.java
@@ -9,11 +9,11 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
public class IntLiteral extends Literal {
- public IntLiteral(String s, SourcePosition posn) {
- super(s, posn);
- }
-
- public R visit(Visitor v, A o) {
- return v.visitIntLiteral(this, o);
- }
+ public IntLiteral(String s, SourcePosition posn) {
+ super(s, posn);
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitIntLiteral(this, o);
+ }
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/LiteralExpr.java b/miniJava/src/miniJava/AbstractSyntaxTrees/LiteralExpr.java
index 49ab09f..83c26d1 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/LiteralExpr.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/LiteralExpr.java
@@ -7,16 +7,15 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class LiteralExpr extends Expression
-{
- public LiteralExpr(Literal c, SourcePosition posn){
- super(posn);
- literal = c;
- }
-
- public R visit(Visitor v, A o){
- return v.visitLiteralExpr(this, o);
- }
+public class LiteralExpr extends Expression {
+ public LiteralExpr(Literal c, SourcePosition posn) {
+ super(posn);
+ literal = c;
+ }
- public Literal literal;
+ public R visit(Visitor v, A o) {
+ return v.visitLiteralExpr(this, o);
+ }
+
+ public Literal literal;
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/LocalDecl.java b/miniJava/src/miniJava/AbstractSyntaxTrees/LocalDecl.java
index ce86df1..d03b6c2 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/LocalDecl.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/LocalDecl.java
@@ -8,9 +8,9 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class LocalDecl extends Declaration {
-
- public LocalDecl(String name, Type t, SourcePosition posn){
- super(name,t,posn);
+
+ public LocalDecl(String name, Type t, SourcePosition posn) {
+ super(name, t, posn);
}
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/MemberDecl.java b/miniJava/src/miniJava/AbstractSyntaxTrees/MemberDecl.java
index 6db1e30..a199a86 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/MemberDecl.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/MemberDecl.java
@@ -9,18 +9,19 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
abstract public class MemberDecl extends Declaration {
- public MemberDecl(boolean isPrivate, boolean isStatic, Type mt, String name, SourcePosition posn) {
- super(name, mt, posn);
- this.isPrivate = isPrivate;
- this.isStatic = isStatic;
- }
-
- public MemberDecl(MemberDecl md, SourcePosition posn){
- super(md.name, md.type, posn);
- this.isPrivate = md.isPrivate;
- this.isStatic = md.isStatic;
- }
-
- public boolean isPrivate;
- public boolean isStatic;
+ public MemberDecl(boolean isPrivate, boolean isStatic, Type mt,
+ String name, SourcePosition posn) {
+ super(name, mt, posn);
+ this.isPrivate = isPrivate;
+ this.isStatic = isStatic;
+ }
+
+ public MemberDecl(MemberDecl md, SourcePosition posn) {
+ super(md.name, md.type, posn);
+ this.isPrivate = md.isPrivate;
+ this.isStatic = md.isStatic;
+ }
+
+ public boolean isPrivate;
+ public boolean isStatic;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/MethodDecl.java b/miniJava/src/miniJava/AbstractSyntaxTrees/MethodDecl.java
index 276dfd1..54ab4e5 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/MethodDecl.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/MethodDecl.java
@@ -8,18 +8,19 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class MethodDecl extends MemberDecl {
-
- public MethodDecl(MemberDecl md, ParameterDeclList pl, StatementList sl, Expression e, SourcePosition posn){
- super(md,posn);
- parameterDeclList = pl;
- statementList = sl;
- returnExp = e;
+
+ public MethodDecl(MemberDecl md, ParameterDeclList pl, StatementList sl,
+ Expression e, SourcePosition posn) {
+ super(md, posn);
+ parameterDeclList = pl;
+ statementList = sl;
+ returnExp = e;
}
-
+
public R visit(Visitor v, A o) {
- return v.visitMethodDecl(this, o);
- }
-
+ return v.visitMethodDecl(this, o);
+ }
+
public ParameterDeclList parameterDeclList;
public StatementList statementList;
public Expression returnExp;
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/MethodDeclList.java b/miniJava/src/miniJava/AbstractSyntaxTrees/MethodDeclList.java
index 609da53..41bb12f 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/MethodDeclList.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/MethodDeclList.java
@@ -7,22 +7,21 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*;
-public class MethodDeclList implements Iterable
-{
+public class MethodDeclList implements Iterable {
public MethodDeclList() {
methodDeclList = new ArrayList();
}
-
+
public MethodDeclList(MethodDecl m) {
methodDeclList = new ArrayList();
methodDeclList.add(m);
}
- public void add(MethodDecl cd){
+ public void add(MethodDecl cd) {
methodDeclList.add(cd);
}
- public MethodDecl get(int i){
+ public MethodDecl get(int i) {
return methodDeclList.get(i);
}
@@ -36,4 +35,3 @@ public class MethodDeclList implements Iterable
private List methodDeclList;
}
-
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/NewArrayExpr.java b/miniJava/src/miniJava/AbstractSyntaxTrees/NewArrayExpr.java
index 4d16a72..398c18e 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/NewArrayExpr.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/NewArrayExpr.java
@@ -7,18 +7,17 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class NewArrayExpr extends NewExpr
-{
- public NewArrayExpr(Type et, Expression e, SourcePosition posn){
- super(posn);
- eltType = et;
- sizeExpr = e;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitNewArrayExpr(this, o);
- }
+public class NewArrayExpr extends NewExpr {
+ public NewArrayExpr(Type et, Expression e, SourcePosition posn) {
+ super(posn);
+ eltType = et;
+ sizeExpr = e;
+ }
- public Type eltType;
- public Expression sizeExpr;
+ public R visit(Visitor v, A o) {
+ return v.visitNewArrayExpr(this, o);
+ }
+
+ public Type eltType;
+ public Expression sizeExpr;
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/NewExpr.java b/miniJava/src/miniJava/AbstractSyntaxTrees/NewExpr.java
index 6e7876a..62c271d 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/NewExpr.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/NewExpr.java
@@ -8,8 +8,8 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class NewExpr extends Expression {
-
+
public NewExpr(SourcePosition posn) {
- super (posn);
- }
+ super(posn);
+ }
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/NewObjectExpr.java b/miniJava/src/miniJava/AbstractSyntaxTrees/NewObjectExpr.java
index 1f8b134..9217ebf 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/NewObjectExpr.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/NewObjectExpr.java
@@ -7,16 +7,15 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class NewObjectExpr extends NewExpr
-{
- public NewObjectExpr(ClassType ct, SourcePosition posn){
- super(posn);
- classtype = ct;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitNewObjectExpr(this, o);
- }
-
- public ClassType classtype;
+public class NewObjectExpr extends NewExpr {
+ public NewObjectExpr(ClassType ct, SourcePosition posn) {
+ super(posn);
+ classtype = ct;
+ }
+
+ public R visit(Visitor v, A o) {
+ return v.visitNewObjectExpr(this, o);
+ }
+
+ public ClassType classtype;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Operator.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Operator.java
index 88e291d..00016ab 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Operator.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Operator.java
@@ -10,13 +10,14 @@ import miniJava.SyntacticAnalyzer.Token;
public class Operator extends Terminal {
- public Operator (Token t, SourcePosition posn) {
- super (t.spelling, posn);
- }
+ public Operator(Token t, SourcePosition posn) {
+ super(t.spelling, posn);
+ token = t;
+ }
- public R visit(Visitor v, A o) {
- return v.visitOperator(this, o);
- }
+ public R visit(Visitor v, A o) {
+ return v.visitOperator(this, o);
+ }
- public Token token;
+ public Token token;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Package.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Package.java
index dc29d7d..974564c 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Package.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Package.java
@@ -9,14 +9,14 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
public class Package extends AST {
- public Package(ClassDeclList cdl, SourcePosition posn) {
- super(posn);
- classDeclList = cdl;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitPackage(this, o);
- }
+ public Package(ClassDeclList cdl, SourcePosition posn) {
+ super(posn);
+ classDeclList = cdl;
+ }
- public ClassDeclList classDeclList;
+ public R visit(Visitor v, A o) {
+ return v.visitPackage(this, o);
+ }
+
+ public ClassDeclList classDeclList;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/ParameterDecl.java b/miniJava/src/miniJava/AbstractSyntaxTrees/ParameterDecl.java
index 1bbee69..56655e4 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/ParameterDecl.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/ParameterDecl.java
@@ -8,13 +8,12 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ParameterDecl extends LocalDecl {
-
- public ParameterDecl(Type t, String name, SourcePosition posn){
+
+ public ParameterDecl(Type t, String name, SourcePosition posn) {
super(name, t, posn);
}
-
- public R visit(Visitor v, A o) {
- return v.visitParameterDecl(this, o);
- }
-}
+ public R visit(Visitor v, A o) {
+ return v.visitParameterDecl(this, o);
+ }
+}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/ParameterDeclList.java b/miniJava/src/miniJava/AbstractSyntaxTrees/ParameterDeclList.java
index b0e5332..9612836 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/ParameterDeclList.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/ParameterDeclList.java
@@ -7,32 +7,31 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*;
-public class ParameterDeclList implements Iterable
-{
- public ParameterDeclList() {
- parameterDeclList = new ArrayList();
- }
-
- public ParameterDeclList(ParameterDecl p) {
- parameterDeclList = new ArrayList();
- parameterDeclList.add(p);
- }
-
- public void add(ParameterDecl s){
- parameterDeclList.add(s);
- }
-
- public ParameterDecl get(int i){
- return parameterDeclList.get(i);
- }
-
- public int size() {
- return parameterDeclList.size();
- }
-
- public Iterator iterator() {
- return parameterDeclList.iterator();
- }
-
- private List parameterDeclList;
+public class ParameterDeclList implements Iterable {
+ public ParameterDeclList() {
+ parameterDeclList = new ArrayList();
+ }
+
+ public ParameterDeclList(ParameterDecl p) {
+ parameterDeclList = new ArrayList();
+ parameterDeclList.add(p);
+ }
+
+ public void add(ParameterDecl s) {
+ parameterDeclList.add(s);
+ }
+
+ public ParameterDecl get(int i) {
+ return parameterDeclList.get(i);
+ }
+
+ public int size() {
+ return parameterDeclList.size();
+ }
+
+ public Iterator iterator() {
+ return parameterDeclList.iterator();
+ }
+
+ private List parameterDeclList;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/QualifiedRef.java b/miniJava/src/miniJava/AbstractSyntaxTrees/QualifiedRef.java
index 3659a45..c147f79 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/QualifiedRef.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/QualifiedRef.java
@@ -8,11 +8,11 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class QualifiedRef extends Reference {
-
- public QualifiedRef(Reference ref, Identifier id, SourcePosition posn){
+
+ public QualifiedRef(Reference ref, Identifier id, SourcePosition posn) {
super(posn);
this.ref = ref;
- this.id = id;
+ this.id = id;
}
@Override
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/RefExpr.java b/miniJava/src/miniJava/AbstractSyntaxTrees/RefExpr.java
index 48917dc..4f289b2 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/RefExpr.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/RefExpr.java
@@ -7,16 +7,15 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class RefExpr extends Expression
-{
- public RefExpr(Reference r, SourcePosition posn){
- super(posn);
- ref = r;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitRefExpr(this, o);
- }
+public class RefExpr extends Expression {
+ public RefExpr(Reference r, SourcePosition posn) {
+ super(posn);
+ ref = r;
+ }
- public Reference ref;
+ public R visit(Visitor v, A o) {
+ return v.visitRefExpr(this, o);
+ }
+
+ public Reference ref;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Reference.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Reference.java
index 5600ed8..7a6e351 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Reference.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Reference.java
@@ -7,11 +7,10 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public abstract class Reference extends AST
-{
- public Reference(SourcePosition posn){
+public abstract class Reference extends AST {
+ public Reference(SourcePosition posn) {
super(posn);
}
-
+
public Declaration decl;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Statement.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Statement.java
index 4fa1af9..e8db4cb 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Statement.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Statement.java
@@ -5,12 +5,12 @@
*/
package miniJava.AbstractSyntaxTrees;
-import miniJava.SyntacticAnalyzer.SourcePosition;
+import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Statement extends AST {
- public Statement(SourcePosition posn) {
- super (posn);
- }
+ public Statement(SourcePosition posn) {
+ super(posn);
+ }
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/StatementList.java b/miniJava/src/miniJava/AbstractSyntaxTrees/StatementList.java
index 2b21f76..b6bd1d9 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/StatementList.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/StatementList.java
@@ -7,27 +7,26 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*;
-public class StatementList implements Iterable
-{
- public StatementList() {
- slist = new ArrayList();
- }
-
- public void add(Statement s){
- slist.add(s);
- }
-
- public Statement get(int i){
- return slist.get(i);
- }
-
- public int size() {
- return slist.size();
- }
-
- public Iterator iterator() {
- return slist.iterator();
- }
-
- private List slist;
+public class StatementList implements Iterable {
+ public StatementList() {
+ slist = new ArrayList();
+ }
+
+ public void add(Statement s) {
+ slist.add(s);
+ }
+
+ public Statement get(int i) {
+ return slist.get(i);
+ }
+
+ public int size() {
+ return slist.size();
+ }
+
+ public Iterator iterator() {
+ return slist.iterator();
+ }
+
+ private List slist;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Terminal.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Terminal.java
index 5b3f7e6..fb28172 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Terminal.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Terminal.java
@@ -9,10 +9,10 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
abstract public class Terminal extends AST {
- public Terminal (String s, SourcePosition posn) {
- super(posn);
- spelling = s;
- }
+ public Terminal(String s, SourcePosition posn) {
+ super(posn);
+ spelling = s;
+ }
- public String spelling;
+ public String spelling;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/ThisRef.java b/miniJava/src/miniJava/AbstractSyntaxTrees/ThisRef.java
index 3beab4e..082340c 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/ThisRef.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/ThisRef.java
@@ -8,7 +8,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ThisRef extends Reference {
-
+
public ThisRef(SourcePosition posn) {
super(posn);
}
@@ -17,5 +17,5 @@ public class ThisRef extends Reference {
public R visit(Visitor v, A o) {
return v.visitThisRef(this, o);
}
-
+
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Type.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Type.java
index cbcaa10..ef45865 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Type.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Type.java
@@ -8,14 +8,12 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
abstract public class Type extends AST {
-
- public Type(TypeKind typ, SourcePosition posn){
- super(posn);
- typeKind = typ;
- }
-
- public TypeKind typeKind;
-
-}
-
\ No newline at end of file
+ public Type(TypeKind typ, SourcePosition posn) {
+ super(posn);
+ typeKind = typ;
+ }
+
+ public TypeKind typeKind;
+
+}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/TypeKind.java b/miniJava/src/miniJava/AbstractSyntaxTrees/TypeKind.java
index 16d16b7..824ddbb 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/TypeKind.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/TypeKind.java
@@ -6,12 +6,5 @@
package miniJava.AbstractSyntaxTrees;
public enum TypeKind {
- VOID,
- INT,
- BOOLEAN,
- CLASS,
- ARRAY,
- UNSUPPORTED,
- ERROR,
- VALID;
+ VOID, INT, BOOLEAN, CLASS, ARRAY, UNSUPPORTED, ERROR, EQUALS, RELATIONAL;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/UnaryExpr.java b/miniJava/src/miniJava/AbstractSyntaxTrees/UnaryExpr.java
index 6bff8ce..eeb8e56 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/UnaryExpr.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/UnaryExpr.java
@@ -7,18 +7,17 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class UnaryExpr extends Expression
-{
- public UnaryExpr(Operator o, Expression e, SourcePosition posn){
- super(posn);
- operator = o;
- expr = e;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitUnaryExpr(this, o);
- }
+public class UnaryExpr extends Expression {
+ public UnaryExpr(Operator o, Expression e, SourcePosition posn) {
+ super(posn);
+ operator = o;
+ expr = e;
+ }
- public Operator operator;
- public Expression expr;
+ public R visit(Visitor v, A o) {
+ return v.visitUnaryExpr(this, o);
+ }
+
+ public Operator operator;
+ public Expression expr;
}
\ No newline at end of file
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/VarDecl.java b/miniJava/src/miniJava/AbstractSyntaxTrees/VarDecl.java
index 3fe19a8..2be65c3 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/VarDecl.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/VarDecl.java
@@ -8,12 +8,12 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class VarDecl extends LocalDecl {
-
+
public VarDecl(Type t, String name, SourcePosition posn) {
super(name, t, posn);
}
-
- public R visit(Visitor v, A o) {
+
+ public R visit(Visitor v, A o) {
return v.visitVarDecl(this, o);
}
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/VarDeclStmt.java b/miniJava/src/miniJava/AbstractSyntaxTrees/VarDeclStmt.java
index b960b2e..3e74d70 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/VarDeclStmt.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/VarDeclStmt.java
@@ -7,18 +7,17 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class VarDeclStmt extends Statement
-{
- public VarDeclStmt(VarDecl vd, Expression e, SourcePosition posn){
- super(posn);
- varDecl = vd;
- initExp = e;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitVardeclStmt(this, o);
- }
+public class VarDeclStmt extends Statement {
+ public VarDeclStmt(VarDecl vd, Expression e, SourcePosition posn) {
+ super(posn);
+ varDecl = vd;
+ initExp = e;
+ }
- public VarDecl varDecl;
- public Expression initExp;
+ public R visit(Visitor v, A o) {
+ return v.visitVardeclStmt(this, o);
+ }
+
+ public VarDecl varDecl;
+ public Expression initExp;
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/Visitor.java b/miniJava/src/miniJava/AbstractSyntaxTrees/Visitor.java
index 74506e0..2045d36 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/Visitor.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/Visitor.java
@@ -6,52 +6,75 @@
package miniJava.AbstractSyntaxTrees;
/**
- * An implementation of the Visitor interface provides a method visitX
- * for each non-abstract AST class X.
+ * An implementation of the Visitor interface provides a method visitX for each
+ * non-abstract AST class X.
*/
-public interface Visitor {
+public interface Visitor {
- // Package
- public ResultType visitPackage(Package prog, ArgType arg);
+ // Package
+ public ResultType visitPackage(Package prog, ArgType arg);
- // Declarations
- public ResultType visitClassDecl(ClassDecl cd, ArgType arg);
- public ResultType visitFieldDecl(FieldDecl fd, ArgType arg);
- public ResultType visitMethodDecl(MethodDecl md, ArgType arg);
- public ResultType visitParameterDecl(ParameterDecl pd, ArgType arg);
- public ResultType visitVarDecl(VarDecl decl, ArgType arg);
-
- // Types
- public ResultType visitBaseType(BaseType type, ArgType arg);
- public ResultType visitClassType(ClassType type, ArgType arg);
- public ResultType visitArrayType(ArrayType type, ArgType arg);
-
- // Statements
- public ResultType visitBlockStmt(BlockStmt stmt, ArgType arg);
- public ResultType visitVardeclStmt(VarDeclStmt stmt, ArgType arg);
- public ResultType visitAssignStmt(AssignStmt stmt, ArgType arg);
- public ResultType visitCallStmt(CallStmt stmt, ArgType arg);
- public ResultType visitIfStmt(IfStmt stmt, ArgType arg);
- public ResultType visitWhileStmt(WhileStmt stmt, ArgType arg);
-
- // Expressions
- public ResultType visitUnaryExpr(UnaryExpr expr, ArgType arg);
- public ResultType visitBinaryExpr(BinaryExpr expr, ArgType arg);
- public ResultType visitRefExpr(RefExpr expr, ArgType arg);
- public ResultType visitCallExpr(CallExpr expr, ArgType arg);
- public ResultType visitLiteralExpr(LiteralExpr expr, ArgType arg);
- public ResultType visitNewObjectExpr(NewObjectExpr expr, ArgType arg);
- public ResultType visitNewArrayExpr(NewArrayExpr expr, ArgType arg);
-
- // References
- public ResultType visitQualifiedRef(QualifiedRef ref, ArgType arg);
- public ResultType visitIndexedRef(IndexedRef ref, ArgType arg);
- public ResultType visitIdRef(IdRef ref, ArgType arg);
- public ResultType visitThisRef(ThisRef ref, ArgType arg);
+ // Declarations
+ public ResultType visitClassDecl(ClassDecl cd, ArgType arg);
- // Terminals
- public ResultType visitIdentifier(Identifier id, ArgType arg);
- public ResultType visitOperator(Operator op, ArgType arg);
- public ResultType visitIntLiteral(IntLiteral num, ArgType arg);
- public ResultType visitBooleanLiteral(BooleanLiteral bool, ArgType arg);
+ public ResultType visitFieldDecl(FieldDecl fd, ArgType arg);
+
+ public ResultType visitMethodDecl(MethodDecl md, ArgType arg);
+
+ public ResultType visitParameterDecl(ParameterDecl pd, ArgType arg);
+
+ public ResultType visitVarDecl(VarDecl decl, ArgType arg);
+
+ // Types
+ public ResultType visitBaseType(BaseType type, ArgType arg);
+
+ public ResultType visitClassType(ClassType type, ArgType arg);
+
+ public ResultType visitArrayType(ArrayType type, ArgType arg);
+
+ // Statements
+ public ResultType visitBlockStmt(BlockStmt stmt, ArgType arg);
+
+ public ResultType visitVardeclStmt(VarDeclStmt stmt, ArgType arg);
+
+ public ResultType visitAssignStmt(AssignStmt stmt, ArgType arg);
+
+ public ResultType visitCallStmt(CallStmt stmt, ArgType arg);
+
+ public ResultType visitIfStmt(IfStmt stmt, ArgType arg);
+
+ public ResultType visitWhileStmt(WhileStmt stmt, ArgType arg);
+
+ // Expressions
+ public ResultType visitUnaryExpr(UnaryExpr expr, ArgType arg);
+
+ public ResultType visitBinaryExpr(BinaryExpr expr, ArgType arg);
+
+ public ResultType visitRefExpr(RefExpr expr, ArgType arg);
+
+ public ResultType visitCallExpr(CallExpr expr, ArgType arg);
+
+ public ResultType visitLiteralExpr(LiteralExpr expr, ArgType arg);
+
+ public ResultType visitNewObjectExpr(NewObjectExpr expr, ArgType arg);
+
+ public ResultType visitNewArrayExpr(NewArrayExpr expr, ArgType arg);
+
+ // References
+ public ResultType visitQualifiedRef(QualifiedRef ref, ArgType arg);
+
+ public ResultType visitIndexedRef(IndexedRef ref, ArgType arg);
+
+ public ResultType visitIdRef(IdRef ref, ArgType arg);
+
+ public ResultType visitThisRef(ThisRef ref, ArgType arg);
+
+ // Terminals
+ public ResultType visitIdentifier(Identifier id, ArgType arg);
+
+ public ResultType visitOperator(Operator op, ArgType arg);
+
+ public ResultType visitIntLiteral(IntLiteral num, ArgType arg);
+
+ public ResultType visitBooleanLiteral(BooleanLiteral bool, ArgType arg);
}
diff --git a/miniJava/src/miniJava/AbstractSyntaxTrees/WhileStmt.java b/miniJava/src/miniJava/AbstractSyntaxTrees/WhileStmt.java
index d76ac87..3695b45 100644
--- a/miniJava/src/miniJava/AbstractSyntaxTrees/WhileStmt.java
+++ b/miniJava/src/miniJava/AbstractSyntaxTrees/WhileStmt.java
@@ -7,18 +7,17 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
-public class WhileStmt extends Statement
-{
- public WhileStmt(Expression b, Statement s, SourcePosition posn){
- super(posn);
- cond = b;
- body = s;
- }
-
- public R visit(Visitor v, A o) {
- return v.visitWhileStmt(this, o);
- }
+public class WhileStmt extends Statement {
+ public WhileStmt(Expression b, Statement s, SourcePosition posn) {
+ super(posn);
+ cond = b;
+ body = s;
+ }
- public Expression cond;
- public Statement body;
+ public R visit(Visitor v, A o) {
+ return v.visitWhileStmt(this, o);
+ }
+
+ public Expression cond;
+ public Statement body;
}
diff --git a/miniJava/src/miniJava/Compiler.java b/miniJava/src/miniJava/Compiler.java
index 74543e2..c5e46ac 100644
--- a/miniJava/src/miniJava/Compiler.java
+++ b/miniJava/src/miniJava/Compiler.java
@@ -3,25 +3,24 @@ package miniJava;
import java.io.*;
import miniJava.SyntacticAnalyzer.*;
-import miniJava.AbstractSyntaxTrees.ASTDisplay;
+// import miniJava.AbstractSyntaxTrees.ASTDisplay;
import miniJava.AbstractSyntaxTrees.Package;
import miniJava.ContextualAnalyzer.Analyzer;
-import miniJava.ContextualAnalyzer.Reporter;
import miniJava.Exceptions.*;
public class Compiler {
-
- private static final int rc = 4;
-
+
+ public static final int rc = 4;
+
public static void main(String[] args) {
-
- if(args.length == 0) {
+
+ if (args.length == 0) {
System.out.println("No file specified");
System.exit(rc);
}
-
- try(FileReader input = new FileReader(args[0])) {
-
+
+ try (FileReader input = new FileReader(args[0])) {
+
// Setup
Scanner scanner = new Scanner(new BufferedReader(input));
Parser parser = new Parser(scanner);
@@ -30,24 +29,22 @@ public class Compiler {
// Display
// ASTDisplay display = new ASTDisplay();
// display.showTree(p);
-
+
// Identification/Type Checking
Analyzer analyzer = new Analyzer();
analyzer.visitPackage(p, null);
- if(Reporter.error) System.exit(rc);
-
- System.exit(0);
-
- } catch(FileNotFoundException e) {
+ System.exit(analyzer.validate());
+
+ } catch (FileNotFoundException e) {
System.out.println("***" + e.getMessage());
- } catch(IOException e) {
+ } catch (IOException e) {
System.out.println("***" + e.getMessage());
- }catch(ScanningException e) {
+ } catch (ScanningException e) {
System.out.println("***" + e.getMessage());
- } catch(ParsingException e) {
+ } catch (ParsingException e) {
System.out.println("***" + e.getMessage());
}
-
+
System.exit(rc);
}
diff --git a/miniJava/src/miniJava/ContextualAnalyzer/Analyzer.java b/miniJava/src/miniJava/ContextualAnalyzer/Analyzer.java
index 0026c65..06a2aca 100644
--- a/miniJava/src/miniJava/ContextualAnalyzer/Analyzer.java
+++ b/miniJava/src/miniJava/ContextualAnalyzer/Analyzer.java
@@ -1,454 +1,568 @@
package miniJava.ContextualAnalyzer;
+import java.util.*;
+
+import miniJava.Compiler;
+import miniJava.Exceptions.*;
+import miniJava.SyntacticAnalyzer.*;
+import miniJava.SyntacticAnalyzer.Scanner;
import miniJava.AbstractSyntaxTrees.*;
import miniJava.AbstractSyntaxTrees.Package;
-import miniJava.SyntacticAnalyzer.Parser;
-import miniJava.SyntacticAnalyzer.Scanner;
-import miniJava.Exceptions.*;
-// Check types
-// Ensure static members
-// Check for main function
public class Analyzer implements Visitor {
-
- // Topmost Identification Table
+
+ private MethodDecl mainMethod = null;
+ private ClassDecl currentClassDecl = null;
+ private MethodDecl currentMethodDecl = null;
private IdentificationTable table = new IdentificationTable();
-
- // Current class decl is for use with the 'this' keyword
- private Declaration currentClassDecl = null;
-
+
+ // Keep track of all predefined names to handle
+ private static ArrayList predefined;
+ static
+ {
+ predefined = new ArrayList();
+ predefined.add("class _PrintStream { public void println(int n){} }");
+ predefined.add("class System { public static _PrintStream out; }");
+ predefined.add("class String { }");
+ }
+
/**
- * Adds predefined names to identification table.
+ * Adds predefined names to topmost table. These names should not be allowed
+ * redefinition and must be ordered such that no class refers to one
+ * undeclared yet.
*/
- public Analyzer() {
- try {
- String __PrintStream = "class _PrintStream { public void println(int n){} }";
- new Parser(new Scanner(__PrintStream)).parse().visit(this, table);
-
- String _System = "class System { public static _PrintStream out; }";
- new Parser(new Scanner(_System)).parse().visit(this, table);
-
- String _String = "class String { }";
- new Parser(new Scanner(_String)).parse().visit(this, table);
-
- } catch(ParsingException e) {
- System.out.println("Predefined declarations parsing error!");
- } catch(ScanningException e) {
- System.out.println("Predefined declarations scanning error!");
+ public Analyzer() throws ParsingException, ScanningException {
+ for (String s : predefined) {
+ Scanner scanner = new Scanner(s);
+ Parser parser = new Parser(scanner);
+ parser.parse().visit(this, table);
}
}
-
+
/**
- * Check that two types match.
- * @param t1
- * @param t2
+ * Checks that contextual analysis was successful or not, returning the
+ * proper error code in either case.
+ *
* @return
*/
- private void match(Type t1, Type t2) {
-
- // Check class types match
- if(t1.typeKind == TypeKind.CLASS) {
- if(t2.typeKind != TypeKind.CLASS) {
- Reporter.report(ErrorType.TYPE_MISMATCH, t1, t2);
- } else {
- ClassType c1 = (ClassType) t1;
- ClassType c2 = (ClassType) t2;
- if(!c1.className.spelling.equals(c2.className.spelling)) {
- Reporter.report(ErrorType.TYPE_CLASS_MISMATCH, t1, t2);
- }
- }
- }
-
- // Check array types match
- else if(t1.typeKind == TypeKind.ARRAY) {
- if(t2.typeKind != TypeKind.ARRAY) {
- Reporter.report(ErrorType.TYPE_MISMATCH, t1, t2);
- } else {
- ArrayType a1 = (ArrayType) t1;
- ArrayType a2 = (ArrayType) t2;
- if(a1.eltType.typeKind != a2.eltType.typeKind) {
- Reporter.report(ErrorType.TYPE_ARRAY_MISMATCH, t1, t2);
- }
- }
- }
-
- // Check primitive types match
- else if(t1.typeKind != t2.typeKind) {
- Reporter.report(ErrorType.TYPE_MISMATCH, t1, t2);
- }
+ public int validate() {
+ // Exactly one public static void main(String[] args) function must be declared
+ if (mainMethod == null)
+ Reporter.report(ErrorType.MAIN_UNDECLARED, null, null);
+
+ return (Reporter.error) ? Compiler.rc : 0;
}
-
- @Override
+
+
+ // /////////////////////////////////////////////////////////////////////////////
+ //
+ // PACKAGE
+ //
+ // /////////////////////////////////////////////////////////////////////////////
+
public Type visitPackage(Package prog, IdentificationTable arg) {
-
- /* Since classes and static methods/fields can be referenced
- * before the classes themselves are declared, we preprocess
- * all classes and methods first.
+
+ /*
+ * Since classes and static methods/fields can be referenced before the
+ * classes themselves are declared, we preprocess all classes and
+ * methods first.
*/
- for(ClassDecl cd : prog.classDeclList) {
-
+ for (ClassDecl cd : prog.classDeclList) {
+
IdentificationTable cdTable = table.openScope(cd);
- if(cdTable != null) {
-
+
+ if (cdTable != null) {
+
// Simply add fields to current scope
- for(FieldDecl fd : cd.fieldDeclList) {
+ for (FieldDecl fd : cd.fieldDeclList) {
cdTable.setDeclarationAtScope(fd);
}
-
+
// Just Add Declaration (also creates another identification table)
- for(MethodDecl md : cd.methodDeclList) {
+ for (MethodDecl md : cd.methodDeclList) {
cdTable.openScope(md);
}
}
}
-
+
// Begin Traversal
- for(ClassDecl cd : prog.classDeclList) {
+ for (ClassDecl cd : prog.classDeclList) {
currentClassDecl = cd;
cd.visit(this, table);
}
-
+
return null;
}
- @Override
+
+ // /////////////////////////////////////////////////////////////////////////////
+ //
+ // DECLARATIONS
+ //
+ // /////////////////////////////////////////////////////////////////////////////
+
public Type visitClassDecl(ClassDecl cd, IdentificationTable arg) {
+
IdentificationTable cdTable = arg.getScope(cd);
- if(cdTable != null) {
-
- for(FieldDecl fd : cd.fieldDeclList) {
+
+ if (cdTable != null) {
+
+ for (FieldDecl fd : cd.fieldDeclList) {
fd.visit(this, cdTable);
}
-
- for(MethodDecl md : cd.methodDeclList) {
+
+ for (MethodDecl md : cd.methodDeclList) {
+ currentMethodDecl = md;
md.visit(this, cdTable);
}
}
-
+
return cd.type;
}
- @Override
public Type visitFieldDecl(FieldDecl fd, IdentificationTable arg) {
-
+
// Must check that the type of the field can be identified
- table.validateClassId(fd.type);
-
+ if (!table.classExists(fd.type)) {
+ Reporter.report(ErrorType.UNDECLARED_TYPE, fd.type, null);
+ }
+
return fd.type;
}
- @Override
public Type visitMethodDecl(MethodDecl md, IdentificationTable arg) {
-
- IdentificationTable mdTable = arg.getScope(md);
-
+
+ // Check if a valid entry point to program
+ if (IdentificationTable.isMainMethod(md)) {
+ if (mainMethod != null)
+ Reporter.report(ErrorType.MULTIPLE_MAIN, md, mainMethod);
+ else
+ mainMethod = md;
+ }
+
// Must check that the type of the method can be identified
- table.validateClassId(md.type);
-
+ if (!table.classExists(md.type)) {
+ Reporter.report(ErrorType.UNDECLARED_TYPE, md.type, null);
+ }
+
// Continue Traversal
- if(mdTable != null) {
- for(ParameterDecl pd : md.parameterDeclList) {
+ IdentificationTable mdTable = arg.getScope(md);
+ if (mdTable != null) {
+ for (ParameterDecl pd : md.parameterDeclList)
pd.visit(this, mdTable);
- }
-
- for(Statement s : md.statementList) {
+ for (Statement s : md.statementList)
s.visit(this, mdTable);
- }
-
+
// Check that return type matches expected type
- if(md.returnExp != null) {
+ if (md.returnExp == null && md.type.typeKind != TypeKind.VOID) {
+ Reporter.report(ErrorType.NO_RETURN, md, null);
+ } else if (md.returnExp != null) {
Type returnType = md.returnExp.visit(this, mdTable);
- match(md.type, returnType);
- } else if(md.type.typeKind != TypeKind.VOID) {
- Reporter.report(ErrorType.NO_RETURN_EXPRESSION, md.type);
+ IdentificationTable.match(md.type, returnType, true);
}
}
-
+
return md.type;
}
- @Override
public Type visitParameterDecl(ParameterDecl pd, IdentificationTable arg) {
arg.setDeclarationAtScope(pd);
- table.validateClassId(pd.type);
+ if (!table.classExists(pd.type)) {
+ Reporter.report(ErrorType.UNDECLARED_TYPE, pd.type, null);
+ }
+
return pd.type;
}
- @Override
public Type visitVarDecl(VarDecl decl, IdentificationTable arg) {
arg.setDeclarationAtScope(decl);
- table.validateClassId(decl.type);
+ if (!table.classExists(decl.type)) {
+ Reporter.report(ErrorType.UNDECLARED_TYPE, decl.type, null);
+ }
+
return decl.type;
}
+
+
+ // /////////////////////////////////////////////////////////////////////////////
+ //
+ // TYPES
+ //
+ // /////////////////////////////////////////////////////////////////////////////
- @Override
public Type visitBaseType(BaseType type, IdentificationTable arg) {
-
+
return type;
}
- @Override
public Type visitClassType(ClassType type, IdentificationTable arg) {
type.className.visit(this, arg);
return type;
}
- @Override
public Type visitArrayType(ArrayType type, IdentificationTable arg) {
type.eltType.visit(this, arg);
return type;
}
+
+
+ // /////////////////////////////////////////////////////////////////////////////
+ //
+ // STATEMENTS
+ //
+ // /////////////////////////////////////////////////////////////////////////////
- @Override
public Type visitBlockStmt(BlockStmt stmt, IdentificationTable arg) {
arg.pushLevel();
- for(Statement s : stmt.sl) {
+ for (Statement s : stmt.sl)
s.visit(this, arg);
- }
arg.popLevel();
-
+
return null;
}
- @Override
+ // The stmt of the vardecl may not refer to the variable itself so we check the stmt first
public Type visitVardeclStmt(VarDeclStmt stmt, IdentificationTable arg) {
-
- // The stmt of the vardecl may not refer to the variable itself so we check the stmt first
Type initExpType = stmt.initExp.visit(this, arg);
Type varDeclType = stmt.varDecl.visit(this, arg);
- match(varDeclType, initExpType);
+ IdentificationTable.match(varDeclType, initExpType, true);
return varDeclType;
}
- @Override
public Type visitAssignStmt(AssignStmt stmt, IdentificationTable arg) {
Type valType = stmt.val.visit(this, arg);
Type refType = stmt.ref.visit(this, arg);
- match(valType, refType);
-
+ IdentificationTable.match(valType, refType, true);
+
return refType;
}
- @Override
public Type visitCallStmt(CallStmt stmt, IdentificationTable arg) {
+
Type methodType = stmt.methodRef.visit(this, arg);
-
+
// Check that parameter count is correct and each type is correct
MethodDecl decl = (MethodDecl) stmt.methodRef.decl;
- if(decl.parameterDeclList.size() != stmt.argList.size()) {
- Reporter.report(ErrorType.INVALID_PARAM_COUNT, stmt);
+ if (decl.parameterDeclList.size() != stmt.argList.size()) {
+ Reporter.report(ErrorType.INVALID_PARAM_COUNT, stmt, decl);
} else {
- for(int i = 0; i < stmt.argList.size(); i++) {
+ for (int i = 0; i < stmt.argList.size(); i++) {
Type exprType = stmt.argList.get(i).visit(this, arg);
Type pdType = decl.parameterDeclList.get(i).type;
- match(pdType, exprType);
+ IdentificationTable.match(pdType, exprType, true);
}
}
-
+
return methodType;
}
- @Override
public Type visitIfStmt(IfStmt stmt, IdentificationTable arg) {
-
+
// The conditional statment must be a boolean
Type condType = stmt.cond.visit(this, arg);
- match(new BaseType(TypeKind.BOOLEAN, null), condType);
-
+ IdentificationTable.match(new BaseType(TypeKind.BOOLEAN, null), condType, true);
+
// A single vardecl cannot exist after a conditional statement
- if(stmt.thenStmt instanceof VarDeclStmt) {
- Reporter.report(ErrorType.VAR_COND_ONLY, stmt.thenStmt);
+ if (stmt.thenStmt instanceof VarDeclStmt) {
+ Reporter.report(ErrorType.SINGLE_VARCOND, stmt.thenStmt, null);
} else {
stmt.thenStmt.visit(this, arg);
- if(stmt.elseStmt != null) {
- if(stmt.elseStmt instanceof VarDeclStmt) {
- Reporter.report(ErrorType.VAR_COND_ONLY, stmt.elseStmt);
+ if (stmt.elseStmt != null) {
+ if (stmt.elseStmt instanceof VarDeclStmt) {
+ Reporter.report(ErrorType.SINGLE_VARCOND, stmt.elseStmt,
+ null);
} else {
stmt.elseStmt.visit(this, arg);
}
}
}
-
+
return null;
}
- @Override
public Type visitWhileStmt(WhileStmt stmt, IdentificationTable arg) {
-
+
// The conditional statment must be a boolean
Type condType = stmt.cond.visit(this, arg);
- match(new BaseType(TypeKind.BOOLEAN, null), condType);
-
- if(stmt.body instanceof VarDeclStmt) {
- Reporter.report(ErrorType.VAR_COND_ONLY, stmt.body);
+ IdentificationTable.match(new BaseType(TypeKind.BOOLEAN, null), condType, true);
+
+ // A single vardecl cannot exist after a conditional statement
+ if (stmt.body instanceof VarDeclStmt) {
+ Reporter.report(ErrorType.SINGLE_VARCOND, stmt.body, null);
} else {
stmt.body.visit(this, arg);
}
-
+
return null;
}
- @Override
+
+ // /////////////////////////////////////////////////////////////////////////////
+ //
+ // EXPRESSIONS
+ //
+ // /////////////////////////////////////////////////////////////////////////////
+
public Type visitUnaryExpr(UnaryExpr expr, IdentificationTable arg) {
Type opType = expr.operator.visit(this, arg);
Type exprType = expr.expr.visit(this, arg);
- match(opType, exprType);
-
- return null;
+ IdentificationTable.match(opType, exprType, true);
+
+ return opType;
}
- @Override
public Type visitBinaryExpr(BinaryExpr expr, IdentificationTable arg) {
Type opType = expr.operator.visit(this, arg);
Type leftType = expr.left.visit(this, arg);
Type rightType = expr.right.visit(this, arg);
- match(opType, leftType);
- match(opType, rightType);
+ // Both sides must be the same
+ if(opType.typeKind == TypeKind.EQUALS) {
+ IdentificationTable.match(leftType, rightType, true);
+ return new BaseType(TypeKind.BOOLEAN, opType.posn);
+ }
- return null;
+ // Both sides must be integers
+ else if(opType.typeKind == TypeKind.RELATIONAL) {
+ BaseType bt = new BaseType(TypeKind.INT, opType.posn);
+ IdentificationTable.match(bt, leftType, true);
+ IdentificationTable.match(bt, rightType, true);
+ return new BaseType(TypeKind.BOOLEAN, opType.posn);
+ }
+
+ // Both sides must match operator type
+ IdentificationTable.match(leftType, opType, true);
+ IdentificationTable.match(rightType, opType, true);
+ return opType;
}
- @Override
public Type visitRefExpr(RefExpr expr, IdentificationTable arg) {
-
- return expr.ref.visit(this, arg);
+ Type exprType = expr.ref.visit(this, arg);
+ return exprType;
}
- @Override
public Type visitCallExpr(CallExpr expr, IdentificationTable arg) {
- Type functionType = expr.functionRef.visit(this, arg);
+ Type functionRefType = expr.functionRef.visit(this, arg);
- // Check that parameter count is correct and each type is correct
- MethodDecl decl = (MethodDecl) expr.functionRef.decl;
- if(decl.parameterDeclList.size() != expr.argList.size()) {
- Reporter.report(ErrorType.INVALID_PARAM_COUNT, expr);
+ if(expr.functionRef.decl instanceof MethodDecl) {
+
+ MethodDecl decl = (MethodDecl) expr.functionRef.decl;
+
+ // Check that parameter count is correct and each type is correct
+ if (decl.parameterDeclList.size() != expr.argList.size()) {
+ Reporter.report(ErrorType.INVALID_PARAM_COUNT, expr, decl);
+ } else {
+ for (int i = 0; i < expr.argList.size(); i++) {
+ Type exprType = expr.argList.get(i).visit(this, arg);
+ Type pdType = decl.parameterDeclList.get(i).type;
+ IdentificationTable.match(pdType, exprType, true);
+ }
+ }
} else {
- for(int i = 0; i < expr.argList.size(); i++) {
- Type exprType = expr.argList.get(i).visit(this, arg);
- Type pdType = decl.parameterDeclList.get(i).type;
- match(pdType, exprType);
+ Reporter.report(ErrorType.NONFUNCTION_CALL, expr, null);
+ }
+
+ return functionRefType;
+ }
+
+ public Type visitLiteralExpr(LiteralExpr expr, IdentificationTable arg) {
+ Type literalType = expr.literal.visit(this, arg);
+ return literalType;
+ }
+
+ public Type visitNewObjectExpr(NewObjectExpr expr, IdentificationTable arg) {
+ Type objectType = expr.classtype.visit(this, arg);
+ return objectType;
+ }
+
+ public Type visitNewArrayExpr(NewArrayExpr expr, IdentificationTable arg) {
+ Type sizeExprType = expr.sizeExpr.visit(this, arg);
+ if (sizeExprType.typeKind != TypeKind.INT) {
+ Reporter.report(ErrorType.INVALID_INDEX, expr.sizeExpr, null);
+ }
+
+ Type eltType = expr.eltType.visit(this, arg);
+ return new ArrayType(eltType, expr.posn);
+ }
+
+
+ // /////////////////////////////////////////////////////////////////////////////
+ //
+ // REFERENCES
+ //
+ // /////////////////////////////////////////////////////////////////////////////
+
+ public Type visitQualifiedRef(QualifiedRef ref, IdentificationTable arg) {
+
+ Type refType = ref.ref.visit(this, arg);
+
+ // Note qualified ref's only make sense in the context of classes
+ if(refType.typeKind != TypeKind.CLASS) {
+ if(refType.typeKind != TypeKind.ERROR) // Don't need to report multiple times
+ Reporter.report(ErrorType.TYPE_MISMATCH, new BaseType(TypeKind.CLASS, null), refType);
+
+ return refType;
+ }
+
+ // Try to find qualified member
+ Declaration qualified = ref.ref.decl;
+ ClassType qualType = (ClassType) qualified.type;
+ Declaration qualClassDecl = table.getDeclaration(qualType.className.spelling);
+ if(qualClassDecl == null) {
+ Reporter.report(ErrorType.UNDECLARED_TYPE, qualified.type, null);
+ return new BaseType(TypeKind.ERROR, qualified.posn);
+ }
+
+ // Get member
+ IdentificationTable cdTable = table.getScope(qualClassDecl);
+ MemberDecl md = (MemberDecl) cdTable.getDeclarationAtScope(ref.id.spelling);
+
+ // Check the member exists at all
+ if(md == null) {
+ Reporter.report(ErrorType.UNDEFINED, ref.id, null);
+ return new BaseType(TypeKind.ERROR, ref.id.posn);
+ }
+
+ // If the qualifed ref is a class declaration, members must be static
+ else if(qualified instanceof ClassDecl) {
+ if(!md.isStatic) {
+ Reporter.report(ErrorType.STATIC, md, ref.id);
+ return new BaseType(TypeKind.ERROR, ref.id.posn);
+ } else if(md.isPrivate) {
+ Reporter.report(ErrorType.VISIBILITY, md, ref.id);
+ return new BaseType(TypeKind.ERROR, ref.id.posn);
}
}
- return functionType;
- }
-
- @Override
- public Type visitLiteralExpr(LiteralExpr expr, IdentificationTable arg) {
-
- return expr.literal.visit(this, arg);
- }
-
- @Override
- public Type visitNewObjectExpr(NewObjectExpr expr, IdentificationTable arg) {
-
- return expr.classtype.visit(this, arg);
- }
-
- @Override
- public Type visitNewArrayExpr(NewArrayExpr expr, IdentificationTable arg) {
- Type sizeExprType = expr.sizeExpr.visit(this, arg);
- if(sizeExprType.typeKind != TypeKind.INT) {
- Reporter.report(ErrorType.INVALID_INDEX, expr.sizeExpr);
+ // The member should not be a method, as this is unsupported
+ else if(qualified instanceof MethodDecl) {
+ Reporter.report(ErrorType.UNDEFINED, ref.id, null);
+ }
+
+ // Otherwise, we can assume the object is a variable and attempt to access members
+ else if(md.isPrivate && currentClassDecl != qualClassDecl) {
+ Reporter.report(ErrorType.VISIBILITY, md, ref.id);
+ return new BaseType(TypeKind.ERROR, ref.id.posn);
}
- return expr.eltType.visit(this, arg);
+ ref.id.decl = md;
+ ref.decl = md;
+ return md.type;
}
- @Override
- public Type visitQualifiedRef(QualifiedRef ref, IdentificationTable arg) {
- ref.ref.visit(this, arg);
- ref.id.visit(this, arg);
-
- // Check that each declaration is nested properly
- if(ref.decl == null || table.getDeclaration(ref.decl.name) == null) {
- System.out.println(ref.id.spelling);
- // report(ErrorType.MISSING_DECL, ref.ref.decl);
- }
-
- return null;
- }
-
- @Override
public Type visitIndexedRef(IndexedRef ref, IdentificationTable arg) {
- ref.ref.visit(this, arg);
- ref.decl = ref.ref.decl;
-
+
+ Type refType = ref.ref.visit(this, arg);
+
// Make sure index is an integer
Type indexExprType = ref.indexExpr.visit(this, arg);
- if(indexExprType.typeKind != TypeKind.INT) {
- Reporter.report(ErrorType.INVALID_INDEX, ref.indexExpr);
+ if (indexExprType.typeKind != TypeKind.INT) {
+ Reporter.report(ErrorType.INVALID_INDEX, ref.indexExpr, null);
}
-
- return ref.decl.type;
+
+ ref.decl = ref.ref.decl;
+ return refType;
}
- @Override
public Type visitIdRef(IdRef ref, IdentificationTable arg) {
- ref.id.visit(this, arg);
+ Type idType = ref.id.visit(this, arg);
ref.decl = ref.id.decl;
-
- return ref.decl.type;
+
+ return idType;
}
- @Override
public Type visitThisRef(ThisRef ref, IdentificationTable arg) {
ref.decl = currentClassDecl;
+ if(currentMethodDecl.isStatic) {
+ Reporter.report(ErrorType.THIS, ref, currentMethodDecl);
+ }
return ref.decl.type;
}
+
+
+ // /////////////////////////////////////////////////////////////////////////////
+ //
+ // TERMINALS
+ //
+ // /////////////////////////////////////////////////////////////////////////////
- @Override
public Type visitIdentifier(Identifier id, IdentificationTable arg) {
- // Try to find identifier, reporting a message if it cannot be found
- Declaration decl = arg.getDeclaration(id.spelling);
- if(decl == null) {
- Reporter.report(ErrorType.UNIDENTIFIED, id);
- return new BaseType(TypeKind.ERROR, null);
+
+ // Check if identifier can be found in current scope
+ Declaration decl = arg.getDeclarationAtScope(id.spelling);
+ if (decl != null) { id.decl = decl; return decl.type; }
+
+ // Access member and check visibility properties
+ Declaration d = arg.getDeclaration(id.spelling);
+ if(d == null) {
+ Reporter.report(ErrorType.UNDEFINED, id, null);
+ return new BaseType(TypeKind.ERROR, id.posn);
} else {
- id.decl = decl;
- return decl.type;
+
+ // Can only be a member at this point
+ if(d.type.typeKind != TypeKind.CLASS) {
+ MemberDecl md = (MemberDecl) d;
+
+ // A static method cannot access instance members
+ if(currentMethodDecl.isStatic && !md.isStatic) {
+ Reporter.report(ErrorType.STATIC, md, id);
+ return new BaseType(TypeKind.ERROR, id.posn);
+ }
+
+ // If a member declaration is private, it must be a member of the current class
+ else if(md.isPrivate) {
+
+ // Check if member is part of the current class
+ IdentificationTable cdTable = table.getScope(currentClassDecl);
+ if(cdTable.getDeclarationAtScope(md.name) == null) {
+ Reporter.report(ErrorType.VISIBILITY, md, id);
+ return new BaseType(TypeKind.ERROR, id.posn);
+ }
+ }
+ }
+
+ id.decl = d;
+ return d.type;
}
}
- @Override
public Type visitOperator(Operator op, IdentificationTable arg) {
- switch(op.token.spelling) {
+ switch (op.token.spelling) {
case "!":
- case ">":
- case "<":
- case "==":
- case "<=":
- case ">=":
- case "!=":
case "&&":
case "||":
return new BaseType(TypeKind.BOOLEAN, op.posn);
+ case ">":
+ case "<":
+ case "<=":
+ case ">=":
+ return new BaseType(TypeKind.RELATIONAL, op.posn);
+
case "+":
case "-":
case "*":
case "/":
return new BaseType(TypeKind.INT, op.posn);
- default:
- Reporter.report(ErrorType.UNIDENTIFIED_TYPE, op);
- return new BaseType(TypeKind.ERROR, op.posn);
+ case "==":
+ case "!=":
+ return new BaseType(TypeKind.EQUALS, op.posn);
}
+
+ return null;
}
- @Override
public Type visitIntLiteral(IntLiteral num, IdentificationTable arg) {
-
+
return new BaseType(TypeKind.INT, num.posn);
}
- @Override
public Type visitBooleanLiteral(BooleanLiteral bool, IdentificationTable arg) {
-
+
return new BaseType(TypeKind.BOOLEAN, bool.posn);
}
diff --git a/miniJava/src/miniJava/ContextualAnalyzer/IdentificationTable.java b/miniJava/src/miniJava/ContextualAnalyzer/IdentificationTable.java
index d2d56e4..6faa248 100644
--- a/miniJava/src/miniJava/ContextualAnalyzer/IdentificationTable.java
+++ b/miniJava/src/miniJava/ContextualAnalyzer/IdentificationTable.java
@@ -1,26 +1,22 @@
package miniJava.ContextualAnalyzer;
-import java.util.HashMap;
-import java.util.ArrayList;
+import java.util.*;
-import miniJava.AbstractSyntaxTrees.ClassType;
-import miniJava.AbstractSyntaxTrees.Declaration;
-import miniJava.AbstractSyntaxTrees.Type;
-import miniJava.AbstractSyntaxTrees.TypeKind;
+import miniJava.AbstractSyntaxTrees.*;
public class IdentificationTable {
-
+
private IdentificationTable parent;
private HashMap scope;
private ArrayList> table;
-
+
/**
*
*/
public IdentificationTable() {
this(null);
}
-
+
/**
*
* @param parent
@@ -31,31 +27,31 @@ public class IdentificationTable {
this.table = new ArrayList>();
this.table.add(new HashMap());
}
-
+
/**
- * Adds another level for variables to be stored at (they will be
- * removed when popping said level).
- */
- public void pushLevel() {
- table.add(new HashMap());
- }
-
- /**
- * Removes all variables declared at the current level (these are
- * no longer accessible).
+ * Adds another level for variables to be stored at
*/
public void popLevel() {
table.remove(table.size() - 1);
}
-
+
/**
- * This method will only ever be called with class/method declarations
+ * Removes all variables declared at the current level
+ */
+ public void pushLevel() {
+ table.add(new HashMap());
+ }
+
+ /**
+ * This method will only ever be called with class/method declarations.
+ *
* @param decl
* @return
*/
public IdentificationTable openScope(Declaration decl) {
- if(scope.containsKey(decl.name) || getDeclarationAtScope(decl.name) != null) {
- Reporter.report(ErrorType.REDEFINITION, decl);
+ Declaration current = getDeclarationAtScope(decl.name);
+ if (scope.containsKey(decl.name) || current != null) {
+ Reporter.report(ErrorType.REDEFINITION, decl, current);
return null;
} else {
table.get(table.size() - 1).put(decl.name, decl);
@@ -63,83 +59,163 @@ public class IdentificationTable {
return scope.get(decl.name);
}
}
-
+
/**
- * Return nested scope corresponding to declaration (or null if non-existant).
+ * Return nested scope corresponding to declaration (or null if
+ * non-existant).
+ *
* @param decl
* @return
*/
public IdentificationTable getScope(Declaration decl) {
- if(scope.containsKey(decl.name)) {
+ if (scope.containsKey(decl.name)) {
return scope.get(decl.name);
}
-
+
return null;
}
-
+
/**
- * Iterates through all parents and tries to find the specified
- * declaration by name.
+ * Iterates through all parents and tries to find the specified declaration
+ * by name.
+ *
* @param name
* @return
*/
public Declaration getDeclaration(String name) {
IdentificationTable current = this;
- while(current != null) {
+ while (current != null) {
Declaration decl = current.getDeclarationAtScope(name);
- if(decl == null) {
- current = current.parent;
- } else {
- return decl;
- }
+ if (decl == null) current = current.parent;
+ else return decl;
}
-
+
return null;
}
-
+
/**
- * Iterates through levels (from higher to lower) for declaration,
- * returning none if it does not exist.
+ * Iterates through levels (from higher to lower) for declaration, returning
+ * none if it does not exist.
+ *
* @param name
* @return
*/
public Declaration getDeclarationAtScope(String name) {
- for(int i = table.size() - 1; i >= 0; i--) {
+ for (int i = table.size() - 1; i >= 0; i--) {
HashMap level = table.get(i);
- if(level.containsKey(name)) {
- return level.get(name);
- }
+ if (level.containsKey(name)) return level.get(name);
}
-
+
return null;
}
-
+
/**
* Add declaration to current table's table member.
+ *
* @param name
*/
public void setDeclarationAtScope(Declaration decl) {
- for(int i = 0; i < table.size(); i++) {
+ for (int i = 0; i < table.size(); i++) {
HashMap level = table.get(i);
- if(level.containsKey(decl.name)) {
- Reporter.report(ErrorType.REDEFINITION, decl);
+ if (level.containsKey(decl.name)) {
+ Declaration defined = level.get(decl.name);
+ Reporter.report(ErrorType.REDEFINITION, decl, defined);
return;
}
}
-
+
table.get(table.size() - 1).put(decl.name, decl);
}
-
+
/**
- * Checks that the passed class type does exist.
- * @param ct
+ * Checks whether the specified class has been declared.
+ *
+ * @param t
+ * @return
*/
- public void validateClassId(Type t) {
- if(t.typeKind == TypeKind.CLASS) {
+ public boolean classExists(Type t) {
+ if (t.typeKind == TypeKind.CLASS) {
ClassType ct = (ClassType) t;
- if(getDeclaration(ct.className.spelling) == null) {
- Reporter.report(ErrorType.MISSING_DECL, ct);
+ return getDeclaration(ct.className.spelling) != null;
+ }
+
+ return true;
+ }
+
+ /**
+ * Determines whether two types match.
+ *
+ * @param t1
+ * @param t2
+ * @return
+ */
+ public static boolean match(Type t1, Type t2) {
+ return IdentificationTable.match(t1, t2, false);
+ }
+
+ /**
+ * Determines whether two type match, reporting an error if they do not.
+ *
+ * @param t1
+ * @param t2
+ * @param report
+ * @return
+ */
+ public static boolean match(Type t1, Type t2, boolean report) {
+
+ if (t1.typeKind != t2.typeKind) {
+ if (report) Reporter.report(ErrorType.TYPE_MISMATCH, t1, t2);
+ return false;
+ }
+
+ // Check Class Types match
+ else if (t1.typeKind == TypeKind.CLASS) {
+ ClassType c1 = (ClassType) t1;
+ ClassType c2 = (ClassType) t2;
+ if (!c1.className.spelling.equals(c2.className.spelling)) {
+ if (report) Reporter.report(ErrorType.TYPE_MISMATCH, t1, t2);
+ return false;
}
}
+
+ // Check array types match
+ else if (t1.typeKind == TypeKind.ARRAY) {
+ ArrayType a1 = (ArrayType) t1;
+ ArrayType a2 = (ArrayType) t2;
+ if (!IdentificationTable.match(a1.eltType, a2.eltType)) {
+ if (report) Reporter.report(ErrorType.TYPE_MISMATCH, t1, t2);
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Determines if the passed method is a valid entry point for the
+ * compilation phase.
+ *
+ * @param md
+ * @return
+ */
+ public static boolean isMainMethod(MethodDecl md) {
+
+ // Check Declaration
+ if (!md.isPrivate && md.isStatic && md.type.typeKind == TypeKind.VOID
+ && md.name.equals("main") && md.parameterDeclList.size() == 1) {
+
+ // Check Parameter Declaration
+ ParameterDecl pd = md.parameterDeclList.get(0);
+
+ if (pd.type.typeKind != TypeKind.ARRAY) return false;
+ ArrayType at = (ArrayType) pd.type;
+
+ if (at.eltType.typeKind != TypeKind.CLASS) return false;
+ ClassType ct = (ClassType) at.eltType;
+
+ return ct.className.spelling.equals("String");
+ }
+
+ return false;
}
}
diff --git a/miniJava/src/miniJava/ContextualAnalyzer/Reporter.java b/miniJava/src/miniJava/ContextualAnalyzer/Reporter.java
index b1c21dc..818540c 100644
--- a/miniJava/src/miniJava/ContextualAnalyzer/Reporter.java
+++ b/miniJava/src/miniJava/ContextualAnalyzer/Reporter.java
@@ -3,120 +3,168 @@ package miniJava.ContextualAnalyzer;
import miniJava.AbstractSyntaxTrees.*;
enum ErrorType {
- VAR_COND_ONLY,
- MISSING_DECL,
- UNIDENTIFIED,
- REDEFINITION,
- INVALID_PARAM_COUNT,
- INVALID_INDEX,
- TYPE_MISMATCH,
- UNIDENTIFIED_TYPE,
- TYPE_CLASS_MISMATCH,
- TYPE_ARRAY_MISMATCH,
- NO_RETURN_EXPRESSION;
+ THIS,
+ NONFUNCTION_CALL,
+ UNDEFINED,
+ STATIC,
+ VISIBILITY,
+ NO_RETURN,
+ TYPE_MISMATCH,
+ REDEFINITION,
+ MAIN_UNDECLARED,
+ INVALID_PARAM_COUNT,
+ MULTIPLE_MAIN,
+ UNDECLARED_TYPE,
+ SINGLE_VARCOND,
+ INVALID_INDEX
}
public class Reporter {
-
+
public static boolean error = false;
-
+
/**
- * Prints out to console correct error message.
- * @param type
- * @param ast
+ * Convenience function for getting type names.
+ *
+ * @param t
+ * @return
*/
- public static void report(ErrorType type, AST ast) {
- error = true;
- switch(type) {
+ private static String getTypeName(Type t) {
+ if (t instanceof ClassType) {
+ ClassType ct = (ClassType) t;
+ return ct.className.spelling;
+ } else if (t instanceof ArrayType) {
+ ArrayType at = (ArrayType) t;
+ return getTypeName(at.eltType);
+ }
+
+ return t.typeKind.toString();
+ }
+
+ /**
+ * Convenience method for formatting error message.
+ *
+ * @param message
+ */
+ private static void emit(String message) {
+ System.out.println("***" + message);
+ }
+
+ /**
+ * Convenience function for managing all error types.
+ *
+ * @param type
+ * @param a1
+ * @param a2
+ */
+ public static void report(ErrorType type, AST a1, AST a2) {
+
+ switch (type) {
- // VarDeclStmt is only statement in conditional branch
- case VAR_COND_ONLY: {
- System.out.println("***Conditional statment cannot be followed by a variable declaration statement " + ast.posn);
+ // Cannot access 'this' in a static method
+ case THIS: {
+ MethodDecl md = (MethodDecl) a2;
+ emit("Cannot reference 'this' " + a1.posn + " in static method '" + md.name + "' " + md.posn);
break;
}
-
- // Declaration does not exist for reference
- case MISSING_DECL: {
- System.out.println("***Reference to a non-existant type " + ast.posn);
+
+ // Attempting to call a non function as a function
+ case NONFUNCTION_CALL: {
+ emit("Not a valid function call at " + a1.posn);
break;
}
-
- // Reports when a reference could not be found
- case UNIDENTIFIED: {
- System.out.println("***Reference refers to a declaration that does not exist " + ast.posn);
+
+ // Tried accessing a non-static member from a static method
+ case STATIC: {
+ MemberDecl md = (MemberDecl) a1;
+ Identifier ident = (Identifier) a2;
+ emit("'" + md.name + "' " + md.posn + " is an instance member and cannot be accessed at " + ident.posn);
break;
}
-
+
+ // Tried accessing a private member of a different class
+ case VISIBILITY: {
+ MemberDecl md = (MemberDecl) a1;
+ Identifier ident = (Identifier) a2;
+ emit("'" + md.name + "' " + md.posn + " is a private member and cannot be accessed at " + ident.posn);
+ break;
+ }
+
+ // Non-void function does not have a return statement
+ case NO_RETURN: {
+ MethodDecl md = (MethodDecl) a1;
+ emit("'" + md.name + "' " + md.posn + " must have a return statement");
+ break;
+ }
+
+ // The passed types are not the same
+ case TYPE_MISMATCH: {
+ String name1 = getTypeName((Type) a1);
+ String name2 = getTypeName((Type) a2);
+ if(a1 instanceof ArrayType) name1 += " Array";
+ if(a2 instanceof ArrayType) name2 += " Array";
+ emit("Expected type '" + name1 + "' but got '" + name2 + "' " + a2.posn);
+ break;
+ }
+
// Attempting to redeclare a variable
case REDEFINITION: {
- System.out.println("***Variable has already been defined earlier " + ast.posn);
+ emit("Variable at " + a1.posn + " already declared earlier at " + a2.posn);
break;
}
- // A non void function does not have a return statement
- case NO_RETURN_EXPRESSION: {
- System.out.println("***Non-void method does not have a return statement " + ast.posn);
+ // Identifier could not be found
+ case UNDEFINED: {
+ Identifier ident = (Identifier) a1;
+ emit("Identifier '" + ident.spelling + "' " + ident.posn + " is undeclared.");
break;
}
-
- // The number of parameters passed is either too few or too great
- case INVALID_PARAM_COUNT: {
- System.out.println("***The number of passed parameters does not equal expected count " + ast.posn);
- }
-
- // The expected expression MUST return an int (such as the index of an array)
- case INVALID_INDEX: {
- System.out.println("***Expected an integer value as the index of an array " + ast.posn);
- break;
- }
-
- // Hmmm.....
- case UNIDENTIFIED_TYPE: {
- System.out.println("***Unexpected type " + ast.posn);
- break;
- }
-
- // Clear Warning
- default:
- break;
- }
- }
- /**
- * Type specific error reporting.
- * @param type
- * @param t1
- * @param t2
- */
- public static void report(ErrorType type, Type t1, Type t2) {
- error = true;
- switch(type) {
-
- // Non class/array types don't match
- case TYPE_MISMATCH: {
- System.out.println("***Expected type " + t1.typeKind + " but got " + t2.typeKind + t2.posn);
+ // A public static void main(String[] args) method was not declared
+ case MAIN_UNDECLARED: {
+ emit("A main function was not declared");
break;
}
-
- // Two classes don't match
- case TYPE_CLASS_MISMATCH: {
- ClassType c1 = (ClassType) t1;
- ClassType c2 = (ClassType) t2;
- System.out.println("***Expected type " + c1.className.spelling + " but got " + c2.className.spelling + c2.posn);
+
+ // Parameter counts of an expression/statement do not match declaration
+ case INVALID_PARAM_COUNT: {
+ MethodDecl md = (MethodDecl) a2;
+ emit("Call to '" + md.name + "' " + a2.posn + " has an invalid parameter count at " + a1.posn);
break;
}
-
- // Two arrays don't match
- case TYPE_ARRAY_MISMATCH: {
- ArrayType a1 = (ArrayType) t1;
- ArrayType a2 = (ArrayType) t2;
- System.out.println("***Expected array type " + a1.eltType.typeKind + " but got " + a2.eltType.typeKind + t2.posn);
+
+ // A public static void main(String[] args) was declared more than once
+ case MULTIPLE_MAIN: {
+ emit("Main function at " + a1.posn + " already declared previously at " + a2.posn);
break;
}
-
- // Clear Warning
- default:
+
+ // A reference has been made to a non-existant type
+ case UNDECLARED_TYPE: {
+ if(a1 instanceof Type) {
+ String typeName = getTypeName((Type) a1);
+ emit("'" + typeName + "' " + a1.posn + " has not been declared previously");
+ } else {
+ emit("Identifier at " + a1.posn + " could not be identified");
+ }
+
break;
+ }
+
+ // A Variable Declaration Statement was made as the only statement of a condition
+ case SINGLE_VARCOND: {
+ emit("Conditional statment cannot be followed by a variable declaration statement exclusively " + a1.posn);
+ break;
+ }
+
+ // An indexed expression must be of an int type
+ case INVALID_INDEX: {
+ emit("Index expression is not of type int " + a1.posn);
+ break;
+ }
}
+
+ error = true;
}
+
}
diff --git a/miniJava/src/miniJava/Exceptions/ParsingException.java b/miniJava/src/miniJava/Exceptions/ParsingException.java
index 97e760e..f655358 100644
--- a/miniJava/src/miniJava/Exceptions/ParsingException.java
+++ b/miniJava/src/miniJava/Exceptions/ParsingException.java
@@ -5,13 +5,13 @@ import miniJava.SyntacticAnalyzer.Token;
public class ParsingException extends Exception {
private static final long serialVersionUID = 1L;
-
+
public ParsingException() {
super("Unidentified parsing error!");
}
public ParsingException(Token t) {
- super("Parsing error with " + t.toString());
- }
-
+ super("Parsing error with " + t.spelling + " at " + t.posn.toString());
+ }
+
}
diff --git a/miniJava/src/miniJava/Exceptions/ScanningException.java b/miniJava/src/miniJava/Exceptions/ScanningException.java
index 4bbfd3a..a24b2e2 100644
--- a/miniJava/src/miniJava/Exceptions/ScanningException.java
+++ b/miniJava/src/miniJava/Exceptions/ScanningException.java
@@ -1,11 +1,11 @@
package miniJava.Exceptions;
public class ScanningException extends Exception {
-
+
private static final long serialVersionUID = 1L;
public ScanningException(int col, int line) {
- super("Scanning error at Column: " + col + ", Line: " + line);
- }
-
+ super("Scanning error at Column: " + col + ", Line: " + line);
+ }
+
}
diff --git a/miniJava/src/miniJava/SyntacticAnalyzer/Parser.java b/miniJava/src/miniJava/SyntacticAnalyzer/Parser.java
index 686ef42..b06176b 100644
--- a/miniJava/src/miniJava/SyntacticAnalyzer/Parser.java
+++ b/miniJava/src/miniJava/SyntacticAnalyzer/Parser.java
@@ -7,320 +7,326 @@ import miniJava.AbstractSyntaxTrees.Package;
import miniJava.Exceptions.*;
public class Parser {
-
+
private Scanner scanner;
private LinkedList stream;
-
+
public Parser(Scanner scanner) {
this.scanner = scanner;
this.stream = new LinkedList();
}
-
+
/**
* Program ::= (ClassDeclaration)* eot
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
public Package parse() throws ParsingException, ScanningException {
ClassDeclList decls = new ClassDeclList();
- while(peek(1).type == Token.TYPE.CLASS) {
+ while (peek(1).type == Token.TYPE.CLASS) {
decls.add(parseClassDeclaration());
}
-
+
accept(Token.TYPE.EOT);
return new Package(decls, new SourcePosition(0, 0));
}
-
+
/**
- * ClassDeclaration ::=
- * class id {
- * (Declarators id (; | MethodDeclaration))*
- * }
+ * ClassDeclaration ::= class id { (Declarators id (; | MethodDeclaration))* }
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
- private ClassDecl parseClassDeclaration() throws ParsingException, ScanningException {
-
+ private ClassDecl parseClassDeclaration() throws ParsingException,
+ ScanningException {
+
// Class Header
Token classToken = accept(Token.TYPE.CLASS);
String cn = accept(Token.TYPE.ID).spelling;
accept(Token.TYPE.LBRACKET);
-
+
// Setup
FieldDeclList fdl = new FieldDeclList();
MethodDeclList mdl = new MethodDeclList();
-
+
// Class Body
- while(peek(1).type != Token.TYPE.RBRACKET) {
+ while (peek(1).type != Token.TYPE.RBRACKET) {
Declarators d = parseDeclarators();
String name = accept(Token.TYPE.ID).spelling;
- FieldDecl f = new FieldDecl(d.isPrivate, d.isStatic, d.mt, name, d.posn);
-
+ FieldDecl f = new FieldDecl(d.isPrivate, d.isStatic, d.mt, name,
+ d.posn);
+
// Field Declarations
- if(peek(1).type == Token.TYPE.SEMICOLON) {
+ if (peek(1).type == Token.TYPE.SEMICOLON) {
accept(Token.TYPE.SEMICOLON);
fdl.add(f);
- }
-
+ }
+
// Method Declarations
- else mdl.add(parseMethodDeclaration(f));
+ else
+ mdl.add(parseMethodDeclaration(f));
}
-
+
accept(Token.TYPE.RBRACKET);
return new ClassDecl(cn, fdl, mdl, classToken.posn);
}
-
+
/**
* Declarators ::= (public | private)? static? Type
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
- private Declarators parseDeclarators() throws ParsingException, ScanningException {
-
+ private Declarators parseDeclarators() throws ParsingException,
+ ScanningException {
+
// Visibility
SourcePosition start = null;
boolean isPrivate = false;
-
- if(peek(1).type == Token.TYPE.PUBLIC) {
+
+ if (peek(1).type == Token.TYPE.PUBLIC) {
start = accept(Token.TYPE.PUBLIC).posn;
- } else if(peek(1).type == Token.TYPE.PRIVATE) {
+ } else if (peek(1).type == Token.TYPE.PRIVATE) {
isPrivate = true;
- start = accept(Token.TYPE.PRIVATE).posn;
+ start = accept(Token.TYPE.PRIVATE).posn;
}
-
+
// Class Methods
boolean isStatic = false;
- if(peek(1).type == Token.TYPE.STATIC) {
+ if (peek(1).type == Token.TYPE.STATIC) {
isStatic = true;
- if(start == null) {
+ if (start == null) {
start = accept(Token.TYPE.STATIC).posn;
} else {
accept(Token.TYPE.STATIC);
}
}
-
+
Type t = parseType();
- if(start == null) {
+ if (start == null) {
start = t.posn;
}
-
+
return new Declarators(isPrivate, isStatic, t, start);
}
/**
- * * MethodDeclaration ::=
- * (ParameterList?) {
- * Statement* (return Expression ;)?
- * }
- * @param f describes the declaratory aspect of the method
+ * MethodDeclaration ::= (ParameterList?) { Statement* (return Expression;)? }
+ * @param f
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
- private MethodDecl parseMethodDeclaration(FieldDecl f) throws ParsingException, ScanningException {
-
+ private MethodDecl parseMethodDeclaration(FieldDecl f)
+ throws ParsingException, ScanningException {
+
// Method Header
accept(Token.TYPE.LPAREN);
-
+
// Parameter List
ParameterDeclList pdl = new ParameterDeclList();
- if(peek(1).type != Token.TYPE.RPAREN) pdl = parseParameterList();
-
+ if (peek(1).type != Token.TYPE.RPAREN)
+ pdl = parseParameterList();
+
accept(Token.TYPE.RPAREN);
accept(Token.TYPE.LBRACKET);
-
+
// Method Body
Expression re = null;
StatementList stl = new StatementList();
- while(peek(1).type != Token.TYPE.RBRACKET) {
-
- if(peek(1).type == Token.TYPE.RETURN) {
+ while (peek(1).type != Token.TYPE.RBRACKET) {
+
+ if (peek(1).type == Token.TYPE.RETURN) {
accept(Token.TYPE.RETURN);
re = parseExpression();
accept(Token.TYPE.SEMICOLON);
break;
}
-
+
stl.add(parseStatement());
}
-
+
accept(Token.TYPE.RBRACKET);
return new MethodDecl(f, pdl, stl, re, f.posn);
}
-
+
/**
* Type ::= boolean | void | int ([])? | id ([])?
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Type parseType() throws ParsingException, ScanningException {
-
+
SourcePosition posn = null;
-
- switch(peek(1).type) {
-
- case BOOLEAN:
- posn = accept(Token.TYPE.BOOLEAN).posn;
- return new BaseType(TypeKind.BOOLEAN, posn);
-
- case VOID:
- posn = accept(Token.TYPE.VOID).posn;
- return new BaseType(TypeKind.VOID, posn);
-
- case INT: {
- posn = accept(Token.TYPE.INT).posn;
- BaseType b = new BaseType(TypeKind.INT, posn);
-
- if(peek(1).type == Token.TYPE.LSQUARE) {
- accept(Token.TYPE.LSQUARE);
- accept(Token.TYPE.RSQUARE);
- return new ArrayType(b, posn);
- }
-
- return b;
+
+ switch (peek(1).type) {
+
+ case BOOLEAN:
+ posn = accept(Token.TYPE.BOOLEAN).posn;
+ return new BaseType(TypeKind.BOOLEAN, posn);
+
+ case VOID:
+ posn = accept(Token.TYPE.VOID).posn;
+ return new BaseType(TypeKind.VOID, posn);
+
+ case INT: {
+ posn = accept(Token.TYPE.INT).posn;
+ BaseType b = new BaseType(TypeKind.INT, posn);
+
+ if (peek(1).type == Token.TYPE.LSQUARE) {
+ accept(Token.TYPE.LSQUARE);
+ accept(Token.TYPE.RSQUARE);
+ return new ArrayType(b, posn);
}
-
- case ID: {
- Token id = accept(peek(1).type);
- Identifier i = new Identifier(id.spelling, id.posn);
- ClassType c = new ClassType(i, id.posn);
-
- if(peek(1).type == Token.TYPE.LSQUARE) {
- accept(Token.TYPE.LSQUARE);
- accept(Token.TYPE.RSQUARE);
- return new ArrayType(c, id.posn);
- }
-
- return c;
+
+ return b;
+ }
+
+ case ID: {
+ Token id = accept(peek(1).type);
+ Identifier i = new Identifier(id.spelling, id.posn);
+ ClassType c = new ClassType(i, id.posn);
+
+ if (peek(1).type == Token.TYPE.LSQUARE) {
+ accept(Token.TYPE.LSQUARE);
+ accept(Token.TYPE.RSQUARE);
+ return new ArrayType(c, id.posn);
}
-
- default:
- throw new ParsingException();
+
+ return c;
+ }
+
+ default:
+ throw new ParsingException();
}
}
-
+
/**
* ParameterList ::= Type id (, Type id)*
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private ParameterDeclList parseParameterList() throws ParsingException, ScanningException {
-
- ParameterDeclList decls = new ParameterDeclList();
-
+
+ ParameterDeclList decls = new ParameterDeclList();
+
// First Parameter
Type t = parseType();
Token id = accept(Token.TYPE.ID);
decls.add(new ParameterDecl(t, id.spelling, id.posn));
-
+
// Remainder of List
- while(peek(1).type == Token.TYPE.COMMA) {
+ while (peek(1).type == Token.TYPE.COMMA) {
accept(Token.TYPE.COMMA);
Type nextType = parseType();
Token nextId = accept(Token.TYPE.ID);
decls.add(new ParameterDecl(nextType, nextId.spelling, nextId.posn));
}
-
+
return decls;
}
-
+
/**
* ArgumentList ::= Expression (, Expression)*
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private ExprList parseArgumentList() throws ParsingException, ScanningException {
ExprList e = new ExprList();
e.add(parseExpression());
- while(peek(1).type == Token.TYPE.COMMA) {
+ while (peek(1).type == Token.TYPE.COMMA) {
accept(Token.TYPE.COMMA);
e.add(parseExpression());
}
-
+
return e;
}
-
+
/**
* Reference ::= BaseRef (. BaseRef)*
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Reference parseReference() throws ParsingException, ScanningException {
Reference r = parseBaseRef();
- while(peek(1).type == Token.TYPE.PERIOD) {
+ while (peek(1).type == Token.TYPE.PERIOD) {
accept(Token.TYPE.PERIOD);
Token tokenId = accept(Token.TYPE.ID);
Identifier id = new Identifier(tokenId.spelling, tokenId.posn);
r = new QualifiedRef(r, id, tokenId.posn);
-
- if(peek(1).type == Token.TYPE.LSQUARE) {
+
+ if (peek(1).type == Token.TYPE.LSQUARE) {
accept(Token.TYPE.LSQUARE);
Expression e = parseExpression();
accept(Token.TYPE.RSQUARE);
r = new IndexedRef(r, e, tokenId.posn);
}
- }
-
+ }
+
return r;
}
/**
* BaseRef ::= this | id ([ Expression])?
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Reference parseBaseRef() throws ParsingException, ScanningException {
-
- switch(peek(1).type) {
+
+ switch (peek(1).type) {
case THIS: {
Token thisToken = accept(Token.TYPE.THIS);
return new ThisRef(thisToken.posn);
}
-
+
// id ([ Expression])?
default: {
Token id = accept(Token.TYPE.ID);
Identifier i = new Identifier(id.spelling, id.posn);
IdRef r = new IdRef(i, id.posn);
-
- if(peek(1).type == Token.TYPE.LSQUARE) {
+
+ if (peek(1).type == Token.TYPE.LSQUARE) {
accept(Token.TYPE.LSQUARE);
Expression e = parseExpression();
accept(Token.TYPE.RSQUARE);
return new IndexedRef(r, e, id.posn);
}
-
+
return r;
}
}
}
-
+
/**
- * Statement ::=
- * {Statement*}
- * | Type id = Expression;
- * | Reference = Expression;
- * | Reference ( ArgumentList? );
- * | if (Expression) Statement (else Statement)?
- * | while (Expression) Statement
+ * Statement ::= {Statement*} | Type id = Expression; | Reference =
+ * Expression; | Reference ( ArgumentList? ); | if (Expression) Statement
+ * (else Statement)? | while (Expression) Statement
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Statement parseStatement() throws ParsingException, ScanningException {
-
- switch(peek(1).type) {
-
+
+ switch (peek(1).type) {
+
// { Statement* }
case LBRACKET: {
Token leftToken = accept(Token.TYPE.LBRACKET);
StatementList stl = new StatementList();
- while(peek(1).type != Token.TYPE.RBRACKET) {
+ while (peek(1).type != Token.TYPE.RBRACKET) {
stl.add(parseStatement());
}
accept(Token.TYPE.RBRACKET);
-
+
return new BlockStmt(stl, leftToken.posn);
}
-
+
// if (Expression) Statement (else Statement)?
case IF: {
Token ifToken = accept(Token.TYPE.IF);
@@ -328,15 +334,15 @@ public class Parser {
Expression e = parseExpression();
accept(Token.TYPE.RPAREN);
Statement s1 = parseStatement();
- if(peek(1).type == Token.TYPE.ELSE) {
+ if (peek(1).type == Token.TYPE.ELSE) {
accept(Token.TYPE.ELSE);
Statement s2 = parseStatement();
return new IfStmt(e, s1, s2, ifToken.posn);
}
-
+
return new IfStmt(e, s1, ifToken.posn);
}
-
+
// while (Expression) Statement
case WHILE: {
Token whileToken = accept(Token.TYPE.WHILE);
@@ -344,74 +350,75 @@ public class Parser {
Expression e = parseExpression();
accept(Token.TYPE.RPAREN);
Statement s = parseStatement();
-
+
return new WhileStmt(e, s, whileToken.posn);
}
-
+
// Type id = Expression ;
- case BOOLEAN: case VOID: case INT: case ID: {
-
+ case BOOLEAN:
+ case VOID:
+ case INT:
+ case ID: {
+
// Must be a type though there is a possibility of a reference
- if(peek(1).type != Token.TYPE.ID || peek(2).type == Token.TYPE.ID
- ||(peek(2).type == Token.TYPE.LSQUARE && peek(3).type == Token.TYPE.RSQUARE)) {
+ if (peek(1).type != Token.TYPE.ID
+ || peek(2).type == Token.TYPE.ID
+ || (peek(2).type == Token.TYPE.LSQUARE && peek(3).type == Token.TYPE.RSQUARE)) {
Type t = parseType();
String name = accept(Token.TYPE.ID).spelling;
VarDecl v = new VarDecl(t, name, t.posn);
-
+
accept(Token.TYPE.EQUALS);
Expression e = parseExpression();
accept(Token.TYPE.SEMICOLON);
-
+
return new VarDeclStmt(v, e, t.posn);
}
-
+
/* Fall Through */
}
-
+
default: {
Statement s = null;
Reference r = parseReference();
-
- // Reference ( ArgumentList? ) ;
- if(peek(1).type == Token.TYPE.LPAREN) {
+
+ // Reference ( ArgumentList? ) ;
+ if (peek(1).type == Token.TYPE.LPAREN) {
ExprList e = new ExprList();
accept(Token.TYPE.LPAREN);
- if(peek(1).type != Token.TYPE.RPAREN) {
+ if (peek(1).type != Token.TYPE.RPAREN) {
e = parseArgumentList();
}
accept(Token.TYPE.RPAREN);
s = new CallStmt(r, e, r.posn);
- }
-
+ }
+
// Reference = Expression ;
else {
accept(Token.TYPE.EQUALS);
Expression e = parseExpression();
s = new AssignStmt(r, e, r.posn);
}
-
+
accept(Token.TYPE.SEMICOLON);
return s;
}
}
}
-
+
/**
- * Expression ::=
- * Reference
- * | Reference ( ArgumentList? )
- * | unop Expression
- * | ( Expression )
- * | num | true | false
- * | new (id() | int [ Expression ] | id [ Expression ] )
+ * Expression ::= Reference | Reference ( ArgumentList? ) | unop Expression
+ * | ( Expression ) | num | true | false | new (id() | int [ Expression ] |
+ * id [ Expression ] )
+ *
* @return
* @throws ScanningException
*/
private Expression parseSingleExpression() throws ParsingException, ScanningException {
-
+
Expression e = null;
- switch(peek(1).type) {
-
+ switch (peek(1).type) {
+
// num
case NUM: {
Token number = accept(Token.TYPE.NUM);
@@ -419,7 +426,7 @@ public class Parser {
e = new LiteralExpr(i, number.posn);
break;
}
-
+
// true | false
case TRUE:
case FALSE: {
@@ -428,7 +435,7 @@ public class Parser {
e = new LiteralExpr(b, bool.posn);
break;
}
-
+
// ( Expression )
case LPAREN: {
accept(Token.TYPE.LPAREN);
@@ -436,39 +443,39 @@ public class Parser {
accept(Token.TYPE.RPAREN);
break;
}
-
+
// unop Expression
case UNOP:
case BINOP: {
- if(peek(1).spelling.equals("!") || peek(1).spelling.equals("-")) {
+ if (peek(1).spelling.equals("!") || peek(1).spelling.equals("-")) {
Token opToken = accept(peek(1).type);
Operator o = new Operator(opToken, opToken.posn);
e = new UnaryExpr(o, parseSingleExpression(), opToken.posn);
- }
- else throw new ParsingException();
+ } else
+ throw new ParsingException();
break;
}
-
- // new ( int [ Expression ] | id ( ) | id [ Expression ] )
+
+ // new ( int [ Expression ] | id ( ) | id [ Expression ] )
case NEW: {
Token newToken = accept(Token.TYPE.NEW);
-
- if(peek(1).type == Token.TYPE.INT) {
+
+ if (peek(1).type == Token.TYPE.INT) {
accept(Token.TYPE.INT);
accept(Token.TYPE.LSQUARE);
Expression e2 = parseExpression();
accept(Token.TYPE.RSQUARE);
-
+
BaseType b = new BaseType(TypeKind.INT, newToken.posn);
e = new NewArrayExpr(b, e2, newToken.posn);
}
-
+
else {
Token id = accept(Token.TYPE.ID);
Identifier i = new Identifier(id.spelling, id.posn);
ClassType c = new ClassType(i, id.posn);
-
- if(peek(1).type == Token.TYPE.LPAREN){
+
+ if (peek(1).type == Token.TYPE.LPAREN) {
accept(Token.TYPE.LPAREN);
accept(Token.TYPE.RPAREN);
e = new NewObjectExpr(c, id.posn);
@@ -479,17 +486,18 @@ public class Parser {
e = new NewArrayExpr(c, e2, id.posn);
}
}
-
+
break;
}
-
+
// Reference ((ArgumentList?))?
- case THIS: case ID: {
+ case THIS:
+ case ID: {
Reference r = parseReference();
- if(peek(1).type == Token.TYPE.LPAREN) {
+ if (peek(1).type == Token.TYPE.LPAREN) {
accept(Token.TYPE.LPAREN);
ExprList el = new ExprList();
- if(peek(1).type != Token.TYPE.RPAREN) {
+ if (peek(1).type != Token.TYPE.RPAREN) {
el = parseArgumentList();
}
accept(Token.TYPE.RPAREN);
@@ -497,147 +505,156 @@ public class Parser {
} else {
e = new RefExpr(r, r.posn);
}
-
+
break;
}
-
+
default:
- throw new ParsingException();
+ throw new ParsingException(peek(1));
}
-
+
return e;
}
/**
- * Disjunction & Initial Call:
- * Expression ::= Expression binop Expression
+ * Disjunction & Initial Call: Expression ::= Expression binop Expression
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Expression parseExpression() throws ParsingException, ScanningException {
-
+
Expression e = parseCExpression();
- while(peek(1).spelling.equals("||")) {
+ while (peek(1).spelling.equals("||")) {
Token opToken = accept(Token.TYPE.BINOP);
Operator o = new Operator(opToken, opToken.posn);
e = new BinaryExpr(o, e, parseCExpression(), e.posn);
}
-
+
return e;
}
-
+
/**
* Conjunction
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Expression parseCExpression() throws ParsingException, ScanningException {
Expression e = parseEExpression();
- while(peek(1).spelling.equals("&&")) {
+ while (peek(1).spelling.equals("&&")) {
Token opToken = accept(Token.TYPE.BINOP);
Operator o = new Operator(opToken, opToken.posn);
e = new BinaryExpr(o, e, parseEExpression(), e.posn);
}
-
+
return e;
}
-
+
/**
* Equality
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Expression parseEExpression() throws ParsingException, ScanningException {
-
+
Expression e = parseRExpression();
- while(peek(1).spelling.equals("==") || peek(1).spelling.equals("!=")) {
+ while (peek(1).spelling.equals("==") || peek(1).spelling.equals("!=")) {
Token opToken = accept(Token.TYPE.BINOP);
Operator o = new Operator(opToken, opToken.posn);
e = new BinaryExpr(o, e, parseRExpression(), e.posn);
}
-
+
return e;
}
-
+
/**
* Relational
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Expression parseRExpression() throws ParsingException, ScanningException {
-
+
Expression e = parseAExpression();
- while(peek(1).spelling.equals("<") || peek(1).spelling.equals("<=")
- || peek(1).spelling.equals(">") || peek(1).spelling.equals(">=")) {
+ while (peek(1).spelling.equals("<") || peek(1).spelling.equals("<=")
+ || peek(1).spelling.equals(">")
+ || peek(1).spelling.equals(">=")) {
Token opToken = accept(Token.TYPE.BINOP);
Operator o = new Operator(opToken, opToken.posn);
e = new BinaryExpr(o, e, parseAExpression(), e.posn);
}
-
+
return e;
}
-
+
/**
* Additive
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Expression parseAExpression() throws ParsingException, ScanningException {
-
+
Expression e = parseMExpression();
- while(peek(1).spelling.equals("+") || peek(1).spelling.equals("-")) {
+ while (peek(1).spelling.equals("+") || peek(1).spelling.equals("-")) {
Token opToken = accept(Token.TYPE.BINOP);
Operator o = new Operator(opToken, opToken.posn);
e = new BinaryExpr(o, e, parseMExpression(), e.posn);
}
-
+
return e;
}
-
+
/**
* Multiplicative
* @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Expression parseMExpression() throws ParsingException, ScanningException {
-
+
Expression e = parseSingleExpression();
- while(peek(1).spelling.equals("*") || peek(1).spelling.equals("/")) {
+ while (peek(1).spelling.equals("*") || peek(1).spelling.equals("/")) {
Token opToken = accept(Token.TYPE.BINOP);
Operator o = new Operator(opToken, opToken.posn);
e = new BinaryExpr(o, e, parseSingleExpression(), e.posn);
}
-
+
return e;
}
-
+
/**
* Sees what the next token is, caching the result.
+ * @param lookahead
* @return
* @throws ScanningException
*/
private Token peek(int lookahead) throws ScanningException {
-
+
// Cache tokens
- while(stream.size() < lookahead) {
+ while (stream.size() < lookahead) {
Token next = scanner.scan();
stream.addLast(next);
}
-
+
return stream.get(lookahead - 1);
}
-
-
+
/**
* Consumes token or throws exception.
+ * @param type
+ * @return
+ * @throws ParsingException
* @throws ScanningException
*/
private Token accept(Token.TYPE type) throws ParsingException, ScanningException {
Token next = peek(1);
if(next.type == type) stream.poll();
else throw new ParsingException(next);
-
+
return next;
}
}
diff --git a/miniJava/src/miniJava/SyntacticAnalyzer/Scanner.java b/miniJava/src/miniJava/SyntacticAnalyzer/Scanner.java
index 0c2aea9..3cd0aa8 100644
--- a/miniJava/src/miniJava/SyntacticAnalyzer/Scanner.java
+++ b/miniJava/src/miniJava/SyntacticAnalyzer/Scanner.java
@@ -4,293 +4,310 @@ import java.io.*;
import miniJava.Exceptions.*;
public class Scanner {
-
+
private int col = 1;
private int line = 1;
private BufferedReader input;
-
- public Scanner(BufferedReader input) {
- this.input = input;
- }
-
- public Scanner(String input) {
- StringReader reader = new StringReader(input);
- this.input = new BufferedReader(reader);
- }
-
- /**
- * Scans in input, returning next token.
- * @return
- * @throws IOException
- */
- public Token scan() throws ScanningException {
- String attr = "";
- Token token = null;
-
- while(token == null) {
-
- // Check for EOF
- int c = read();
- if(c == -1) return new Token("", Token.TYPE.EOT);
-
- // Setup
- attr += (char) c;
-
- switch(c) {
-
- // Operators
- case '*':
- token = new Token(attr, Token.TYPE.BINOP);
- break;
-
- case '+':
- if(peek('+')) throw new ScanningException(col, line);
- token = new Token(attr, Token.TYPE.BINOP);
- break;
-
- case '-':
- if(peek('-')) throw new ScanningException(col, line);
- token = new Token(attr, Token.TYPE.BINOP);
- break;
-
- // Check for comment
- case '/':
- if(peek('*')) { read(); readComment(); attr = ""; }
- else if(peek('/')) { readLine(); attr = ""; }
- else token = new Token(attr, Token.TYPE.BINOP);
- break;
-
- // Check for c or c=
- case '>':
- case '<':
- if(peek('=')) attr += (char) read();
- token = new Token(attr, Token.TYPE.BINOP);
- break;
-
- // Check for ! or !=
- case '!':
- if(!peek('=')) token = new Token(attr, Token.TYPE.UNOP);
- else {
- attr += (char) read();
- token = new Token(attr, Token.TYPE.BINOP);
- }
- break;
-
- // Check for && or ||
- case '&':
- case '|':
- if(!peek((char) c)) throw new ScanningException(col, line);
- else {
- attr += (char) read();
- token = new Token(attr, Token.TYPE.BINOP);
- }
- break;
-
- // Other Operators
- case '=':
- if(!peek('=')) token = new Token(attr, Token.TYPE.EQUALS);
- else {
- attr += (char) read();
- token = new Token(attr, Token.TYPE.BINOP);
- }
- break;
-
- case '.':
- token = new Token(attr, Token.TYPE.PERIOD);
- break;
-
- case ',':
- token = new Token(attr, Token.TYPE.COMMA);
- break;
-
- case '[':
- token = new Token(attr, Token.TYPE.LSQUARE);
- break;
-
- case ']':
- token = new Token(attr, Token.TYPE.RSQUARE);
- break;
-
- case '{':
- token = new Token(attr, Token.TYPE.LBRACKET);
- break;
-
- case '}':
- token = new Token(attr, Token.TYPE.RBRACKET);
- break;
-
- case '(':
- token = new Token(attr, Token.TYPE.LPAREN);
- break;
-
- case ')':
- token = new Token(attr, Token.TYPE.RPAREN);
- break;
-
- case ';':
- token = new Token(attr, Token.TYPE.SEMICOLON);
- break;
-
- default:
-
- // Identifier or Keyword
- if(isAlpha((char) c)) {
- for(char n = peek(); isAlpha(n) || isDigit(n);) {
- attr += (char) read();
- n = peek();
- }
-
- if(Token.keywords.containsKey(attr)) {
- token = new Token(attr, Token.keywords.get(attr));
- } else {
- token = new Token(attr, Token.TYPE.ID);
- }
- }
-
- // Number
- else if(isDigit((char) c)) {
- for(char n = peek(); isDigit(n);) {
- attr += (char) read();
- n = peek();
- }
-
- token = new Token(attr, Token.TYPE.NUM);
- }
-
- // Whitespace
- else if(isWhitespace((char) c)) {
- attr = "";
- }
-
- // Unrecognized Character
- else throw new ScanningException(col, line);;
-
- break;
- }
- }
+ public Scanner(BufferedReader input) {
+ this.input = input;
+ }
- token.posn = new SourcePosition(line, col - token.spelling.length());
- return token;
- }
-
-
- /**
- * Looks at next character in stream without consuming.
- * @return
- * @throws IOException
- */
- private char peek() throws ScanningException {
- try {
- input.mark(1);
- int next = input.read();
- input.reset();
-
- return next == -1 ? '\0' : (char) next;
- } catch(IOException e) {
- throw new ScanningException(col, line);
- }
- }
-
-
- /**
- * Returns whether passed character is next in stream.
- * @param c
- * @return
- * @throws IOException
- */
- private boolean peek(char c) throws ScanningException {
- try {
- input.mark(1);
- int next = input.read();
- input.reset();
-
- return c == next;
- } catch(IOException e) {
- throw new ScanningException(col, line);
- }
- }
-
-
- /**
- * Alternative reading that keeps track of position.
- * @return
- * @throws IOException
- */
- private int read() throws ScanningException {
- try {
- int next = input.read();
- if(next != '\n' && next != '\r') col += 1;
- else {
- col = 1; line += 1;
- if(peek('\r') || peek('\n')) next = input.read();
- }
-
- return next;
- } catch(IOException e) {
- throw new ScanningException(col, line);
- }
- }
-
-
- /**
- * Consumes input until an end of comment has been reached.
- * @throws IOException
- */
- private void readComment() throws ScanningException {
- char prev = '\0', current = '\0';
- while(prev != '*' || current != '/') {
-
- prev = current;
+ public Scanner(String input) {
+ StringReader reader = new StringReader(input);
+ this.input = new BufferedReader(reader);
+ }
- int next = read();
- if(next == -1) throw new ScanningException(col, line);
- else current = (char) next;
- }
- }
-
-
- /**
- * Consumes input until the end of line is reached
- * @throws IOException
- */
- private void readLine() throws ScanningException {
- for(int n = 0; n != '\n' && n != '\r' && n != -1; n = read()) {}
- }
-
-
- /**
- * Tells whether character is alphabetical.
- * @param c
- * @return
- */
- private boolean isAlpha(char c) {
- return (c >= 'a' && c <= 'z')
- || (c >= 'A' && c <= 'Z')
- || c == '_';
- }
-
-
- /**
- * Tells whether character is numerical.
- * @param c
- * @return
- */
- private boolean isDigit(char c) {
- return c >= '0' && c <= '9';
- }
-
-
- /**
- * Tells wheter character is whitespace.
- * @param c
- * @return
- */
- private boolean isWhitespace(char c) {
- return c == ' '
- || c == '\n'
- || c == '\r'
- || c == '\t';
- }
+ /**
+ * Scans in input, returning next token.
+ *
+ * @return
+ * @throws IOException
+ */
+ public Token scan() throws ScanningException {
+
+ String attr = "";
+ Token token = null;
+
+ while (token == null) {
+
+ // Check for EOF
+ int c = read();
+ if (c == -1)
+ return new Token("", Token.TYPE.EOT);
+
+ // Setup
+ attr += (char) c;
+
+ switch (c) {
+
+ // Operators
+ case '*':
+ token = new Token(attr, Token.TYPE.BINOP);
+ break;
+
+ case '+':
+ if (peek('+'))
+ throw new ScanningException(col, line);
+ token = new Token(attr, Token.TYPE.BINOP);
+ break;
+
+ case '-':
+ if (peek('-'))
+ throw new ScanningException(col, line);
+ token = new Token(attr, Token.TYPE.BINOP);
+ break;
+
+ // Check for comment
+ case '/':
+ if (peek('*')) {
+ read();
+ readComment();
+ attr = "";
+ } else if (peek('/')) {
+ readLine();
+ attr = "";
+ } else
+ token = new Token(attr, Token.TYPE.BINOP);
+ break;
+
+ // Check for c or c=
+ case '>':
+ case '<':
+ if (peek('='))
+ attr += (char) read();
+ token = new Token(attr, Token.TYPE.BINOP);
+ break;
+
+ // Check for ! or !=
+ case '!':
+ if (!peek('='))
+ token = new Token(attr, Token.TYPE.UNOP);
+ else {
+ attr += (char) read();
+ token = new Token(attr, Token.TYPE.BINOP);
+ }
+ break;
+
+ // Check for && or ||
+ case '&':
+ case '|':
+ if (!peek((char) c))
+ throw new ScanningException(col, line);
+ else {
+ attr += (char) read();
+ token = new Token(attr, Token.TYPE.BINOP);
+ }
+ break;
+
+ // Other Operators
+ case '=':
+ if (!peek('='))
+ token = new Token(attr, Token.TYPE.EQUALS);
+ else {
+ attr += (char) read();
+ token = new Token(attr, Token.TYPE.BINOP);
+ }
+ break;
+
+ case '.':
+ token = new Token(attr, Token.TYPE.PERIOD);
+ break;
+
+ case ',':
+ token = new Token(attr, Token.TYPE.COMMA);
+ break;
+
+ case '[':
+ token = new Token(attr, Token.TYPE.LSQUARE);
+ break;
+
+ case ']':
+ token = new Token(attr, Token.TYPE.RSQUARE);
+ break;
+
+ case '{':
+ token = new Token(attr, Token.TYPE.LBRACKET);
+ break;
+
+ case '}':
+ token = new Token(attr, Token.TYPE.RBRACKET);
+ break;
+
+ case '(':
+ token = new Token(attr, Token.TYPE.LPAREN);
+ break;
+
+ case ')':
+ token = new Token(attr, Token.TYPE.RPAREN);
+ break;
+
+ case ';':
+ token = new Token(attr, Token.TYPE.SEMICOLON);
+ break;
+
+ default:
+
+ // Identifier or Keyword
+ if (isAlpha((char) c)) {
+ for (char n = peek(); isAlpha(n) || isDigit(n);) {
+ attr += (char) read();
+ n = peek();
+ }
+
+ if (Token.keywords.containsKey(attr)) {
+ token = new Token(attr, Token.keywords.get(attr));
+ } else {
+ token = new Token(attr, Token.TYPE.ID);
+ }
+ }
+
+ // Number
+ else if (isDigit((char) c)) {
+ for (char n = peek(); isDigit(n);) {
+ attr += (char) read();
+ n = peek();
+ }
+
+ token = new Token(attr, Token.TYPE.NUM);
+ }
+
+ // Whitespace
+ else if (isWhitespace((char) c)) {
+ attr = "";
+ }
+
+ // Unrecognized Character
+ else
+ throw new ScanningException(col, line);
+ ;
+
+ break;
+ }
+ }
+
+ token.posn = new SourcePosition(line, col - token.spelling.length());
+ return token;
+ }
+
+ /**
+ * Looks at next character in stream without consuming.
+ *
+ * @return
+ * @throws IOException
+ */
+ private char peek() throws ScanningException {
+ try {
+ input.mark(1);
+ int next = input.read();
+ input.reset();
+
+ return next == -1 ? '\0' : (char) next;
+ } catch (IOException e) {
+ throw new ScanningException(col, line);
+ }
+ }
+
+ /**
+ * Returns whether passed character is next in stream.
+ *
+ * @param c
+ * @return
+ * @throws IOException
+ */
+ private boolean peek(char c) throws ScanningException {
+ try {
+ input.mark(1);
+ int next = input.read();
+ input.reset();
+
+ return c == next;
+ } catch (IOException e) {
+ throw new ScanningException(col, line);
+ }
+ }
+
+ /**
+ * Alternative reading that keeps track of position.
+ *
+ * @return
+ * @throws IOException
+ */
+ private int read() throws ScanningException {
+ try {
+ int next = input.read();
+ if (next != '\n' && next != '\r')
+ col += 1;
+ else {
+ col = 1;
+ line += 1;
+ if (peek('\r') || peek('\n'))
+ next = input.read();
+ }
+
+ return next;
+ } catch (IOException e) {
+ throw new ScanningException(col, line);
+ }
+ }
+
+ /**
+ * Consumes input until an end of comment has been reached.
+ *
+ * @throws IOException
+ */
+ private void readComment() throws ScanningException {
+ char prev = '\0', current = '\0';
+ while (prev != '*' || current != '/') {
+
+ prev = current;
+
+ int next = read();
+ if (next == -1)
+ throw new ScanningException(col, line);
+ else
+ current = (char) next;
+ }
+ }
+
+ /**
+ * Consumes input until the end of line is reached
+ *
+ * @throws IOException
+ */
+ private void readLine() throws ScanningException {
+ for (int n = 0; n != '\n' && n != '\r' && n != -1; n = read()) {
+ }
+ }
+
+ /**
+ * Tells whether character is alphabetical.
+ *
+ * @param c
+ * @return
+ */
+ private boolean isAlpha(char c) {
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
+ }
+
+ /**
+ * Tells whether character is numerical.
+ *
+ * @param c
+ * @return
+ */
+ private boolean isDigit(char c) {
+ return c >= '0' && c <= '9';
+ }
+
+ /**
+ * Tells wheter character is whitespace.
+ *
+ * @param c
+ * @return
+ */
+ private boolean isWhitespace(char c) {
+ return c == ' ' || c == '\n' || c == '\r' || c == '\t';
+ }
}
diff --git a/miniJava/src/miniJava/SyntacticAnalyzer/SourcePosition.java b/miniJava/src/miniJava/SyntacticAnalyzer/SourcePosition.java
index 3855ab2..272acc4 100644
--- a/miniJava/src/miniJava/SyntacticAnalyzer/SourcePosition.java
+++ b/miniJava/src/miniJava/SyntacticAnalyzer/SourcePosition.java
@@ -4,12 +4,12 @@ public class SourcePosition {
public final int col;
public final int line;
-
+
public SourcePosition(int line, int col) {
this.col = col;
this.line = line;
}
-
+
@Override
public String toString() {
return "(Line: " + line + ", Column: " + col + ")";
diff --git a/miniJava/src/miniJava/SyntacticAnalyzer/Token.java b/miniJava/src/miniJava/SyntacticAnalyzer/Token.java
index 519dc58..08f5abb 100644
--- a/miniJava/src/miniJava/SyntacticAnalyzer/Token.java
+++ b/miniJava/src/miniJava/SyntacticAnalyzer/Token.java
@@ -3,75 +3,49 @@ package miniJava.SyntacticAnalyzer;
import java.util.HashMap;
public class Token {
-
+
public enum TYPE {
-
+
// Possible Terminals
- ID,
- NUM,
- UNOP,
- BINOP,
-
+ ID, NUM, UNOP, BINOP,
+
// Keywords
- IF,
- ELSE,
- NEW,
- INT,
- VOID,
- THIS,
- TRUE,
- FALSE,
- CLASS,
- WHILE,
- RETURN,
- BOOLEAN,
-
+ IF, ELSE, NEW, INT, VOID, THIS, TRUE, FALSE, CLASS, WHILE, RETURN, BOOLEAN,
+
// Declarators
- STATIC,
- PUBLIC,
- PRIVATE,
-
+ STATIC, PUBLIC, PRIVATE,
+
// Other Terminals
- EQUALS,
- PERIOD,
- COMMA,
- LPAREN,
- RPAREN,
- LSQUARE,
- RSQUARE,
- LBRACKET,
- RBRACKET,
- SEMICOLON,
-
+ EQUALS, PERIOD, COMMA, LPAREN, RPAREN, LSQUARE, RSQUARE, LBRACKET, RBRACKET, SEMICOLON,
+
// End of Token Stream
EOT
};
-
- public final static HashMap keywords;
- static
- {
- keywords = new HashMap();
- keywords.put("class", TYPE.CLASS);
- keywords.put("return", TYPE.RETURN);
- keywords.put("public", TYPE.PUBLIC);
- keywords.put("private", TYPE.PRIVATE);
- keywords.put("static", TYPE.STATIC);
- keywords.put("int", TYPE.INT);
- keywords.put("boolean", TYPE.BOOLEAN);
- keywords.put("void", TYPE.VOID);
- keywords.put("this", TYPE.THIS);
- keywords.put("if", TYPE.IF);
- keywords.put("else", TYPE.ELSE);
- keywords.put("while", TYPE.WHILE);
- keywords.put("true", TYPE.TRUE);
- keywords.put("false", TYPE.FALSE);
- keywords.put("new", TYPE.NEW);
- }
-
+
+ public final static HashMap keywords;
+ static {
+ keywords = new HashMap();
+ keywords.put("class", TYPE.CLASS);
+ keywords.put("return", TYPE.RETURN);
+ keywords.put("public", TYPE.PUBLIC);
+ keywords.put("private", TYPE.PRIVATE);
+ keywords.put("static", TYPE.STATIC);
+ keywords.put("int", TYPE.INT);
+ keywords.put("boolean", TYPE.BOOLEAN);
+ keywords.put("void", TYPE.VOID);
+ keywords.put("this", TYPE.THIS);
+ keywords.put("if", TYPE.IF);
+ keywords.put("else", TYPE.ELSE);
+ keywords.put("while", TYPE.WHILE);
+ keywords.put("true", TYPE.TRUE);
+ keywords.put("false", TYPE.FALSE);
+ keywords.put("new", TYPE.NEW);
+ }
+
public final TYPE type;
public SourcePosition posn;
public final String spelling;
-
+
public Token(String spelling, TYPE type) {
this.type = type;
this.posn = null;
diff --git a/miniJava/test.java b/miniJava/test.java
index cf43519..e69de29 100644
--- a/miniJava/test.java
+++ b/miniJava/test.java
@@ -1,5 +0,0 @@
-class Test {
- public int Test() {
-
- }
-}
\ No newline at end of file