1
Fork 0

Precedence

Incorporated necessary precedence rules.
master
Joshua Potter 2014-02-22 02:33:31 -05:00
parent 46a6e73f5d
commit 690dc9d562
3 changed files with 59 additions and 54 deletions

View File

@ -387,15 +387,14 @@ public class Parser {
* Reference * Reference
* | Reference ( ArgumentList? ) * | Reference ( ArgumentList? )
* | unop Expression * | unop Expression
* | Expression binop Expression
* | ( Expression ) * | ( Expression )
* | num | true | false * | num | true | false
* | new (id() | int [ Expression ] | id [ Expression ] ) * | new (id() | int [ Expression ] | id [ Expression ] )
* @return * @return
* @throws IOException * @throws IOException
*/ */
private Expression parseExpression() throws IOException { private Expression parseSingleExpression() throws IOException {
Expression e = null; Expression e = null;
switch(peek(1).type) { switch(peek(1).type) {
@ -429,7 +428,7 @@ public class Parser {
case BINOP: { case BINOP: {
if(peek(1).spelling.equals("!") || peek(1).spelling.equals("-")) { if(peek(1).spelling.equals("!") || peek(1).spelling.equals("-")) {
Operator o = new Operator(accept(peek(1).type), null); 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(); else throw new IOException();
break; break;
@ -491,22 +490,33 @@ public class Parser {
throw new IOException(); throw new IOException();
} }
// Expression binop Expression return e;
if(peek(1).type == Token.TYPE.BINOP) { }
/**
* 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); 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; return e;
} }
/** /**
* Disjunction * Conjunction
* @return
* @throws IOException * @throws IOException
*/ */
private Expression parseDExpression() throws IOException { private Expression parseCExpression() throws IOException {
Expression e = parseCExpression(); Expression e = parseEExpression();
while(peek(1).spelling.equals("||")) { while(peek(1).spelling.equals("&&")) {
Operator o = new Operator(accept(Token.TYPE.BINOP), null); Operator o = new Operator(accept(Token.TYPE.BINOP), null);
e = new BinaryExpr(o, e, parseCExpression(), null); e = new BinaryExpr(o, e, parseCExpression(), null);
} }
@ -515,12 +525,13 @@ public class Parser {
} }
/** /**
* Conjunction * Equality
* @return
* @throws IOException * @throws IOException
*/ */
private Expression parseCExpression() throws IOException { private Expression parseEExpression() throws IOException {
Expression e = parseEExpression(); Expression e = parseRExpression();
while(peek(1).spelling.equals("&&")) { while(peek(1).spelling.equals("==") || peek(1).spelling.equals("!=")) {
Operator o = new Operator(accept(Token.TYPE.BINOP), null); Operator o = new Operator(accept(Token.TYPE.BINOP), null);
e = new BinaryExpr(o, e, parseEExpression(), null); e = new BinaryExpr(o, e, parseEExpression(), null);
} }
@ -529,12 +540,14 @@ public class Parser {
} }
/** /**
* Equality * Relational
* @return
* @throws IOException * @throws IOException
*/ */
private Expression parseEExpression() throws IOException { private Expression parseRExpression() throws IOException {
Expression e = parseRExpression(); 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(">=")) {
Operator o = new Operator(accept(Token.TYPE.BINOP), null); Operator o = new Operator(accept(Token.TYPE.BINOP), null);
e = new BinaryExpr(o, e, parseRExpression(), null); e = new BinaryExpr(o, e, parseRExpression(), null);
} }
@ -542,49 +555,35 @@ public class Parser {
return e; 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 * Additive
* @return
* @throws IOException * @throws IOException
*/ */
private Expression parseAExpression() throws IOException { private Expression parseAExpression() throws IOException {
Expression e = parseMExpression(); Expression e = parseMExpression();
while(peek(1).spelling.equals("+") || peek(1).spelling.equals("-")) { while(peek(1).spelling.equals("+") || peek(1).spelling.equals("-")) {
Operator o = new Operator(accept(Token.TYPE.BINOP), null); Operator o = new Operator(accept(Token.TYPE.BINOP), null);
e = new BinaryExpr(o, e, parseMExpression(), null); e = new BinaryExpr(o, e, parseAExpression(), 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);
} }
return e; 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. * Sees what the next token is, caching the result.

View File

@ -32,9 +32,17 @@ public class Scanner {
switch(c) { switch(c) {
// Operators // Operators
case '*':
token = new Token(attr, Token.TYPE.BINOP);
break;
case '+': case '+':
case '*': if(peek('+')) throw new IOException();
token = new Token(attr, Token.TYPE.BINOP);
break;
case '-': case '-':
if(peek('-')) throw new IOException();
token = new Token(attr, Token.TYPE.BINOP); token = new Token(attr, Token.TYPE.BINOP);
break; break;

View File

@ -2,11 +2,9 @@
class PA2 { class PA2 {
public boolean c; public boolean c;
private static id[] test;
public static void main(String[] args) { public static void main(String[] args) {
if(x > 1) int x = -1 || 2 + (8 <= 8) == -6 && (9 > 10) * 3;
x = 1 + 2 * x;
else
b[3].a = 4;
} }
} }