You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java编译器语法分析:如何在控制台正确打印语法分析树?

语法分析树打印问题(Java编译器模拟)

我用Java编写模拟编译器6个阶段的程序,目前卡在第二阶段——语法分析器的语法分析树打印环节。已调试确认节点的父子关系正确,但视觉显示的深度存在错误。输入内容为z=y*y+x*3,由于递归逻辑,当遇到带有子节点的*符号时,程序会先打印其子节点,再打印同为*的兄弟节点,导致显示不符合预期。

初始displayParseTree方法代码

public static void displayParseTree(ParseTreeNode node, int depth) {
    if (node != null) {
        StringBuilder indent = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            indent.append(" ");
        }

        //if node has children print / -> child1  \ -> child2
        System.out.print(indent.toString() + node.token.getValue());
        // Check if the node has any children.
        if (!node.children.isEmpty()) {

            System.out.println("");
            
            System.out.print("/  ");
            System.out.println(" \\");

            // Print the left child node.
            displayParseTree(node.children.get(0), depth + 1);

            // Print the right child node.
            displayParseTree(node.children.get(1), depth + 1);
            System.out.println("");
        }
    }
}

当前输出

Please enter the source code: 
z=y*y+x*3
Source Form: z=y*y+x*3
Math Form: z=yxy+xx3
Lexical Analyzer: id1=id2*id2+id3*3
Syntax analysis completed. The input is valid.

=
/   \
 id1 +
/   \
  *
/   \
   id2   id2
  *
/   \
   id3   3

期望输出样式

=
  / \
id1   +
    /   \
 *       *
/   \   /  \
id2 id2 id3  3

硬编码临时实现

我通过硬编码实现了部分效果(并不满意),暂时先这样,直到找到更好的方法。以下是硬编码后的代码:

public static void displayParseTree(ParseTreeNode node, int depth) {
    if (node != null) {
        StringBuilder indent = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            indent.append(" ");
        }

//if node has children print / -> child1  \ -> child2
        if (node.token.getValue().equals("=")){
            System.out.println("  " + indent.toString() + node.token.getValue());
        }

        // Check if the node has any children.
        if (!node.children.isEmpty()) {

            if (node.children.get(0).token.getValue().equals("*") && node.children.get(1).token.getValue().equals("*")) {
                System.out.print("/  ");
                System.out.println(" \\");

                for (int i = 0; i < 2; i++) {
                    System.out.print(node.children.get(i).token.getValue() + "  ");
                }
                System.out.println("");

                System.out.print("/  ");
                System.out.print(" \\   ");
                System.out.print("  /  ");
                System.out.println(" \\");

                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < 2; j++)
                    System.out.print(node.children.get(i).children.get(j).token.getValue() + "  ");
                }
                System.out.println("");
            }
            else {
                // Print the left child node.
                System.out.print("/  ");
                System.out.println(" \\");

                for (int i = 0; i < 2; i++) {
                    System.out.print(node.children.get(i).token.getValue() + "  ");
                }
                System.out.println("");

                displayParseTree(node.children.get(0), depth + 1);

                // Print the right child node.
                displayParseTree(node.children.get(1), depth + 1);
            }
        }
    }
}

硬编码后的输出

=
    /   \
    id1  +  
    /   \
    *  *  
    /   \     /   \
    id2  id2  id3  3

内容的提问来源于stack exchange,提问作者Seifeldin Elkasrawy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 01:52:19