views:

708

answers:

1

I'm using this grammar to calculate math expressions:

    // Konfiguration (JavaCC-Manual konsultieren)
options {
    STATIC = true; // alle Parser-operationen sind static
    // verwende zwei Token um zu entscheiden, was passieren soll
    LOOKAHEAD = 2;
}

// hier beginnt unsere Parser Klasse ("MathParse")
PARSER_BEGIN(MathParse)
// hier kann ganz normaler Java-Code verwendet werden
public class MathParse {
    public static void main(String[] args) {
     // auch ein statischer Parser muss initiiert werden
     // - allerdings nur einmal
     MathParse parser = new MathParse(System.in);
     try {
      System.out.println(parser.parse());
     } catch(ParseException e) {
      e.printStackTrace();
     }
    }

    // die Parser-Methoden werden automatisch hinzugefügt
}
PARSER_END(MathParse)

// Diese Zeichen ignorieren wir
// SKIP : { $regex$ }
SKIP : { " " | "\t" }

// Jetzt definieren wir unsere Token

// TOKEN : { < $tokenname$ : $regex$ >}
// "NUMBER" entspricht einer unbegrenzten Anzahl (min. 1) an Zahlen
// (von 0 bis 9)
TOKEN : { < NUMBER : (["0"-"9"])+ ("." (["0"-"9"])+)? > }
TOKEN : { < EOL : "\n" > }

// Und fröhlich weiter mit der tatsächlichen Syntax
double parse() : {
    double value;
}
{
    value=expr()
    (<EOF> | <EOL>)  { return value; }
}

double expr() : {
    double x;
    double y;
}
{
    x=term()
    (
     "+" y=expr() { x += y; }
    |
     "-" y=expr() { x -= y; } 
    )*
    { return x; }
}

double term() : {
    double x;
    double y;
}
{
    x=value()
    (
     "*" y=term() { x *= y; }
     |
     "/" y=term() { x /= y; }
    )*
    { return x; }
}

double value() : {
    double value;
}
{
    "-" value=number() { return -value; }
    |
    value=number()  { return value; }
}

double number() : {
    Token t;
    double value;
}
{
    t=<NUMBER>  { return Double.parseDouble(t.image); }
    |
    "(" value=expr() ")" { return value; }
}

That works fine. But now I don't want to get a number as the result, but a class structure representing the term. I thought of something like this:

public abstract class Expression {
    public abstract double calculate();
}

public class NumberExpression extends Expression {
    public double Value;

    public NumberExpression(double value) {
        this.Value = value;
    }

    public double calculate() {
        return this.Value;
    }
}

public class ComplexExpression extends Expression {
    public Operator Operator;
    public Vector SubExpressions;

    public double calculate() {
        double result = ((Expression)this.SubExpressions.elementAt(0)).calculate();

        for (int i = 1; i < this.SubExpressions.size(); ++i)
            result = this.Operator.calculate(result, ((Expression)this.SubExpressions.elementAt(i)).calculate());

        return result;
    }
}

public abstract class Operator {
    public abstract String getOperator();
    public abstract double calculate(double x, double y);
}

I didn't have much to do with Java or JavaCC, so can you tell me how to write a grammar for that?

PS: I'm using Java ME.

A: 

eWolf,

Try using JJTree which is a preprocessor for JavaCC and automatically introduces java code that creates Abstract Syntax Trees for parsed input.

Just visit https://javacc.dev.java.net/ and find the JJTree manual.