1
Fork 0

Type Checking

Completed type checking as well as formatted files.
master
Joshua Potter 2014-03-22 21:01:40 -04:00
parent 83c72019f5
commit 3517e9648c
61 changed files with 2046 additions and 1807 deletions

View File

@ -22,7 +22,9 @@ public class ASTDisplay implements Visitor<String,Object> {
/** /**
* print text representation of AST to stdout * print text representation of AST to stdout
* @param ast root node of AST *
* @param ast
* root node of AST
*/ */
public void showTree(AST ast) { public void showTree(AST ast) {
System.out.println("======= AST Display ========================="); System.out.println("======= AST Display =========================");
@ -34,8 +36,11 @@ public class ASTDisplay implements Visitor<String,Object> {
/** /**
* display arbitrary text for a node * display arbitrary text for a node
* @param prefix spacing to indicate depth in AST *
* @param text preformatted node display * @param prefix
* spacing to indicate depth in AST
* @param text
* preformatted node display
*/ */
private void show(String prefix, String text) { private void show(String prefix, String text) {
System.out.println(prefix + text); System.out.println(prefix + text);
@ -43,8 +48,11 @@ public class ASTDisplay implements Visitor<String,Object> {
/** /**
* display AST node by name * display AST node by name
* @param prefix spacing to indicate depth in AST *
* @param node AST node, will be shown 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) { private void show(String prefix, AST node) {
System.out.println(prefix + node.toString()); System.out.println(prefix + node.toString());
@ -52,7 +60,9 @@ public class ASTDisplay implements Visitor<String,Object> {
/** /**
* quote a string * quote a string
* @param text string to quote *
* @param text
* string to quote
*/ */
private String quote(String text) { private String quote(String text) {
return ("\"" + text + "\""); return ("\"" + text + "\"");
@ -60,14 +70,15 @@ public class ASTDisplay implements Visitor<String,Object> {
/** /**
* increase depth in AST * increase depth in AST
* @param prefix current spacing to indicate depth in AST *
* @param prefix
* current spacing to indicate depth in AST
* @return new spacing * @return new spacing
*/ */
private String indent(String prefix) { private String indent(String prefix) {
return prefix + " "; return prefix + " ";
} }
// ///////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////
// //
// PACKAGE // PACKAGE
@ -85,7 +96,6 @@ public class ASTDisplay implements Visitor<String,Object> {
return null; return null;
} }
// ///////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////
// //
// DECLARATIONS // DECLARATIONS
@ -149,7 +159,6 @@ public class ASTDisplay implements Visitor<String,Object> {
return null; return null;
} }
// ///////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////
// //
// TYPES // TYPES
@ -173,7 +182,6 @@ public class ASTDisplay implements Visitor<String,Object> {
return null; return null;
} }
// ///////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////
// //
// STATEMENTS // STATEMENTS
@ -233,7 +241,6 @@ public class ASTDisplay implements Visitor<String,Object> {
return null; return null;
} }
// ///////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////
// //
// EXPRESSIONS // EXPRESSIONS
@ -292,7 +299,6 @@ public class ASTDisplay implements Visitor<String,Object> {
return null; return null;
} }
// ///////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////
// //
// REFERENCES // REFERENCES
@ -324,7 +330,6 @@ public class ASTDisplay implements Visitor<String,Object> {
return null; return null;
} }
// ///////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////
// //
// TERMINALS // TERMINALS

View File

@ -21,4 +21,3 @@ public class ArrayType extends Type {
public Type eltType; public Type eltType;
} }

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class AssignStmt extends Statement public class AssignStmt extends Statement {
{
public AssignStmt(Reference r, Expression e, SourcePosition posn) { public AssignStmt(Reference r, Expression e, SourcePosition posn) {
super(posn); super(posn);
ref = r; ref = r;

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class BaseType extends Type public class BaseType extends Type {
{
public BaseType(TypeKind t, SourcePosition posn) { public BaseType(TypeKind t, SourcePosition posn) {
super(t, posn); super(t, posn);
} }

View File

@ -7,9 +7,9 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class BinaryExpr extends Expression public class BinaryExpr extends Expression {
{ public BinaryExpr(Operator o, Expression e1, Expression e2,
public BinaryExpr(Operator o, Expression e1, Expression e2, SourcePosition posn){ SourcePosition posn) {
super(posn); super(posn);
operator = o; operator = o;
left = e1; left = e1;

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class BlockStmt extends Statement public class BlockStmt extends Statement {
{
public BlockStmt(StatementList sl, SourcePosition posn) { public BlockStmt(StatementList sl, SourcePosition posn) {
super(posn); super(posn);
this.sl = sl; this.sl = sl;

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class CallExpr extends Expression public class CallExpr extends Expression {
{
public CallExpr(Reference f, ExprList el, SourcePosition posn) { public CallExpr(Reference f, ExprList el, SourcePosition posn) {
super(posn); super(posn);
functionRef = f; functionRef = f;

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class CallStmt extends Statement public class CallStmt extends Statement {
{
public CallStmt(Reference m, ExprList el, SourcePosition posn) { public CallStmt(Reference m, ExprList el, SourcePosition posn) {
super(posn); super(posn);
methodRef = m; methodRef = m;

View File

@ -13,6 +13,9 @@ public class ClassDecl extends Declaration {
super(cn, null, posn); super(cn, null, posn);
fieldDeclList = fdl; fieldDeclList = fdl;
methodDeclList = mdl; methodDeclList = mdl;
Identifier ident = new Identifier(cn, posn);
type = new ClassType(ident, posn);
} }
public <A, R> R visit(Visitor<A, R> v, A o) { public <A, R> R visit(Visitor<A, R> v, A o) {

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*; import java.util.*;
public class ClassDeclList implements Iterable<ClassDecl> public class ClassDeclList implements Iterable<ClassDecl> {
{
public ClassDeclList() { public ClassDeclList() {
classDeclList = new ArrayList<ClassDecl>(); classDeclList = new ArrayList<ClassDecl>();
} }
@ -31,4 +30,3 @@ public class ClassDeclList implements Iterable<ClassDecl>
private List<ClassDecl> classDeclList; private List<ClassDecl> classDeclList;
} }

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class ClassType extends Type public class ClassType extends Type {
{
public ClassType(Identifier cn, SourcePosition posn) { public ClassType(Identifier cn, SourcePosition posn) {
super(TypeKind.CLASS, posn); super(TypeKind.CLASS, posn);
className = cn; className = cn;

View File

@ -18,7 +18,8 @@ public abstract class Declaration extends AST {
@Override @Override
public String toString() { public String toString() {
if (posn != null) { if (posn != null) {
return this.name + "(Line: " + posn.line + ", Column: " + posn.col + ")"; return this.name + "(Line: " + posn.line + ", Column: " + posn.col
+ ")";
} else { } else {
return super.toString(); return super.toString();
} }

View File

@ -4,7 +4,8 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
public class Declarators { 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.isPrivate = isPrivate;
this.isStatic = isStatic; this.isStatic = isStatic;
this.mt = mt; this.mt = mt;

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*; import java.util.*;
public class ExprList implements Iterable<Expression> public class ExprList implements Iterable<Expression> {
{
public ExprList() { public ExprList() {
elist = new ArrayList<Expression>(); elist = new ArrayList<Expression>();
} }

View File

@ -9,7 +9,8 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
public class FieldDecl extends MemberDecl { public class FieldDecl extends MemberDecl {
public FieldDecl(boolean isPrivate, boolean isStatic, Type t, String name, SourcePosition posn){ public FieldDecl(boolean isPrivate, boolean isStatic, Type t, String name,
SourcePosition posn) {
super(isPrivate, isStatic, t, name, posn); super(isPrivate, isStatic, t, name, posn);
} }
@ -21,4 +22,3 @@ public class FieldDecl extends MemberDecl {
return v.visitFieldDecl(this, o); return v.visitFieldDecl(this, o);
} }
} }

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*; import java.util.*;
public class FieldDeclList implements Iterable<FieldDecl> public class FieldDeclList implements Iterable<FieldDecl> {
{
public FieldDeclList() { public FieldDeclList() {
fieldDeclList = new ArrayList<FieldDecl>(); fieldDeclList = new ArrayList<FieldDecl>();
} }
@ -36,4 +35,3 @@ public class FieldDeclList implements Iterable<FieldDecl>
private List<FieldDecl> fieldDeclList; private List<FieldDecl> fieldDeclList;
} }

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class IfStmt extends Statement public class IfStmt extends Statement {
{
public IfStmt(Expression b, Statement t, Statement e, SourcePosition posn) { public IfStmt(Expression b, Statement t, Statement e, SourcePosition posn) {
super(posn); super(posn);
cond = b; cond = b;

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class LiteralExpr extends Expression public class LiteralExpr extends Expression {
{
public LiteralExpr(Literal c, SourcePosition posn) { public LiteralExpr(Literal c, SourcePosition posn) {
super(posn); super(posn);
literal = c; literal = c;

View File

@ -9,7 +9,8 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
abstract public class MemberDecl extends Declaration { abstract public class MemberDecl extends Declaration {
public MemberDecl(boolean isPrivate, boolean isStatic, Type mt, String name, SourcePosition posn) { public MemberDecl(boolean isPrivate, boolean isStatic, Type mt,
String name, SourcePosition posn) {
super(name, mt, posn); super(name, mt, posn);
this.isPrivate = isPrivate; this.isPrivate = isPrivate;
this.isStatic = isStatic; this.isStatic = isStatic;

View File

@ -9,7 +9,8 @@ import miniJava.SyntacticAnalyzer.SourcePosition;
public class MethodDecl extends MemberDecl { public class MethodDecl extends MemberDecl {
public MethodDecl(MemberDecl md, ParameterDeclList pl, StatementList sl, Expression e, SourcePosition posn){ public MethodDecl(MemberDecl md, ParameterDeclList pl, StatementList sl,
Expression e, SourcePosition posn) {
super(md, posn); super(md, posn);
parameterDeclList = pl; parameterDeclList = pl;
statementList = sl; statementList = sl;

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*; import java.util.*;
public class MethodDeclList implements Iterable<MethodDecl> public class MethodDeclList implements Iterable<MethodDecl> {
{
public MethodDeclList() { public MethodDeclList() {
methodDeclList = new ArrayList<MethodDecl>(); methodDeclList = new ArrayList<MethodDecl>();
} }
@ -36,4 +35,3 @@ public class MethodDeclList implements Iterable<MethodDecl>
private List<MethodDecl> methodDeclList; private List<MethodDecl> methodDeclList;
} }

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class NewArrayExpr extends NewExpr public class NewArrayExpr extends NewExpr {
{
public NewArrayExpr(Type et, Expression e, SourcePosition posn) { public NewArrayExpr(Type et, Expression e, SourcePosition posn) {
super(posn); super(posn);
eltType = et; eltType = et;

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class NewObjectExpr extends NewExpr public class NewObjectExpr extends NewExpr {
{
public NewObjectExpr(ClassType ct, SourcePosition posn) { public NewObjectExpr(ClassType ct, SourcePosition posn) {
super(posn); super(posn);
classtype = ct; classtype = ct;

View File

@ -12,6 +12,7 @@ public class Operator extends Terminal {
public Operator(Token t, SourcePosition posn) { public Operator(Token t, SourcePosition posn) {
super(t.spelling, posn); super(t.spelling, posn);
token = t;
} }
public <A, R> R visit(Visitor<A, R> v, A o) { public <A, R> R visit(Visitor<A, R> v, A o) {

View File

@ -17,4 +17,3 @@ public class ParameterDecl extends LocalDecl {
return v.visitParameterDecl(this, o); return v.visitParameterDecl(this, o);
} }
} }

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*; import java.util.*;
public class ParameterDeclList implements Iterable<ParameterDecl> public class ParameterDeclList implements Iterable<ParameterDecl> {
{
public ParameterDeclList() { public ParameterDeclList() {
parameterDeclList = new ArrayList<ParameterDecl>(); parameterDeclList = new ArrayList<ParameterDecl>();
} }

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class RefExpr extends Expression public class RefExpr extends Expression {
{
public RefExpr(Reference r, SourcePosition posn) { public RefExpr(Reference r, SourcePosition posn) {
super(posn); super(posn);
ref = r; ref = r;

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Reference extends AST public abstract class Reference extends AST {
{
public Reference(SourcePosition posn) { public Reference(SourcePosition posn) {
super(posn); super(posn);
} }

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import java.util.*; import java.util.*;
public class StatementList implements Iterable<Statement> public class StatementList implements Iterable<Statement> {
{
public StatementList() { public StatementList() {
slist = new ArrayList<Statement>(); slist = new ArrayList<Statement>();
} }

View File

@ -17,5 +17,3 @@ abstract public class Type extends AST {
public TypeKind typeKind; public TypeKind typeKind;
} }

View File

@ -6,12 +6,5 @@
package miniJava.AbstractSyntaxTrees; package miniJava.AbstractSyntaxTrees;
public enum TypeKind { public enum TypeKind {
VOID, VOID, INT, BOOLEAN, CLASS, ARRAY, UNSUPPORTED, ERROR, EQUALS, RELATIONAL;
INT,
BOOLEAN,
CLASS,
ARRAY,
UNSUPPORTED,
ERROR,
VALID;
} }

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class UnaryExpr extends Expression public class UnaryExpr extends Expression {
{
public UnaryExpr(Operator o, Expression e, SourcePosition posn) { public UnaryExpr(Operator o, Expression e, SourcePosition posn) {
super(posn); super(posn);
operator = o; operator = o;

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class VarDeclStmt extends Statement public class VarDeclStmt extends Statement {
{
public VarDeclStmt(VarDecl vd, Expression e, SourcePosition posn) { public VarDeclStmt(VarDecl vd, Expression e, SourcePosition posn) {
super(posn); super(posn);
varDecl = vd; varDecl = vd;

View File

@ -6,8 +6,8 @@
package miniJava.AbstractSyntaxTrees; package miniJava.AbstractSyntaxTrees;
/** /**
* An implementation of the Visitor interface provides a method visitX * An implementation of the Visitor interface provides a method visitX for each
* for each non-abstract AST class X. * non-abstract AST class X.
*/ */
public interface Visitor<ArgType, ResultType> { public interface Visitor<ArgType, ResultType> {
@ -16,42 +16,65 @@ public interface Visitor<ArgType,ResultType> {
// Declarations // Declarations
public ResultType visitClassDecl(ClassDecl cd, ArgType arg); public ResultType visitClassDecl(ClassDecl cd, ArgType arg);
public ResultType visitFieldDecl(FieldDecl fd, ArgType arg); public ResultType visitFieldDecl(FieldDecl fd, ArgType arg);
public ResultType visitMethodDecl(MethodDecl md, ArgType arg); public ResultType visitMethodDecl(MethodDecl md, ArgType arg);
public ResultType visitParameterDecl(ParameterDecl pd, ArgType arg); public ResultType visitParameterDecl(ParameterDecl pd, ArgType arg);
public ResultType visitVarDecl(VarDecl decl, ArgType arg); public ResultType visitVarDecl(VarDecl decl, ArgType arg);
// Types // Types
public ResultType visitBaseType(BaseType type, ArgType arg); public ResultType visitBaseType(BaseType type, ArgType arg);
public ResultType visitClassType(ClassType type, ArgType arg); public ResultType visitClassType(ClassType type, ArgType arg);
public ResultType visitArrayType(ArrayType type, ArgType arg); public ResultType visitArrayType(ArrayType type, ArgType arg);
// Statements // Statements
public ResultType visitBlockStmt(BlockStmt stmt, ArgType arg); public ResultType visitBlockStmt(BlockStmt stmt, ArgType arg);
public ResultType visitVardeclStmt(VarDeclStmt stmt, ArgType arg); public ResultType visitVardeclStmt(VarDeclStmt stmt, ArgType arg);
public ResultType visitAssignStmt(AssignStmt stmt, ArgType arg); public ResultType visitAssignStmt(AssignStmt stmt, ArgType arg);
public ResultType visitCallStmt(CallStmt stmt, ArgType arg); public ResultType visitCallStmt(CallStmt stmt, ArgType arg);
public ResultType visitIfStmt(IfStmt stmt, ArgType arg); public ResultType visitIfStmt(IfStmt stmt, ArgType arg);
public ResultType visitWhileStmt(WhileStmt stmt, ArgType arg); public ResultType visitWhileStmt(WhileStmt stmt, ArgType arg);
// Expressions // Expressions
public ResultType visitUnaryExpr(UnaryExpr expr, ArgType arg); public ResultType visitUnaryExpr(UnaryExpr expr, ArgType arg);
public ResultType visitBinaryExpr(BinaryExpr expr, ArgType arg); public ResultType visitBinaryExpr(BinaryExpr expr, ArgType arg);
public ResultType visitRefExpr(RefExpr expr, ArgType arg); public ResultType visitRefExpr(RefExpr expr, ArgType arg);
public ResultType visitCallExpr(CallExpr expr, ArgType arg); public ResultType visitCallExpr(CallExpr expr, ArgType arg);
public ResultType visitLiteralExpr(LiteralExpr expr, ArgType arg); public ResultType visitLiteralExpr(LiteralExpr expr, ArgType arg);
public ResultType visitNewObjectExpr(NewObjectExpr expr, ArgType arg); public ResultType visitNewObjectExpr(NewObjectExpr expr, ArgType arg);
public ResultType visitNewArrayExpr(NewArrayExpr expr, ArgType arg); public ResultType visitNewArrayExpr(NewArrayExpr expr, ArgType arg);
// References // References
public ResultType visitQualifiedRef(QualifiedRef ref, ArgType arg); public ResultType visitQualifiedRef(QualifiedRef ref, ArgType arg);
public ResultType visitIndexedRef(IndexedRef ref, ArgType arg); public ResultType visitIndexedRef(IndexedRef ref, ArgType arg);
public ResultType visitIdRef(IdRef ref, ArgType arg); public ResultType visitIdRef(IdRef ref, ArgType arg);
public ResultType visitThisRef(ThisRef ref, ArgType arg); public ResultType visitThisRef(ThisRef ref, ArgType arg);
// Terminals // Terminals
public ResultType visitIdentifier(Identifier id, ArgType arg); public ResultType visitIdentifier(Identifier id, ArgType arg);
public ResultType visitOperator(Operator op, ArgType arg); public ResultType visitOperator(Operator op, ArgType arg);
public ResultType visitIntLiteral(IntLiteral num, ArgType arg); public ResultType visitIntLiteral(IntLiteral num, ArgType arg);
public ResultType visitBooleanLiteral(BooleanLiteral bool, ArgType arg); public ResultType visitBooleanLiteral(BooleanLiteral bool, ArgType arg);
} }

View File

@ -7,8 +7,7 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition; import miniJava.SyntacticAnalyzer.SourcePosition;
public class WhileStmt extends Statement public class WhileStmt extends Statement {
{
public WhileStmt(Expression b, Statement s, SourcePosition posn) { public WhileStmt(Expression b, Statement s, SourcePosition posn) {
super(posn); super(posn);
cond = b; cond = b;

View File

@ -3,15 +3,14 @@ package miniJava;
import java.io.*; import java.io.*;
import miniJava.SyntacticAnalyzer.*; import miniJava.SyntacticAnalyzer.*;
import miniJava.AbstractSyntaxTrees.ASTDisplay; // import miniJava.AbstractSyntaxTrees.ASTDisplay;
import miniJava.AbstractSyntaxTrees.Package; import miniJava.AbstractSyntaxTrees.Package;
import miniJava.ContextualAnalyzer.Analyzer; import miniJava.ContextualAnalyzer.Analyzer;
import miniJava.ContextualAnalyzer.Reporter;
import miniJava.Exceptions.*; import miniJava.Exceptions.*;
public class Compiler { public class Compiler {
private static final int rc = 4; public static final int rc = 4;
public static void main(String[] args) { public static void main(String[] args) {
@ -34,9 +33,7 @@ public class Compiler {
// Identification/Type Checking // Identification/Type Checking
Analyzer analyzer = new Analyzer(); Analyzer analyzer = new Analyzer();
analyzer.visitPackage(p, null); analyzer.visitPackage(p, null);
if(Reporter.error) System.exit(rc); System.exit(analyzer.validate());
System.exit(0);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
System.out.println("***" + e.getMessage()); System.out.println("***" + e.getMessage());

View File

@ -1,93 +1,76 @@
package miniJava.ContextualAnalyzer; 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.*;
import miniJava.AbstractSyntaxTrees.Package; 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<IdentificationTable, Type> { public class Analyzer implements Visitor<IdentificationTable, Type> {
// Topmost Identification Table private MethodDecl mainMethod = null;
private ClassDecl currentClassDecl = null;
private MethodDecl currentMethodDecl = null;
private IdentificationTable table = new IdentificationTable(); private IdentificationTable table = new IdentificationTable();
// Current class decl is for use with the 'this' keyword // Keep track of all predefined names to handle
private Declaration currentClassDecl = null; private static ArrayList<String> predefined;
static
{
predefined = new ArrayList<String>();
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() { public Analyzer() throws ParsingException, ScanningException {
try { for (String s : predefined) {
String __PrintStream = "class _PrintStream { public void println(int n){} }"; Scanner scanner = new Scanner(s);
new Parser(new Scanner(__PrintStream)).parse().visit(this, table); Parser parser = new Parser(scanner);
parser.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!");
} }
} }
/** /**
* Check that two types match. * Checks that contextual analysis was successful or not, returning the
* @param t1 * proper error code in either case.
* @param t2 *
* @return * @return
*/ */
private void match(Type t1, Type 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);
// Check class types match return (Reporter.error) ? Compiler.rc : 0;
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); // PACKAGE
} //
} // /////////////////////////////////////////////////////////////////////////////
@Override
public Type visitPackage(Package prog, IdentificationTable arg) { public Type visitPackage(Package prog, IdentificationTable arg) {
/* Since classes and static methods/fields can be referenced /*
* before the classes themselves are declared, we preprocess * Since classes and static methods/fields can be referenced before the
* all classes and methods first. * 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); IdentificationTable cdTable = table.openScope(cd);
if (cdTable != null) { if (cdTable != null) {
// Simply add fields to current scope // Simply add fields to current scope
@ -111,9 +94,17 @@ public class Analyzer implements Visitor<IdentificationTable, Type> {
return null; return null;
} }
@Override
// /////////////////////////////////////////////////////////////////////////////
//
// DECLARATIONS
//
// /////////////////////////////////////////////////////////////////////////////
public Type visitClassDecl(ClassDecl cd, IdentificationTable arg) { public Type visitClassDecl(ClassDecl cd, IdentificationTable arg) {
IdentificationTable cdTable = arg.getScope(cd); IdentificationTable cdTable = arg.getScope(cd);
if (cdTable != null) { if (cdTable != null) {
for (FieldDecl fd : cd.fieldDeclList) { for (FieldDecl fd : cd.fieldDeclList) {
@ -121,6 +112,7 @@ public class Analyzer implements Visitor<IdentificationTable, Type> {
} }
for (MethodDecl md : cd.methodDeclList) { for (MethodDecl md : cd.methodDeclList) {
currentMethodDecl = md;
md.visit(this, cdTable); md.visit(this, cdTable);
} }
} }
@ -128,142 +120,158 @@ public class Analyzer implements Visitor<IdentificationTable, Type> {
return cd.type; return cd.type;
} }
@Override
public Type visitFieldDecl(FieldDecl fd, IdentificationTable arg) { public Type visitFieldDecl(FieldDecl fd, IdentificationTable arg) {
// Must check that the type of the field can be identified // 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; return fd.type;
} }
@Override
public Type visitMethodDecl(MethodDecl md, IdentificationTable arg) { 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 // 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 // Continue Traversal
IdentificationTable mdTable = arg.getScope(md);
if (mdTable != null) { if (mdTable != null) {
for(ParameterDecl pd : md.parameterDeclList) { for (ParameterDecl pd : md.parameterDeclList)
pd.visit(this, mdTable); pd.visit(this, mdTable);
} for (Statement s : md.statementList)
for(Statement s : md.statementList) {
s.visit(this, mdTable); s.visit(this, mdTable);
}
// Check that return type matches expected type // 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); Type returnType = md.returnExp.visit(this, mdTable);
match(md.type, returnType); IdentificationTable.match(md.type, returnType, true);
} else if(md.type.typeKind != TypeKind.VOID) {
Reporter.report(ErrorType.NO_RETURN_EXPRESSION, md.type);
} }
} }
return md.type; return md.type;
} }
@Override
public Type visitParameterDecl(ParameterDecl pd, IdentificationTable arg) { public Type visitParameterDecl(ParameterDecl pd, IdentificationTable arg) {
arg.setDeclarationAtScope(pd); arg.setDeclarationAtScope(pd);
table.validateClassId(pd.type); if (!table.classExists(pd.type)) {
Reporter.report(ErrorType.UNDECLARED_TYPE, pd.type, null);
}
return pd.type; return pd.type;
} }
@Override
public Type visitVarDecl(VarDecl decl, IdentificationTable arg) { public Type visitVarDecl(VarDecl decl, IdentificationTable arg) {
arg.setDeclarationAtScope(decl); arg.setDeclarationAtScope(decl);
table.validateClassId(decl.type); if (!table.classExists(decl.type)) {
Reporter.report(ErrorType.UNDECLARED_TYPE, decl.type, null);
}
return decl.type; return decl.type;
} }
@Override
// /////////////////////////////////////////////////////////////////////////////
//
// TYPES
//
// /////////////////////////////////////////////////////////////////////////////
public Type visitBaseType(BaseType type, IdentificationTable arg) { public Type visitBaseType(BaseType type, IdentificationTable arg) {
return type; return type;
} }
@Override
public Type visitClassType(ClassType type, IdentificationTable arg) { public Type visitClassType(ClassType type, IdentificationTable arg) {
type.className.visit(this, arg); type.className.visit(this, arg);
return type; return type;
} }
@Override
public Type visitArrayType(ArrayType type, IdentificationTable arg) { public Type visitArrayType(ArrayType type, IdentificationTable arg) {
type.eltType.visit(this, arg); type.eltType.visit(this, arg);
return type; return type;
} }
@Override
// /////////////////////////////////////////////////////////////////////////////
//
// STATEMENTS
//
// /////////////////////////////////////////////////////////////////////////////
public Type visitBlockStmt(BlockStmt stmt, IdentificationTable arg) { public Type visitBlockStmt(BlockStmt stmt, IdentificationTable arg) {
arg.pushLevel(); arg.pushLevel();
for(Statement s : stmt.sl) { for (Statement s : stmt.sl)
s.visit(this, arg); s.visit(this, arg);
}
arg.popLevel(); arg.popLevel();
return null; return null;
} }
@Override
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 // 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) {
Type initExpType = stmt.initExp.visit(this, arg); Type initExpType = stmt.initExp.visit(this, arg);
Type varDeclType = stmt.varDecl.visit(this, arg); Type varDeclType = stmt.varDecl.visit(this, arg);
match(varDeclType, initExpType); IdentificationTable.match(varDeclType, initExpType, true);
return varDeclType; return varDeclType;
} }
@Override
public Type visitAssignStmt(AssignStmt stmt, IdentificationTable arg) { public Type visitAssignStmt(AssignStmt stmt, IdentificationTable arg) {
Type valType = stmt.val.visit(this, arg); Type valType = stmt.val.visit(this, arg);
Type refType = stmt.ref.visit(this, arg); Type refType = stmt.ref.visit(this, arg);
match(valType, refType); IdentificationTable.match(valType, refType, true);
return refType; return refType;
} }
@Override
public Type visitCallStmt(CallStmt stmt, IdentificationTable arg) { public Type visitCallStmt(CallStmt stmt, IdentificationTable arg) {
Type methodType = stmt.methodRef.visit(this, arg); Type methodType = stmt.methodRef.visit(this, arg);
// Check that parameter count is correct and each type is correct // Check that parameter count is correct and each type is correct
MethodDecl decl = (MethodDecl) stmt.methodRef.decl; MethodDecl decl = (MethodDecl) stmt.methodRef.decl;
if (decl.parameterDeclList.size() != stmt.argList.size()) { if (decl.parameterDeclList.size() != stmt.argList.size()) {
Reporter.report(ErrorType.INVALID_PARAM_COUNT, stmt); Reporter.report(ErrorType.INVALID_PARAM_COUNT, stmt, decl);
} else { } 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 exprType = stmt.argList.get(i).visit(this, arg);
Type pdType = decl.parameterDeclList.get(i).type; Type pdType = decl.parameterDeclList.get(i).type;
match(pdType, exprType); IdentificationTable.match(pdType, exprType, true);
} }
} }
return methodType; return methodType;
} }
@Override
public Type visitIfStmt(IfStmt stmt, IdentificationTable arg) { public Type visitIfStmt(IfStmt stmt, IdentificationTable arg) {
// The conditional statment must be a boolean // The conditional statment must be a boolean
Type condType = stmt.cond.visit(this, arg); 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 // A single vardecl cannot exist after a conditional statement
if (stmt.thenStmt instanceof VarDeclStmt) { if (stmt.thenStmt instanceof VarDeclStmt) {
Reporter.report(ErrorType.VAR_COND_ONLY, stmt.thenStmt); Reporter.report(ErrorType.SINGLE_VARCOND, stmt.thenStmt, null);
} else { } else {
stmt.thenStmt.visit(this, arg); stmt.thenStmt.visit(this, arg);
if (stmt.elseStmt != null) { if (stmt.elseStmt != null) {
if (stmt.elseStmt instanceof VarDeclStmt) { if (stmt.elseStmt instanceof VarDeclStmt) {
Reporter.report(ErrorType.VAR_COND_ONLY, stmt.elseStmt); Reporter.report(ErrorType.SINGLE_VARCOND, stmt.elseStmt,
null);
} else { } else {
stmt.elseStmt.visit(this, arg); stmt.elseStmt.visit(this, arg);
} }
@ -273,15 +281,15 @@ public class Analyzer implements Visitor<IdentificationTable, Type> {
return null; return null;
} }
@Override
public Type visitWhileStmt(WhileStmt stmt, IdentificationTable arg) { public Type visitWhileStmt(WhileStmt stmt, IdentificationTable arg) {
// The conditional statment must be a boolean // The conditional statment must be a boolean
Type condType = stmt.cond.visit(this, arg); 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.body instanceof VarDeclStmt) { if (stmt.body instanceof VarDeclStmt) {
Reporter.report(ErrorType.VAR_COND_ONLY, stmt.body); Reporter.report(ErrorType.SINGLE_VARCOND, stmt.body, null);
} else { } else {
stmt.body.visit(this, arg); stmt.body.visit(this, arg);
} }
@ -289,164 +297,270 @@ public class Analyzer implements Visitor<IdentificationTable, Type> {
return null; return null;
} }
@Override
// /////////////////////////////////////////////////////////////////////////////
//
// EXPRESSIONS
//
// /////////////////////////////////////////////////////////////////////////////
public Type visitUnaryExpr(UnaryExpr expr, IdentificationTable arg) { public Type visitUnaryExpr(UnaryExpr expr, IdentificationTable arg) {
Type opType = expr.operator.visit(this, arg); Type opType = expr.operator.visit(this, arg);
Type exprType = expr.expr.visit(this, arg); Type exprType = expr.expr.visit(this, arg);
match(opType, exprType); IdentificationTable.match(opType, exprType, true);
return null; return opType;
} }
@Override
public Type visitBinaryExpr(BinaryExpr expr, IdentificationTable arg) { public Type visitBinaryExpr(BinaryExpr expr, IdentificationTable arg) {
Type opType = expr.operator.visit(this, arg); Type opType = expr.operator.visit(this, arg);
Type leftType = expr.left.visit(this, arg); Type leftType = expr.left.visit(this, arg);
Type rightType = expr.right.visit(this, arg); Type rightType = expr.right.visit(this, arg);
match(opType, leftType); // Both sides must be the same
match(opType, rightType); if(opType.typeKind == TypeKind.EQUALS) {
IdentificationTable.match(leftType, rightType, true);
return null; return new BaseType(TypeKind.BOOLEAN, opType.posn);
}
// 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) { public Type visitRefExpr(RefExpr expr, IdentificationTable arg) {
Type exprType = expr.ref.visit(this, arg);
return expr.ref.visit(this, arg); return exprType;
} }
@Override
public Type visitCallExpr(CallExpr expr, IdentificationTable arg) { public Type visitCallExpr(CallExpr expr, IdentificationTable arg) {
Type functionType = expr.functionRef.visit(this, arg); Type functionRefType = expr.functionRef.visit(this, arg);
if(expr.functionRef.decl instanceof MethodDecl) {
MethodDecl decl = (MethodDecl) expr.functionRef.decl;
// Check that parameter count is correct and each type is correct // Check that parameter count is correct and each type is correct
MethodDecl decl = (MethodDecl) expr.functionRef.decl;
if (decl.parameterDeclList.size() != expr.argList.size()) { if (decl.parameterDeclList.size() != expr.argList.size()) {
Reporter.report(ErrorType.INVALID_PARAM_COUNT, expr); Reporter.report(ErrorType.INVALID_PARAM_COUNT, expr, decl);
} else { } else {
for (int i = 0; i < expr.argList.size(); i++) { for (int i = 0; i < expr.argList.size(); i++) {
Type exprType = expr.argList.get(i).visit(this, arg); Type exprType = expr.argList.get(i).visit(this, arg);
Type pdType = decl.parameterDeclList.get(i).type; Type pdType = decl.parameterDeclList.get(i).type;
match(pdType, exprType); IdentificationTable.match(pdType, exprType, true);
} }
} }
} else {
Reporter.report(ErrorType.NONFUNCTION_CALL, expr, null);
}
return functionType; return functionRefType;
} }
@Override
public Type visitLiteralExpr(LiteralExpr expr, IdentificationTable arg) { public Type visitLiteralExpr(LiteralExpr expr, IdentificationTable arg) {
Type literalType = expr.literal.visit(this, arg);
return expr.literal.visit(this, arg); return literalType;
} }
@Override
public Type visitNewObjectExpr(NewObjectExpr expr, IdentificationTable arg) { public Type visitNewObjectExpr(NewObjectExpr expr, IdentificationTable arg) {
Type objectType = expr.classtype.visit(this, arg);
return expr.classtype.visit(this, arg); return objectType;
} }
@Override
public Type visitNewArrayExpr(NewArrayExpr expr, IdentificationTable arg) { public Type visitNewArrayExpr(NewArrayExpr expr, IdentificationTable arg) {
Type sizeExprType = expr.sizeExpr.visit(this, arg); Type sizeExprType = expr.sizeExpr.visit(this, arg);
if (sizeExprType.typeKind != TypeKind.INT) { if (sizeExprType.typeKind != TypeKind.INT) {
Reporter.report(ErrorType.INVALID_INDEX, expr.sizeExpr); Reporter.report(ErrorType.INVALID_INDEX, expr.sizeExpr, null);
} }
return expr.eltType.visit(this, arg); Type eltType = expr.eltType.visit(this, arg);
return new ArrayType(eltType, expr.posn);
} }
@Override
// /////////////////////////////////////////////////////////////////////////////
//
// REFERENCES
//
// /////////////////////////////////////////////////////////////////////////////
public Type visitQualifiedRef(QualifiedRef ref, IdentificationTable arg) { public Type visitQualifiedRef(QualifiedRef ref, IdentificationTable arg) {
ref.ref.visit(this, arg);
ref.id.visit(this, arg);
// Check that each declaration is nested properly Type refType = ref.ref.visit(this, arg);
if(ref.decl == null || table.getDeclaration(ref.decl.name) == null) {
System.out.println(ref.id.spelling); // Note qualified ref's only make sense in the context of classes
// report(ErrorType.MISSING_DECL, ref.ref.decl); 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;
} }
return null; // 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);
}
}
// 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);
}
ref.id.decl = md;
ref.decl = md;
return md.type;
} }
@Override
public Type visitIndexedRef(IndexedRef ref, IdentificationTable arg) { 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 // Make sure index is an integer
Type indexExprType = ref.indexExpr.visit(this, arg); Type indexExprType = ref.indexExpr.visit(this, arg);
if (indexExprType.typeKind != TypeKind.INT) { if (indexExprType.typeKind != TypeKind.INT) {
Reporter.report(ErrorType.INVALID_INDEX, ref.indexExpr); 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) { public Type visitIdRef(IdRef ref, IdentificationTable arg) {
ref.id.visit(this, arg); Type idType = ref.id.visit(this, arg);
ref.decl = ref.id.decl; ref.decl = ref.id.decl;
return ref.decl.type; return idType;
} }
@Override
public Type visitThisRef(ThisRef ref, IdentificationTable arg) { public Type visitThisRef(ThisRef ref, IdentificationTable arg) {
ref.decl = currentClassDecl; ref.decl = currentClassDecl;
if(currentMethodDecl.isStatic) {
Reporter.report(ErrorType.THIS, ref, currentMethodDecl);
}
return ref.decl.type; return ref.decl.type;
} }
@Override
// /////////////////////////////////////////////////////////////////////////////
//
// TERMINALS
//
// /////////////////////////////////////////////////////////////////////////////
public Type visitIdentifier(Identifier id, IdentificationTable arg) { 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); // Check if identifier can be found in current scope
if(decl == null) { Declaration decl = arg.getDeclarationAtScope(id.spelling);
Reporter.report(ErrorType.UNIDENTIFIED, id); if (decl != null) { id.decl = decl; return decl.type; }
return new BaseType(TypeKind.ERROR, null);
// 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 { } 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) { public Type visitOperator(Operator op, IdentificationTable arg) {
switch (op.token.spelling) { switch (op.token.spelling) {
case "!": case "!":
case ">":
case "<":
case "==":
case "<=":
case ">=":
case "!=":
case "&&": case "&&":
case "||": case "||":
return new BaseType(TypeKind.BOOLEAN, op.posn); return new BaseType(TypeKind.BOOLEAN, op.posn);
case ">":
case "<":
case "<=":
case ">=":
return new BaseType(TypeKind.RELATIONAL, op.posn);
case "+": case "+":
case "-": case "-":
case "*": case "*":
case "/": case "/":
return new BaseType(TypeKind.INT, op.posn); return new BaseType(TypeKind.INT, op.posn);
default: case "==":
Reporter.report(ErrorType.UNIDENTIFIED_TYPE, op); case "!=":
return new BaseType(TypeKind.ERROR, op.posn); return new BaseType(TypeKind.EQUALS, op.posn);
} }
return null;
} }
@Override
public Type visitIntLiteral(IntLiteral num, IdentificationTable arg) { public Type visitIntLiteral(IntLiteral num, IdentificationTable arg) {
return new BaseType(TypeKind.INT, num.posn); return new BaseType(TypeKind.INT, num.posn);
} }
@Override
public Type visitBooleanLiteral(BooleanLiteral bool, IdentificationTable arg) { public Type visitBooleanLiteral(BooleanLiteral bool, IdentificationTable arg) {
return new BaseType(TypeKind.BOOLEAN, bool.posn); return new BaseType(TypeKind.BOOLEAN, bool.posn);

View File

@ -1,12 +1,8 @@
package miniJava.ContextualAnalyzer; package miniJava.ContextualAnalyzer;
import java.util.HashMap; import java.util.*;
import java.util.ArrayList;
import miniJava.AbstractSyntaxTrees.ClassType; import miniJava.AbstractSyntaxTrees.*;
import miniJava.AbstractSyntaxTrees.Declaration;
import miniJava.AbstractSyntaxTrees.Type;
import miniJava.AbstractSyntaxTrees.TypeKind;
public class IdentificationTable { public class IdentificationTable {
@ -33,29 +29,29 @@ public class IdentificationTable {
} }
/** /**
* Adds another level for variables to be stored at (they will be * Adds another level for variables to be stored at
* removed when popping said level).
*/
public void pushLevel() {
table.add(new HashMap<String, Declaration>());
}
/**
* Removes all variables declared at the current level (these are
* no longer accessible).
*/ */
public void popLevel() { public void popLevel() {
table.remove(table.size() - 1); 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<String, Declaration>());
}
/**
* This method will only ever be called with class/method declarations.
*
* @param decl * @param decl
* @return * @return
*/ */
public IdentificationTable openScope(Declaration decl) { public IdentificationTable openScope(Declaration decl) {
if(scope.containsKey(decl.name) || getDeclarationAtScope(decl.name) != null) { Declaration current = getDeclarationAtScope(decl.name);
Reporter.report(ErrorType.REDEFINITION, decl); if (scope.containsKey(decl.name) || current != null) {
Reporter.report(ErrorType.REDEFINITION, decl, current);
return null; return null;
} else { } else {
table.get(table.size() - 1).put(decl.name, decl); table.get(table.size() - 1).put(decl.name, decl);
@ -65,7 +61,9 @@ public class IdentificationTable {
} }
/** /**
* Return nested scope corresponding to declaration (or null if non-existant). * Return nested scope corresponding to declaration (or null if
* non-existant).
*
* @param decl * @param decl
* @return * @return
*/ */
@ -78,8 +76,9 @@ public class IdentificationTable {
} }
/** /**
* Iterates through all parents and tries to find the specified * Iterates through all parents and tries to find the specified declaration
* declaration by name. * by name.
*
* @param name * @param name
* @return * @return
*/ */
@ -87,28 +86,24 @@ public class IdentificationTable {
IdentificationTable current = this; IdentificationTable current = this;
while (current != null) { while (current != null) {
Declaration decl = current.getDeclarationAtScope(name); Declaration decl = current.getDeclarationAtScope(name);
if(decl == null) { if (decl == null) current = current.parent;
current = current.parent; else return decl;
} else {
return decl;
}
} }
return null; return null;
} }
/** /**
* Iterates through levels (from higher to lower) for declaration, * Iterates through levels (from higher to lower) for declaration, returning
* returning none if it does not exist. * none if it does not exist.
*
* @param name * @param name
* @return * @return
*/ */
public Declaration getDeclarationAtScope(String name) { public Declaration getDeclarationAtScope(String name) {
for (int i = table.size() - 1; i >= 0; i--) { for (int i = table.size() - 1; i >= 0; i--) {
HashMap<String, Declaration> level = table.get(i); HashMap<String, Declaration> level = table.get(i);
if(level.containsKey(name)) { if (level.containsKey(name)) return level.get(name);
return level.get(name);
}
} }
return null; return null;
@ -116,13 +111,15 @@ public class IdentificationTable {
/** /**
* Add declaration to current table's table member. * Add declaration to current table's table member.
*
* @param name * @param name
*/ */
public void setDeclarationAtScope(Declaration decl) { public void setDeclarationAtScope(Declaration decl) {
for (int i = 0; i < table.size(); i++) { for (int i = 0; i < table.size(); i++) {
HashMap<String, Declaration> level = table.get(i); HashMap<String, Declaration> level = table.get(i);
if (level.containsKey(decl.name)) { if (level.containsKey(decl.name)) {
Reporter.report(ErrorType.REDEFINITION, decl); Declaration defined = level.get(decl.name);
Reporter.report(ErrorType.REDEFINITION, decl, defined);
return; return;
} }
} }
@ -131,15 +128,94 @@ public class IdentificationTable {
} }
/** /**
* Checks that the passed class type does exist. * Checks whether the specified class has been declared.
* @param ct *
* @param t
* @return
*/ */
public void validateClassId(Type t) { public boolean classExists(Type t) {
if (t.typeKind == TypeKind.CLASS) { if (t.typeKind == TypeKind.CLASS) {
ClassType ct = (ClassType) t; ClassType ct = (ClassType) t;
if(getDeclaration(ct.className.spelling) == null) { return getDeclaration(ct.className.spelling) != null;
Reporter.report(ErrorType.MISSING_DECL, ct); }
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;
} }
} }

View File

@ -3,17 +3,20 @@ package miniJava.ContextualAnalyzer;
import miniJava.AbstractSyntaxTrees.*; import miniJava.AbstractSyntaxTrees.*;
enum ErrorType { enum ErrorType {
VAR_COND_ONLY, THIS,
MISSING_DECL, NONFUNCTION_CALL,
UNIDENTIFIED, UNDEFINED,
REDEFINITION, STATIC,
INVALID_PARAM_COUNT, VISIBILITY,
INVALID_INDEX, NO_RETURN,
TYPE_MISMATCH, TYPE_MISMATCH,
UNIDENTIFIED_TYPE, REDEFINITION,
TYPE_CLASS_MISMATCH, MAIN_UNDECLARED,
TYPE_ARRAY_MISMATCH, INVALID_PARAM_COUNT,
NO_RETURN_EXPRESSION; MULTIPLE_MAIN,
UNDECLARED_TYPE,
SINGLE_VARCOND,
INVALID_INDEX
} }
public class Reporter { public class Reporter {
@ -21,102 +24,147 @@ public class Reporter {
public static boolean error = false; public static boolean error = false;
/** /**
* Prints out to console correct error message. * Convenience function for getting type names.
* @param type *
* @param ast * @param t
* @return
*/ */
public static void report(ErrorType type, AST ast) { private static String getTypeName(Type t) {
error = true; 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) { switch (type) {
// VarDeclStmt is only statement in conditional branch // Cannot access 'this' in a static method
case VAR_COND_ONLY: { case THIS: {
System.out.println("***Conditional statment cannot be followed by a variable declaration statement " + ast.posn); MethodDecl md = (MethodDecl) a2;
emit("Cannot reference 'this' " + a1.posn + " in static method '" + md.name + "' " + md.posn);
break; break;
} }
// Declaration does not exist for reference // Attempting to call a non function as a function
case MISSING_DECL: { case NONFUNCTION_CALL: {
System.out.println("***Reference to a non-existant type " + ast.posn); emit("Not a valid function call at " + a1.posn);
break; break;
} }
// Reports when a reference could not be found // Tried accessing a non-static member from a static method
case UNIDENTIFIED: { case STATIC: {
System.out.println("***Reference refers to a declaration that does not exist " + ast.posn); 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; break;
} }
// Attempting to redeclare a variable // Attempting to redeclare a variable
case REDEFINITION: { 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; break;
} }
// A non void function does not have a return statement // Identifier could not be found
case NO_RETURN_EXPRESSION: { case UNDEFINED: {
System.out.println("***Non-void method does not have a return statement " + ast.posn); Identifier ident = (Identifier) a1;
emit("Identifier '" + ident.spelling + "' " + ident.posn + " is undeclared.");
break; break;
} }
// The number of parameters passed is either too few or too great // A public static void main(String[] args) method was not declared
case MAIN_UNDECLARED: {
emit("A main function was not declared");
break;
}
// Parameter counts of an expression/statement do not match declaration
case INVALID_PARAM_COUNT: { case INVALID_PARAM_COUNT: {
System.out.println("***The number of passed parameters does not equal expected count " + ast.posn); MethodDecl md = (MethodDecl) a2;
emit("Call to '" + md.name + "' " + a2.posn + " has an invalid parameter count at " + a1.posn);
break;
} }
// The expected expression MUST return an int (such as the index of an array) // 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;
}
// 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: { case INVALID_INDEX: {
System.out.println("***Expected an integer value as the index of an array " + ast.posn); emit("Index expression is not of type int " + a1.posn);
break;
}
// Hmmm.....
case UNIDENTIFIED_TYPE: {
System.out.println("***Unexpected type " + ast.posn);
break;
}
// Clear Warning
default:
break; break;
} }
} }
/**
* Type specific error reporting.
* @param type
* @param t1
* @param t2
*/
public static void report(ErrorType type, Type t1, Type t2) {
error = true; 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);
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);
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);
break;
}
// Clear Warning
default:
break;
}
}
} }

View File

@ -11,7 +11,7 @@ public class ParsingException extends Exception {
} }
public ParsingException(Token t) { public ParsingException(Token t) {
super("Parsing error with " + t.toString()); super("Parsing error with " + t.spelling + " at " + t.posn.toString());
} }
} }

View File

@ -19,6 +19,7 @@ public class Parser {
/** /**
* Program ::= (ClassDeclaration)* eot * Program ::= (ClassDeclaration)* eot
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
public Package parse() throws ParsingException, ScanningException { public Package parse() throws ParsingException, ScanningException {
@ -32,14 +33,13 @@ public class Parser {
} }
/** /**
* ClassDeclaration ::= * ClassDeclaration ::= class id { (Declarators id (; | MethodDeclaration))* }
* class id {
* (Declarators id (; | MethodDeclaration))*
* }
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private ClassDecl parseClassDeclaration() throws ParsingException, ScanningException { private ClassDecl parseClassDeclaration() throws ParsingException,
ScanningException {
// Class Header // Class Header
Token classToken = accept(Token.TYPE.CLASS); Token classToken = accept(Token.TYPE.CLASS);
@ -54,7 +54,8 @@ public class Parser {
while (peek(1).type != Token.TYPE.RBRACKET) { while (peek(1).type != Token.TYPE.RBRACKET) {
Declarators d = parseDeclarators(); Declarators d = parseDeclarators();
String name = accept(Token.TYPE.ID).spelling; 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 // Field Declarations
if (peek(1).type == Token.TYPE.SEMICOLON) { if (peek(1).type == Token.TYPE.SEMICOLON) {
@ -63,7 +64,8 @@ public class Parser {
} }
// Method Declarations // Method Declarations
else mdl.add(parseMethodDeclaration(f)); else
mdl.add(parseMethodDeclaration(f));
} }
accept(Token.TYPE.RBRACKET); accept(Token.TYPE.RBRACKET);
@ -73,9 +75,11 @@ public class Parser {
/** /**
* Declarators ::= (public | private)? static? Type * Declarators ::= (public | private)? static? Type
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Declarators parseDeclarators() throws ParsingException, ScanningException { private Declarators parseDeclarators() throws ParsingException,
ScanningException {
// Visibility // Visibility
SourcePosition start = null; SourcePosition start = null;
@ -108,22 +112,22 @@ public class Parser {
} }
/** /**
* * MethodDeclaration ::= * MethodDeclaration ::= (ParameterList?) { Statement* (return Expression;)? }
* (ParameterList?) { * @param f
* Statement* (return Expression ;)?
* }
* @param f describes the declaratory aspect of the method
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private MethodDecl parseMethodDeclaration(FieldDecl f) throws ParsingException, ScanningException { private MethodDecl parseMethodDeclaration(FieldDecl f)
throws ParsingException, ScanningException {
// Method Header // Method Header
accept(Token.TYPE.LPAREN); accept(Token.TYPE.LPAREN);
// Parameter List // Parameter List
ParameterDeclList pdl = new ParameterDeclList(); 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.RPAREN);
accept(Token.TYPE.LBRACKET); accept(Token.TYPE.LBRACKET);
@ -150,6 +154,7 @@ public class Parser {
/** /**
* Type ::= boolean | void | int ([])? | id ([])? * Type ::= boolean | void | int ([])? | id ([])?
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Type parseType() throws ParsingException, ScanningException { private Type parseType() throws ParsingException, ScanningException {
@ -201,6 +206,7 @@ public class Parser {
/** /**
* ParameterList ::= Type id (, Type id)* * ParameterList ::= Type id (, Type id)*
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private ParameterDeclList parseParameterList() throws ParsingException, ScanningException { private ParameterDeclList parseParameterList() throws ParsingException, ScanningException {
@ -226,6 +232,7 @@ public class Parser {
/** /**
* ArgumentList ::= Expression (, Expression)* * ArgumentList ::= Expression (, Expression)*
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private ExprList parseArgumentList() throws ParsingException, ScanningException { private ExprList parseArgumentList() throws ParsingException, ScanningException {
@ -242,6 +249,7 @@ public class Parser {
/** /**
* Reference ::= BaseRef (. BaseRef)* * Reference ::= BaseRef (. BaseRef)*
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Reference parseReference() throws ParsingException, ScanningException { private Reference parseReference() throws ParsingException, ScanningException {
@ -266,6 +274,7 @@ public class Parser {
/** /**
* BaseRef ::= this | id ([ Expression])? * BaseRef ::= this | id ([ Expression])?
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Reference parseBaseRef() throws ParsingException, ScanningException { private Reference parseBaseRef() throws ParsingException, ScanningException {
@ -295,14 +304,11 @@ public class Parser {
} }
/** /**
* Statement ::= * Statement ::= {Statement*} | Type id = Expression; | Reference =
* {Statement*} * Expression; | Reference ( ArgumentList? ); | if (Expression) Statement
* | Type id = Expression; * (else Statement)? | while (Expression) Statement
* | Reference = Expression;
* | Reference ( ArgumentList? );
* | if (Expression) Statement (else Statement)?
* | while (Expression) Statement
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Statement parseStatement() throws ParsingException, ScanningException { private Statement parseStatement() throws ParsingException, ScanningException {
@ -349,10 +355,14 @@ public class Parser {
} }
// Type id = Expression ; // 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 // 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 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)) { || (peek(2).type == Token.TYPE.LSQUARE && peek(3).type == Token.TYPE.RSQUARE)) {
Type t = parseType(); Type t = parseType();
String name = accept(Token.TYPE.ID).spelling; String name = accept(Token.TYPE.ID).spelling;
@ -397,13 +407,10 @@ public class Parser {
} }
/** /**
* Expression ::= * Expression ::= Reference | Reference ( ArgumentList? ) | unop Expression
* Reference * | ( Expression ) | num | true | false | new (id() | int [ Expression ] |
* | Reference ( ArgumentList? ) * id [ Expression ] )
* | unop Expression *
* | ( Expression )
* | num | true | false
* | new (id() | int [ Expression ] | id [ Expression ] )
* @return * @return
* @throws ScanningException * @throws ScanningException
*/ */
@ -444,8 +451,8 @@ public class Parser {
Token opToken = accept(peek(1).type); Token opToken = accept(peek(1).type);
Operator o = new Operator(opToken, opToken.posn); Operator o = new Operator(opToken, opToken.posn);
e = new UnaryExpr(o, parseSingleExpression(), opToken.posn); e = new UnaryExpr(o, parseSingleExpression(), opToken.posn);
} } else
else throw new ParsingException(); throw new ParsingException();
break; break;
} }
@ -484,7 +491,8 @@ public class Parser {
} }
// Reference ((ArgumentList?))? // Reference ((ArgumentList?))?
case THIS: case ID: { case THIS:
case ID: {
Reference r = parseReference(); Reference r = parseReference();
if (peek(1).type == Token.TYPE.LPAREN) { if (peek(1).type == Token.TYPE.LPAREN) {
accept(Token.TYPE.LPAREN); accept(Token.TYPE.LPAREN);
@ -502,16 +510,16 @@ public class Parser {
} }
default: default:
throw new ParsingException(); throw new ParsingException(peek(1));
} }
return e; return e;
} }
/** /**
* Disjunction & Initial Call: * Disjunction & Initial Call: Expression ::= Expression binop Expression
* Expression ::= Expression binop Expression
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Expression parseExpression() throws ParsingException, ScanningException { private Expression parseExpression() throws ParsingException, ScanningException {
@ -529,6 +537,7 @@ public class Parser {
/** /**
* Conjunction * Conjunction
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Expression parseCExpression() throws ParsingException, ScanningException { private Expression parseCExpression() throws ParsingException, ScanningException {
@ -546,6 +555,7 @@ public class Parser {
/** /**
* Equality * Equality
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Expression parseEExpression() throws ParsingException, ScanningException { private Expression parseEExpression() throws ParsingException, ScanningException {
@ -563,13 +573,15 @@ public class Parser {
/** /**
* Relational * Relational
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Expression parseRExpression() throws ParsingException, ScanningException { private Expression parseRExpression() throws ParsingException, ScanningException {
Expression e = parseAExpression(); Expression e = parseAExpression();
while (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(">=")) { || peek(1).spelling.equals(">")
|| peek(1).spelling.equals(">=")) {
Token opToken = accept(Token.TYPE.BINOP); Token opToken = accept(Token.TYPE.BINOP);
Operator o = new Operator(opToken, opToken.posn); Operator o = new Operator(opToken, opToken.posn);
e = new BinaryExpr(o, e, parseAExpression(), e.posn); e = new BinaryExpr(o, e, parseAExpression(), e.posn);
@ -581,6 +593,7 @@ public class Parser {
/** /**
* Additive * Additive
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Expression parseAExpression() throws ParsingException, ScanningException { private Expression parseAExpression() throws ParsingException, ScanningException {
@ -598,6 +611,7 @@ public class Parser {
/** /**
* Multiplicative * Multiplicative
* @return * @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Expression parseMExpression() throws ParsingException, ScanningException { private Expression parseMExpression() throws ParsingException, ScanningException {
@ -614,6 +628,7 @@ public class Parser {
/** /**
* Sees what the next token is, caching the result. * Sees what the next token is, caching the result.
* @param lookahead
* @return * @return
* @throws ScanningException * @throws ScanningException
*/ */
@ -628,9 +643,11 @@ public class Parser {
return stream.get(lookahead - 1); return stream.get(lookahead - 1);
} }
/** /**
* Consumes token or throws exception. * Consumes token or throws exception.
* @param type
* @return
* @throws ParsingException
* @throws ScanningException * @throws ScanningException
*/ */
private Token accept(Token.TYPE type) throws ParsingException, ScanningException { private Token accept(Token.TYPE type) throws ParsingException, ScanningException {

View File

@ -20,6 +20,7 @@ public class Scanner {
/** /**
* Scans in input, returning next token. * Scans in input, returning next token.
*
* @return * @return
* @throws IOException * @throws IOException
*/ */
@ -32,7 +33,8 @@ public class Scanner {
// Check for EOF // Check for EOF
int c = read(); int c = read();
if(c == -1) return new Token("", Token.TYPE.EOT); if (c == -1)
return new Token("", Token.TYPE.EOT);
// Setup // Setup
attr += (char) c; attr += (char) c;
@ -45,32 +47,42 @@ public class Scanner {
break; break;
case '+': case '+':
if(peek('+')) throw new ScanningException(col, line); if (peek('+'))
throw new ScanningException(col, line);
token = new Token(attr, Token.TYPE.BINOP); token = new Token(attr, Token.TYPE.BINOP);
break; break;
case '-': case '-':
if(peek('-')) throw new ScanningException(col, line); if (peek('-'))
throw new ScanningException(col, line);
token = new Token(attr, Token.TYPE.BINOP); token = new Token(attr, Token.TYPE.BINOP);
break; break;
// Check for comment // Check for comment
case '/': case '/':
if(peek('*')) { read(); readComment(); attr = ""; } if (peek('*')) {
else if(peek('/')) { readLine(); attr = ""; } read();
else token = new Token(attr, Token.TYPE.BINOP); readComment();
attr = "";
} else if (peek('/')) {
readLine();
attr = "";
} else
token = new Token(attr, Token.TYPE.BINOP);
break; break;
// Check for c or c= // Check for c or c=
case '>': case '>':
case '<': case '<':
if(peek('=')) attr += (char) read(); if (peek('='))
attr += (char) read();
token = new Token(attr, Token.TYPE.BINOP); token = new Token(attr, Token.TYPE.BINOP);
break; break;
// Check for ! or != // Check for ! or !=
case '!': case '!':
if(!peek('=')) token = new Token(attr, Token.TYPE.UNOP); if (!peek('='))
token = new Token(attr, Token.TYPE.UNOP);
else { else {
attr += (char) read(); attr += (char) read();
token = new Token(attr, Token.TYPE.BINOP); token = new Token(attr, Token.TYPE.BINOP);
@ -80,7 +92,8 @@ public class Scanner {
// Check for && or || // Check for && or ||
case '&': case '&':
case '|': case '|':
if(!peek((char) c)) throw new ScanningException(col, line); if (!peek((char) c))
throw new ScanningException(col, line);
else { else {
attr += (char) read(); attr += (char) read();
token = new Token(attr, Token.TYPE.BINOP); token = new Token(attr, Token.TYPE.BINOP);
@ -89,7 +102,8 @@ public class Scanner {
// Other Operators // Other Operators
case '=': case '=':
if(!peek('=')) token = new Token(attr, Token.TYPE.EQUALS); if (!peek('='))
token = new Token(attr, Token.TYPE.EQUALS);
else { else {
attr += (char) read(); attr += (char) read();
token = new Token(attr, Token.TYPE.BINOP); token = new Token(attr, Token.TYPE.BINOP);
@ -164,7 +178,9 @@ public class Scanner {
} }
// Unrecognized Character // Unrecognized Character
else throw new ScanningException(col, line);; else
throw new ScanningException(col, line);
;
break; break;
} }
@ -174,9 +190,9 @@ public class Scanner {
return token; return token;
} }
/** /**
* Looks at next character in stream without consuming. * Looks at next character in stream without consuming.
*
* @return * @return
* @throws IOException * @throws IOException
*/ */
@ -192,9 +208,9 @@ public class Scanner {
} }
} }
/** /**
* Returns whether passed character is next in stream. * Returns whether passed character is next in stream.
*
* @param c * @param c
* @return * @return
* @throws IOException * @throws IOException
@ -211,19 +227,22 @@ public class Scanner {
} }
} }
/** /**
* Alternative reading that keeps track of position. * Alternative reading that keeps track of position.
*
* @return * @return
* @throws IOException * @throws IOException
*/ */
private int read() throws ScanningException { private int read() throws ScanningException {
try { try {
int next = input.read(); int next = input.read();
if(next != '\n' && next != '\r') col += 1; if (next != '\n' && next != '\r')
col += 1;
else { else {
col = 1; line += 1; col = 1;
if(peek('\r') || peek('\n')) next = input.read(); line += 1;
if (peek('\r') || peek('\n'))
next = input.read();
} }
return next; return next;
@ -232,9 +251,9 @@ public class Scanner {
} }
} }
/** /**
* Consumes input until an end of comment has been reached. * Consumes input until an end of comment has been reached.
*
* @throws IOException * @throws IOException
*/ */
private void readComment() throws ScanningException { private void readComment() throws ScanningException {
@ -244,35 +263,36 @@ public class Scanner {
prev = current; prev = current;
int next = read(); int next = read();
if(next == -1) throw new ScanningException(col, line); if (next == -1)
else current = (char) next; throw new ScanningException(col, line);
else
current = (char) next;
} }
} }
/** /**
* Consumes input until the end of line is reached * Consumes input until the end of line is reached
*
* @throws IOException * @throws IOException
*/ */
private void readLine() throws ScanningException { private void readLine() throws ScanningException {
for(int n = 0; n != '\n' && n != '\r' && n != -1; n = read()) {} for (int n = 0; n != '\n' && n != '\r' && n != -1; n = read()) {
}
} }
/** /**
* Tells whether character is alphabetical. * Tells whether character is alphabetical.
*
* @param c * @param c
* @return * @return
*/ */
private boolean isAlpha(char c) { private boolean isAlpha(char c) {
return (c >= 'a' && c <= 'z') return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
|| (c >= 'A' && c <= 'Z')
|| c == '_';
} }
/** /**
* Tells whether character is numerical. * Tells whether character is numerical.
*
* @param c * @param c
* @return * @return
*/ */
@ -280,17 +300,14 @@ public class Scanner {
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
/** /**
* Tells wheter character is whitespace. * Tells wheter character is whitespace.
*
* @param c * @param c
* @return * @return
*/ */
private boolean isWhitespace(char c) { private boolean isWhitespace(char c) {
return c == ' ' return c == ' ' || c == '\n' || c == '\r' || c == '\t';
|| c == '\n'
|| c == '\r'
|| c == '\t';
} }
} }

View File

@ -7,49 +7,23 @@ public class Token {
public enum TYPE { public enum TYPE {
// Possible Terminals // Possible Terminals
ID, ID, NUM, UNOP, BINOP,
NUM,
UNOP,
BINOP,
// Keywords // Keywords
IF, IF, ELSE, NEW, INT, VOID, THIS, TRUE, FALSE, CLASS, WHILE, RETURN, BOOLEAN,
ELSE,
NEW,
INT,
VOID,
THIS,
TRUE,
FALSE,
CLASS,
WHILE,
RETURN,
BOOLEAN,
// Declarators // Declarators
STATIC, STATIC, PUBLIC, PRIVATE,
PUBLIC,
PRIVATE,
// Other Terminals // Other Terminals
EQUALS, EQUALS, PERIOD, COMMA, LPAREN, RPAREN, LSQUARE, RSQUARE, LBRACKET, RBRACKET, SEMICOLON,
PERIOD,
COMMA,
LPAREN,
RPAREN,
LSQUARE,
RSQUARE,
LBRACKET,
RBRACKET,
SEMICOLON,
// End of Token Stream // End of Token Stream
EOT EOT
}; };
public final static HashMap<String, TYPE> keywords; public final static HashMap<String, TYPE> keywords;
static static {
{
keywords = new HashMap<String, TYPE>(); keywords = new HashMap<String, TYPE>();
keywords.put("class", TYPE.CLASS); keywords.put("class", TYPE.CLASS);
keywords.put("return", TYPE.RETURN); keywords.put("return", TYPE.RETURN);

View File

@ -1,5 +0,0 @@
class Test {
public int Test() {
}
}