在 Java 中递归地创建/解析子级

发布于 2024-10-21 23:27:52 字数 1438 浏览 0 评论 0原文

我正在尝试递归地解析节点树,但我添加的每个子节点都会计算出最后一个值。

示例:解析 2+3 应该生成一个计算结果为 2 的节点和一个计算结果为 3 的节点,但我却得到了计算结果为 3 的 2 个节点。

根据调试器的说法,subBefore 是“2”,subAfter 是“3”,因为它应该是这样的是。

构造一个新的操作将参数添加为子项。为什么我最终会得到评估为相同值的孩子?

代码如下。 Term 几乎是一样的东西,但检查 * 和 / 而不是 + 和 -

Full code:

public class Operation extends ASTNode {

static char op; 

  private Operation(ASTNode... n) { super(n); }



  public static Operation parse(String s) {

      String str = s.trim();

    if(Term.parse(str) != null) return new Operation(Term.parse(str));
    else {

// now make substrings

    int lastOpPlus = str.lastIndexOf('+');
    int lastOpMinus = str.lastIndexOf('-');

    if (lastOpPlus > lastOpMinus) {
        op = '+';

        String subAfter = str.substring(lastOpPlus+1);
        String subBefore = str.substring(0, lastOpPlus);

        if(Operation.parse(subBefore)!=null && Term.parse(subAfter) != null) {
            return new Operation(Operation.parse(subBefore), Term.parse(subAfter));
        }

    }

    return null;
    }
  }

  public double eval(java.util.Map<String,Double> symtab) {
    // first check if state is okay checkState();

      if(arity() > 1) { 
          return (getChild(0).eval(symtab)+getChild(1).eval(symtab));
      }
      //System.out.println(arity());
      //getChild(1).eval(symtab);
      else{ 
          return getChild(0).eval(symtab);
      }
  }

}

I'm trying to parse a tree of nodes recursively, but every child I add evaluates to the last value.

Example: parsing 2+3 should make a node that evaluates 2 and a node that evaluates 3, but instead I get 2 nodes that evaluate to 3.

According to the debugger, subBefore is "2" and subAfter is "3" as it should be.

Constructing a new Operation adds the arguments as children. Why do I end up with children that evaluate to the same value?

Code is below. Term is pretty much the same thing but checks for * and / instead of + and -

Full code:

public class Operation extends ASTNode {

static char op; 

  private Operation(ASTNode... n) { super(n); }



  public static Operation parse(String s) {

      String str = s.trim();

    if(Term.parse(str) != null) return new Operation(Term.parse(str));
    else {

// now make substrings

    int lastOpPlus = str.lastIndexOf('+');
    int lastOpMinus = str.lastIndexOf('-');

    if (lastOpPlus > lastOpMinus) {
        op = '+';

        String subAfter = str.substring(lastOpPlus+1);
        String subBefore = str.substring(0, lastOpPlus);

        if(Operation.parse(subBefore)!=null && Term.parse(subAfter) != null) {
            return new Operation(Operation.parse(subBefore), Term.parse(subAfter));
        }

    }

    return null;
    }
  }

  public double eval(java.util.Map<String,Double> symtab) {
    // first check if state is okay checkState();

      if(arity() > 1) { 
          return (getChild(0).eval(symtab)+getChild(1).eval(symtab));
      }
      //System.out.println(arity());
      //getChild(1).eval(symtab);
      else{ 
          return getChild(0).eval(symtab);
      }
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2024-10-28 23:27:52

如果没有看到 Operation.parseTerm.parseOperation 构造函数的代码,很难判断,但我的猜测是您正在共享某处的缓冲区或变量。

另外,您要执行两次解析工作。像这样重新组织代码可能会很好:

Object before = Operation.parse(subBefore); // a more specific type, not Object
Object after = Term.parse(subAfter);
if (before != null && after != null) {
    return new Operation(before, after);
}

Hard to tell without seeing the code for Operation.parse, Term.parse, and the Operation constructor, but my guess is that you are sharing a buffer or variable somewhere.

On a separate note, you're doing the parse work twice. It might be good to reorganize your code like this:

Object before = Operation.parse(subBefore); // a more specific type, not Object
Object after = Term.parse(subAfter);
if (before != null && after != null) {
    return new Operation(before, after);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文