aboutsummaryrefslogtreecommitdiff
path: root/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java
diff options
context:
space:
mode:
authorMaël Gassmann <mael.gassmann@students.bfh.ch>2021-06-11 17:28:55 +0200
committerMaël Gassmann <mael.gassmann@students.bfh.ch>2021-06-11 17:28:55 +0200
commit98ddff97ec1d092b18ef2d176e83bd92f9671e03 (patch)
treeaf1457e05063421dc2860e6c033e58b28f9a8e4e /calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java
parent49f30529d9cd2ed62902079a45d4d3c1f45afbda (diff)
[+] Added StatementParser, [~] Restructured the Parsers
Diffstat (limited to 'calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java')
-rw-r--r--calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java180
1 files changed, 10 insertions, 170 deletions
diff --git a/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java b/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java
index f96353e..3ec0194 100644
--- a/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java
+++ b/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java
@@ -10,21 +10,27 @@ public class ExpressionParser extends Parser {
protected ArrayList<Parser> parsers = new ArrayList<>();
protected ArrayList<Token> ops = new ArrayList<>();
+ private boolean isParenthesised = false;
public ExpressionParser(CalculatorLexer cl) {
this.cl = cl;
parse();
}
- private ExpressionParser(CalculatorLexer cl, Token lastToken) {
+ protected ExpressionParser(CalculatorLexer cl, Token lastToken, boolean isParenthesised) {
this.cl = cl;
this.lastToken = lastToken;
- parse();
+ this.isParenthesised = isParenthesised; //If an expression starts with the lastToken set as '(' it could mean that the Expression first term will be a parenthesised expression
+ parse(); //But it could also mean that this Expression is the parenthesised one, that is why this variable is required.
}
@Override
protected void parse() {
- Token token = cl.nextToken();
+ Token token;
+ if(lastToken != null && !isParenthesised)
+ token = lastToken;
+ else
+ token = cl.nextToken();
Parser l = null;
Parser r = null; // left and right expressions
Token op = null; // operation of the current l and right expressions
@@ -58,6 +64,7 @@ public class ExpressionParser extends Parser {
this.ops.add(op);
} // Then the '-' is the start of a new expression and not an operation
case Token.PAL:
+ case Token.ID:
case Token.NUM:
if (l == null) {
l = new TermParser(cl, token);
@@ -104,171 +111,4 @@ public class ExpressionParser extends Parser {
}
return result;
}
-
- private class TermParser extends ExpressionParser {
-
- public TermParser(CalculatorLexer cl, Token lastToken) {
- super(cl, lastToken);
- }
-
- @Override
- protected void parse() {
- Token token = lastToken;
- lastToken = null;
- Parser l = null;
- Parser r = null; // left and right expressions
- Token op = null; // operation of the current l and right expressions
-
- loop:
- while (token != null && token.type != Token.EOL) {
- switch (token.type) {
- case Token.ADD:
- if (op != null) {
- lastToken = token;
- break loop; // going here on case 'n/+...' or 'n*+...' since '+' would be invalid
- }
- case Token.SUB:
- case Token.NUM:
- case Token.PAL:
- if (l == null) {
- l = new FactorParser(cl, token);
- this.parsers.add(l);
- token = l.lastToken;
- continue loop;
- } else if (op != null) {
- r = new FactorParser(cl, token);
- this.parsers.add(r);
- token = r.lastToken;
- lastToken = token; // in case we finished
- continue loop;
- } else {
- lastToken = token;
- break loop; // going here on case 'n+...' or 'n-...' since '+' and '-' are part of the parent expression
- }
- case Token.MUL:
- case Token.DIV:
- if (l != null && op == null) { // if the '*' or '/' signs are between two Factors (op)
- op = token;
- this.ops.add(op);
- } else if (l != null && r != null) { // if we already found a 'l op r' we roll
- l = r;
- r = null;
- op = token;
- this.ops.add(op);
- } else
- throw new ParserException("Two '" + token.str + "' in a row were found.");
- break;
- case Token.PAR: // Going as high as possible --> possibly the end of an expression created by a Factor :)
- lastToken = token;
- break loop;
- default:
- //TODO - Replace default by each individual case
- throw new ParserException("Malformed Expression.");
- }
- token = cl.nextToken();
- }
- if (op != null && r == null)
- throw new ParserException("Missing expression after last operation '"+op.str+"'");
- }
-
- @Override
- public double getValue(){
- double result = 0.0;
- if (!this.parsers.isEmpty()) {
- result = this.parsers.get(0).getValue();
-
- for (int i = 1; i < this.parsers.size(); i++) {
- switch (this.ops.get(i-1).type){
- case Token.MUL:
- result *= this.parsers.get(i).getValue();
- break;
- case Token.DIV:
- result /= this.parsers.get(i).getValue();
- break;
- }
- }
- }
- return result;
- }
-
- private class FactorParser extends Parser {
-
- ExpressionParser expression; // only used when parenthesis are found
- boolean isNegative = false;
- boolean somethingWasParsed = false;
-
- public FactorParser(CalculatorLexer cl, Token lastToken) {
- this.cl = cl;
- this.lastToken = lastToken;
- parse();
- }
-
- @Override
- protected void parse() {
- Token token = lastToken;
- lastToken = null;
- loop:
- while (token != null && token.type != Token.EOL) {
- switch (token.type) {
- case Token.MUL:
- case Token.DIV:
- case Token.ADD:
- if (somethingWasParsed) { // finished parsing the Factor
- lastToken = token; // sending the token to the parent
- break loop;
- } else
- throw new ParserException("Empty expression before '" + token.str + "'.");
- case Token.SUB:
- if (!somethingWasParsed)
- if (!isNegative)
- isNegative = true;
- else
- throw new ParserException("A factor cannot contain two '-'.");
- else { // Meaning we could have parsed '-4' or '4' and '-' is the next digit which is op for the overall expression
- lastToken = token;
- break loop;
- }
- break;
- case Token.NUM:
- if (!somethingWasParsed) {
- value = token.value;
- somethingWasParsed = true;
- } else
- throw new ParserException("Missing operator between the two Factors.");
- break;
- case Token.PAL:
- if (!somethingWasParsed) {
- expression = new ExpressionParser(cl, token);
- if (expression.lastToken.type != Token.PAR)
- throw new ParserException("No matching closing parenthesis were found.");
- somethingWasParsed = true;
- } else
- throw new ParserException("Missing operator between the two Factors.");
- break;
- case Token.PAR: // Going as high as possible --> possibly the end of an expression created by a Factor :)
- if (somethingWasParsed)
- lastToken = token;
- else throw new ParserException("Missing expression before closing the parenthesis.");
- break loop;
- default:
- //TODO - Replace default by each individual case
- throw new ParserException("Malformed Expression.");
- }
- token = cl.nextToken();
- }
- }
-
- @Override
- public double getValue(){
- if (expression != null)
- if (isNegative)
- return expression.getValue()*(-1);
- else
- return expression.getValue();
- if (isNegative)
- return value*(-1);
- return value;
- }
- }
- }
}