JavaCC-将数学表达式解析为类结构 [英] JavaCC - Parse math expressions into a class structure

查看:114
本文介绍了JavaCC-将数学表达式解析为类结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这种语法来计算数学表达式:

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

我与Java或JavaCC没有太多关系,所以您能告诉我如何为此编写语法吗?

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

PS:我正在使用Java ME.

PS: I'm using Java ME.

推荐答案

eWolf,

尝试使用JJTree(它是JavaCC的预处理器),并自动引入Java代码,该代码为已解析的输入创建抽象语法树.

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

只需访问 https://javacc.dev.java.net/并找到JJTree手册.

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

这篇关于JavaCC-将数学表达式解析为类结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆