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 ( 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.

View File

@ -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;

View File

@ -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;
}
}