From 617fa5c11772911aa07a83b7206713cfbdcc4dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gassmann?= Date: Sat, 12 Jun 2021 11:42:56 +0200 Subject: [~] Factors implemented --- calculator-javacc/Calculator.jj | 74 ++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 15 deletions(-) (limited to 'calculator-javacc') diff --git a/calculator-javacc/Calculator.jj b/calculator-javacc/Calculator.jj index 6cc78f5..9222654 100644 --- a/calculator-javacc/Calculator.jj +++ b/calculator-javacc/Calculator.jj @@ -1,15 +1,36 @@ -options{DEBUG_PARSER = true; DEBUG_TOKEN_MANAGER = true;} +options{ + //DEBUG_PARSER = true; DEBUG_TOKEN_MANAGER = true; + STATIC=false; +} PARSER_BEGIN(Calculator) +import java.util.Scanner; +import java.io.StringReader; + public class Calculator { - public static void main(String[] args) { - try{ - Calculator parser = new Calculator(System.in); - parser.Start(); - }catch (ParseException e){ - System.out.println(e.getMessage()); - } - } + + 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; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(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); + } + } + } } PARSER_END(Calculator) @@ -22,14 +43,37 @@ SKIP : { } TOKEN : { - | - | - | - ( ) + > + | + | + | + | + | + | + | + | + | + +} + + + +double factor() : +{ + Token t; + double i; +} +{ + t = + { + i = Double.parseDouble(t.image); + currentNumber += i; + return i; + } } -void Start() : { +void parse() : { } { - ()* + (factor())* } \ No newline at end of file -- cgit v1.2.3