C 中 AST 打印的故障

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

我正在使用 C 编写编译器,当我打印 AST 时,它会打印部分代码两次。

打印AST的代码:

void show_tree(int indent, AST_Node* ptr){
    if(ptr == NULL){
        return;
    }
    for(int i=0;i<indent;i++){
        printf(" ");
    }
    switch(ptr->TYPE){
        case PROGRAM:
            printf("Program [%s]:\n",ptr->NODE.Prog.head);
            show_tree(indent+4,ptr->NODE.Prog.child);
            break;
        case FUNCTION_DECL:
            printf("Function Decl [%s]:\n",ptr->NODE.Func.name);
            show_tree(indent+4,ptr->NODE.Func.body);
            break;
        case BINOP:
            printf("Binary Operator [%d]:\n",ptr->NODE.BinOp.op);
            show_tree(indent+4,ptr->NODE.BinOp.left);
            show_tree(indent+4,ptr->NODE.BinOp.right);
            break;
        case RELOP:
            printf("Relative Operator [%d]:\n",ptr->NODE.RelOp.op);
            show_tree(indent+4,ptr->NODE.RelOp.left);
            show_tree(indent+4,ptr->NODE.RelOp.right);
            break;
        case RETURN:
            printf("Return:\n");
            show_tree(indent+4,ptr->NODE.Return.val);
            break;
        case UNOP:
            printf("Unary Operator [%d]\n",ptr->NODE.UnOp.op);
            show_tree(indent+4,ptr->NODE.UnOp.val);
            break;
        case CONSTANT:
            printf("Constant [%d]\n",ptr->NODE.Const.Tok_Value);
            break;
        case VARIABLE:
            printf("Variable [%s]\n",ptr->NODE.Variable.Tok_Value);
            break;
        case ASSIGN:
            printf("Assignment:\n");
            show_tree(indent+4,ptr->NODE.AssignOp.var);
            if(ptr->NODE.AssignOp.expr != NULL){
                show_tree(indent+4,ptr->NODE.AssignOp.expr);
            }
            break;
        case IF:
            printf("If Condition:\n");
            show_tree(indent+4,ptr->NODE.IfBlock.cond_block);

            show_tree(indent+4,ptr->NODE.IfBlock.stmnt_block);

            if(ptr->NODE.IfBlock.else_block){
                show_tree(indent+4,ptr->NODE.IfBlock.else_block);
            }
            break;

    }
    if(ptr->SIBLING != NULL){
        show_tree(indent,ptr->SIBLING);
    }
}

司机代码:

int main(){
    BASE_TOKEN = GiveTok();       // Parent Node of the lexemes
    BASE_AST_NODE = GiveNode();     // Parent Node of the AST
    FILE_NAME = "test.txt";


    printf("BASE_TOKEN addr: %p \n", BASE_TOKEN);
    printf("BASE_AST_NODE addr: %p \n", BASE_AST_NODE);
    printf("GLOBAL_TABLE addr: %p \n\n", GLOBAL_TABLE_HEAD);

    
    
    Set_Up();
    TOKENIZER();
    //show_tokens(BASE_TOKEN);

    PARSER();    // When this executes successfully, it shows [PARSER ENDS]
    show_tree(0,BASE_AST_NODE);

    //ShowGlobal();
    
    //BUILD(BASE_AST_NODE);

    int x;
    printf("\nput input: "); //for verifying the code executed successfully
    scanf("%d",&x);
    system("pause");
    return 0;
}

Here is the glitch, as PARSER ENDS and the tree is partially printed twice

我尝试运行不同的测试用例,因为它们可能存在一些解析错误,但问题仍然存在。

这是有问题的测试用例。

int test(){
    int a;
    int b = 10;
    int c;
    c = a+10*b;
}
int test2(){
    int x16;
    int x17;
}

执行顺利:

int test(){
    int c;
    c = a+10*b;
}
int test2(){
    int x16;
    int x17;
}
c compiler-construction
© www.soinside.com 2019 - 2024. All rights reserved.