aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--calculator-javacc/Calculator.jj136
1 files changed, 106 insertions, 30 deletions
diff --git a/calculator-javacc/Calculator.jj b/calculator-javacc/Calculator.jj
index 9222654..cc22b0a 100644
--- a/calculator-javacc/Calculator.jj
+++ b/calculator-javacc/Calculator.jj
@@ -1,48 +1,40 @@
options{
//DEBUG_PARSER = true; DEBUG_TOKEN_MANAGER = true;
- STATIC=false;
}
PARSER_BEGIN(Calculator)
-import java.util.Scanner;
-import java.io.StringReader;
+import java.util.HashMap;
public class Calculator {
- private static double currentNumber;
-
- public static double fromString(String stringToParse) throws ParseException{
- currentNumber = 0.0;
-
- Calculator parser = new Calculator(new StringReader(stringToParse));
- parser.parse();
- return currentNumber;
- }
+ private static HashMap variables = new HashMap();
public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
+ Calculator parser = new Calculator(System.in);
while (true){
System.out.print("Type your expression: ");
- String expression = scanner.nextLine();
try{
- System.out.println("Returned: " + fromString(expression));
- }catch (Throwable t){
- System.out.println(t);
+ double value = parser.evaluateNextLine();
+ System.out.println("Returned: " + value);
+ }catch (ParseException e){
+ System.out.println(e.getMessage());
+ try{
+ parser.passStatement(); // wiping the tokens of the malformed expression
+ }catch (ParseException ex){
+ System.out.println(ex.getMessage());
+ }
}
}
}
}
PARSER_END(Calculator)
-/**
-* For now only using the Hello World grammar
-*/
-
SKIP : {
- " "|"\t"|"\n"|"\r"
+ " "|"\t"
}
TOKEN : {
+ <EOL: "\n"|"\r">|
<PAL: "(">|
<PAR: ")">|
<ADD: "+">|
@@ -56,24 +48,108 @@ TOKEN : {
<ID: (["a"-"z"]|["A"-"Z"])+>
}
+double statement() :
+{
+ Token id;
+ double i = 0.0;
+}
+{
+ <END> { System.exit(0); }|
+ i=expression() { return i; }|
+ <LET> id=<ID> <EQU> i=expression()
+ {
+ variables.put(id.image,i); //Storing the variable
+ return i;
+ }
+}
+
+double expression() :
+{
+ Token op = null;
+ double l = 0.0;
+ double r = 0.0;
+}
+{
+ l=term() ((op=<ADD> | op=<SUB>) r=term()
+ { // folding everything in the l variable
+ if (op != null){
+ if (op.image.equals("+"))
+ l += r;
+ else
+ l -= r;
+ }
+
+ }
+ )*
+ { //returning the parsed value
+ return l;
+ }
+}
+
+double term() :
+{
+ Token op = null;
+ double l = 0.0;
+ double r = 0.0;
+}
+{
+ l=factor() ((op=<DIV> | op=<MUL>) r=factor()
+ { // folding everything in the l variable
+ if (op != null){
+ if (op.image.equals("/"))
+ l /= r;
+ else
+ l *= r;
+ }
+ }
+ )*
+ { //returning the parsed value
+ return l;
+ }
+}
double factor() :
{
- Token t;
- double i;
+ Token t = null;
+ Token id = null;
+ double i = 0.0;
}
{
- t = <NUM>
+ (t=<NUM> | id=<ID> | <PAL> (i=expression())? <PAR>)
{
- i = Double.parseDouble(t.image);
- currentNumber += i;
+ if(id != null){
+ Object var = variables.get(id.image);
+ if (var != null)
+ i = (double) var;
+ else
+ throw new ParseException("'" + id.image + "' is not yet defined.");
+ }else if(t != null)
+ i = Double.parseDouble(t.image);
return i;
- }
+ }|
+ <SUB> (t=<NUM> | id=<ID> | <PAL> (i=expression())? <PAR>)
+ {
+ if(id != null){
+ i = (double)variables.get(id.image);
+ }else if(t != null)
+ i = Double.parseDouble(t.image);
+ return i*(-1);
+ }
}
-void parse() : {
+double evaluateNextLine() :
+{
+ double result = 0.0;
+}
+{
+ (result=statement())? <EOL> {return result;}|
+ <EOF>{System.out.println("\nBuffer was closed. Exiting."); System.exit(0);}
}
+
+void passStatement() :
+{}
{
- (factor())* <EOF>
+ <EOL>|
+ <EOF>{System.out.println("\nBuffer was closed. Exiting."); System.exit(0);}
} \ No newline at end of file