From 690dc9d5629502bef796121a01d46362a149d378 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Sat, 22 Feb 2014 02:33:31 -0500 Subject: [PATCH] Precedence Incorporated necessary precedence rules. --- .../miniJava/SyntacticAnalyzer/Parser.java | 97 +++++++++---------- .../miniJava/SyntacticAnalyzer/Scanner.java | 10 +- miniJava/test.java | 6 +- 3 files changed, 59 insertions(+), 54 deletions(-) diff --git a/miniJava/src/miniJava/SyntacticAnalyzer/Parser.java b/miniJava/src/miniJava/SyntacticAnalyzer/Parser.java index 6930c9a..ea433a5 100644 --- a/miniJava/src/miniJava/SyntacticAnalyzer/Parser.java +++ b/miniJava/src/miniJava/SyntacticAnalyzer/Parser.java @@ -387,15 +387,14 @@ public class Parser { * Reference * | Reference ( ArgumentList? ) * | unop Expression - * | Expression binop Expression * | ( Expression ) * | num | true | false * | new (id() | int [ Expression ] | id [ Expression ] ) * @return * @throws IOException */ - private Expression parseExpression() throws IOException { - + private Expression parseSingleExpression() throws IOException { + Expression e = null; switch(peek(1).type) { @@ -429,7 +428,7 @@ public class Parser { case BINOP: { if(peek(1).spelling.equals("!") || peek(1).spelling.equals("-")) { Operator o = new Operator(accept(peek(1).type), null); - e = new UnaryExpr(o, parseExpression(), null); + e = new UnaryExpr(o, parseSingleExpression(), null); } else throw new IOException(); break; @@ -491,22 +490,33 @@ public class Parser { throw new IOException(); } - // Expression binop Expression - if(peek(1).type == Token.TYPE.BINOP) { + return e; + } + + /** + * Disjunction & Initial Call: + * Expression ::= Expression binop Expression + * @return + * @throws IOException + */ + private Expression parseExpression() throws IOException { + Expression e = parseCExpression(); + while(peek(1).spelling.equals("||")) { Operator o = new Operator(accept(Token.TYPE.BINOP), null); - e = new BinaryExpr(o, e, parseDExpression(), null); + e = new BinaryExpr(o, e, parseExpression(), null); } return e; } /** - * Disjunction + * Conjunction + * @return * @throws IOException */ - private Expression parseDExpression() throws IOException { - Expression e = parseCExpression(); - while(peek(1).spelling.equals("||")) { + private Expression parseCExpression() throws IOException { + Expression e = parseEExpression(); + while(peek(1).spelling.equals("&&")) { Operator o = new Operator(accept(Token.TYPE.BINOP), null); e = new BinaryExpr(o, e, parseCExpression(), null); } @@ -515,12 +525,13 @@ public class Parser { } /** - * Conjunction + * Equality + * @return * @throws IOException */ - private Expression parseCExpression() throws IOException { - Expression e = parseEExpression(); - while(peek(1).spelling.equals("&&")) { + private Expression parseEExpression() throws IOException { + Expression e = parseRExpression(); + while(peek(1).spelling.equals("==") || peek(1).spelling.equals("!=")) { Operator o = new Operator(accept(Token.TYPE.BINOP), null); e = new BinaryExpr(o, e, parseEExpression(), null); } @@ -529,12 +540,14 @@ public class Parser { } /** - * Equality + * Relational + * @return * @throws IOException */ - private Expression parseEExpression() throws IOException { - Expression e = parseRExpression(); - while(peek(1).spelling.equals("==") || peek(1).spelling.equals("!=")) { + private Expression parseRExpression() throws IOException { + Expression e = parseAExpression(); + while(peek(1).spelling.equals("<") || peek(1).spelling.equals("<=") + || peek(1).spelling.equals(">") || peek(1).spelling.equals(">=")) { Operator o = new Operator(accept(Token.TYPE.BINOP), null); e = new BinaryExpr(o, e, parseRExpression(), null); } @@ -542,49 +555,35 @@ public class Parser { return e; } - /** - * Relational - * @throws IOException - */ - private Expression parseRExpression() throws IOException { - Expression e = parseAExpression(); - while(peek(1).spelling.equals("<=") || peek(1).spelling.equals(">=") - || peek(1).spelling.equals("<") || peek(1).spelling.equals(">")) { - Operator o = new Operator(accept(Token.TYPE.BINOP), null); - e = new BinaryExpr(o, e, parseAExpression(), null); - } - - return e; - } - /** * Additive + * @return * @throws IOException */ private Expression parseAExpression() throws IOException { Expression e = parseMExpression(); while(peek(1).spelling.equals("+") || peek(1).spelling.equals("-")) { Operator o = new Operator(accept(Token.TYPE.BINOP), null); - e = new BinaryExpr(o, e, parseMExpression(), null); - } - - return e; - } - - /** - * Multiplicative - * @throws IOException - */ - private Expression parseMExpression() throws IOException { - Expression e = parseExpression(); - while(peek(1).spelling.equals("*") || peek(1).spelling.equals("/")) { - Operator o = new Operator(accept(Token.TYPE.BINOP), null); - e = new BinaryExpr(o, e, parseExpression(), null); + e = new BinaryExpr(o, e, parseAExpression(), null); } return e; } + /** + * Multiplicative + * @return + * @throws IOException + */ + private Expression parseMExpression() throws IOException { + Expression e = parseSingleExpression(); + while(peek(1).spelling.equals("*") || peek(1).spelling.equals("/")) { + Operator o = new Operator(accept(Token.TYPE.BINOP), null); + e = new BinaryExpr(o, e, parseMExpression(), null); + } + + return e; + } /** * Sees what the next token is, caching the result. diff --git a/miniJava/src/miniJava/SyntacticAnalyzer/Scanner.java b/miniJava/src/miniJava/SyntacticAnalyzer/Scanner.java index 98a6cb2..c3dec71 100644 --- a/miniJava/src/miniJava/SyntacticAnalyzer/Scanner.java +++ b/miniJava/src/miniJava/SyntacticAnalyzer/Scanner.java @@ -32,9 +32,17 @@ public class Scanner { switch(c) { // Operators + case '*': + token = new Token(attr, Token.TYPE.BINOP); + break; + case '+': - case '*': + if(peek('+')) throw new IOException(); + token = new Token(attr, Token.TYPE.BINOP); + break; + case '-': + if(peek('-')) throw new IOException(); token = new Token(attr, Token.TYPE.BINOP); break; diff --git a/miniJava/test.java b/miniJava/test.java index b4ffe40..9bf40d7 100644 --- a/miniJava/test.java +++ b/miniJava/test.java @@ -2,11 +2,9 @@ class PA2 { public boolean c; + private static id[] test; public static void main(String[] args) { - if(x > 1) - x = 1 + 2 * x; - else - b[3].a = 4; + int x = -1 || 2 + (8 <= 8) == -6 && (9 > 10) * 3; } } \ No newline at end of file