如何使用Java在控制台中打印正确的解析树?

问题描述 投票:0回答:1

我正在编写一个程序,使用 java 模拟 6 个编译器阶段。目前,停留在第二阶段语法分析器的解析树打印上。经过一些调试我已经知道我的问题是什么了。父节点和子节点是正确的,但视觉上显示的深度错误。

输入:

z=y*y+x*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
            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("");
            }
        }
    }

Output

如果 img 打不开,这是我当前的输出:

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
java compiler-construction parse-tree
1个回答
0
投票

我想通了。虽然我认为本来可以有更好的解决方案,但这就是我设法想出的。 因为只有乘法和除法运算符可以达到相同的深度,所以我专门为它们制定了一个条件。首先检查两个操作是否都是 * 或 /,如果是,则打印 / \ 分支,然后打印操作,然后打印其所有子级的分支,即 4 所以 / \ / \ 然后使用嵌套循环将其两个子级打印在同一条线,其他一切正常。就是这样。之后,只需调整空间即可获得我正在寻找的输出。

     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("*") ||
                        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);
                }
            }
        }
    }
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
© www.soinside.com 2019 - 2024. All rights reserved.