簡體   English   中英

ANTLR4-使用Visitor將語法樹轉換為AST

[英]ANTLR4 - using Visitor to turn syntax tree into AST

我的任務是實現一個訪問者,該訪問者會將以下將算術表達式求值的語法轉換為AST:

grammar SmallC;


program :   exp
;

exp :   lexp ((op = '>' | op = '<' |op = '>=' | op = '<=' | op = '!=' | op = '==') lexp)?
;

lexp :  term ((op = '+'|op = '-') term)*
;

term:   factor((op = '%'| op = '\*'| op = '/') factor)*
;

factor  :   Number
;

Number  : [0-9]+
;    

當前,ANTLR為表達式“ 3 * 6”創建以下樹:

程序-> exp-> lexp->項->(factor * factor)-> Number->(3 * 6)

我想要的是這樣的:

程序->乘法(3,6)

我嘗試通過實現可遍歷樹並輸出某些數據結構的訪問者函數來開始使用此函數,但是它失敗了。 這是我的課程:

Main.Java

import org.antlr.v4.runtime.*;

public class Main {

public static void main(String[] args) {

ANTLRInputStream input = new ANTLRInputStream("3*6");
SmallCLexer lexer = new SmallCLexer(null);
lexer.setInputStream(input);

CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();

SmallCParser parser = new SmallCParser(null);
parser.setBuildParseTree(true);
parser.setTokenStream(tokens);

ParserRuleContext tree = parser.program();
MyVisitor visitor = new MyVisitor();
visitor.visit(tree);

}

}

MyVisitor.java

import org.antlr.v4.runtime.*;


public class MyVisitor extends SmallCBaseVisitor<SmallCNode> {


@Override public SmallCNode visitExp(SmallCParser.ExpContext ctx)  {
    if ( ctx.lexp().size() == 2) {
        SmallCLexp lhs = (SmallCLexp) ctx.lexp(0).accept(this);
        SmallCLexp rhs = (SmallCLexp) ctx.lexp(1).accept(this);
        String op = ctx.op.getText();
        return new SmallCExp(lhs,rhs,op);
    } else {
        visitLexp(ctx.lexp(0));
    }


}

@Override public SmallCNode visitLexp(SmallCParser.LexpContext ctx) {
    if ( ctx.term().size() == 2) {
        SmallCTerm lhs = (SmallCTerm) ctx.term(0).accept(this);
        SmallCTerm rhs = (SmallCTerm) ctx.term(1).accept(this);
        String op = ctx.op.getText();
        return new SmallCLexp(lhs,rhs,op);
    } else {
        visitTerm(ctx.term(0));
    }


}

@Override public SmallCNode visitTerm(SmallCParser.TermContext ctx) {
    if (ctx.factor().size() == 2) {
        SmallCFactor lhs = (SmallCFactor) ctx.factor(0).accept(this);
        SmallCFactor rhs = (SmallCFactor) ctx.factor(1).accept(this);
        String op = ctx.op.getText();
        return new SmallCTerm(lhs,rhs,op);
    } else {
        visitFactor(ctx.factor(0));
    }



}

@Override public SmallCNode visitFactor(SmallCParser.FactorContext ctx) {
        String fc = ctx.getText();
        return new SmallCFactor(fc);
}

這是我的節點類:

public class SmallCNode {

}


public class SmallCExp extends SmallCNode{
    SmallCLexp lhs;
    SmallCLexp rhs;
    String op;
    public SmallCExp(SmallCLexp lhs, SmallCLexp rhs, String op) {
        super();
        this.lhs = lhs;
        this.rhs = rhs;
        this.op = op;
    }
}

public class SmallCLexp extends SmallCNode {
    SmallCTerm lhs;
    SmallCTerm rhs;
    String op;
    public SmallCLexp(SmallCTerm lhs, SmallCTerm rhs, String op) {
        super();
        this.lhs = lhs;
        this.rhs = rhs;
        this.op = op;
    }
}

public class SmallCTerm extends SmallCNode{
    SmallCFactor lhs;
    SmallCFactor rhs;
    String op;
    public SmallCTerm(SmallCFactor lhs, SmallCFactor rhs, String op) {
        super();
        this.lhs = lhs;
        this.rhs = rhs;
        this.op = op;
}

}

public class SmallCFactor extends SmallCNode {
    String factor;

    public SmallCFactor(String factor) {
        super();
        this.factor = factor;
}

}

當我運行Main.java時,出現以下錯誤:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    This method must return a result of type SmallCNode

    at MyVisitor.visitExp(MyVisitor.java:8)
    at MyVisitor.visitExp(MyVisitor.java:1)
    at SmallCParser$ExpContext.accept(SmallCParser.java:151)
    at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visitChildren(AbstractParseTreeVisitor.java:70)
    at SmallCBaseVisitor.visitProgram(SmallCBaseVisitor.java:20)
    at SmallCParser$ProgramContext.accept(SmallCParser.java:103)
    at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:42)
    at Main.main(Main.java:20)

現在,我對這件事很陌生,我意識到可能很多我想念的東西。 我不知道如何從這里繼續,如果能得到我所缺少的東西的清單,我將不勝感激:)

您代碼中的許多else塊都不返回SmallCNode ,而必須返回。

它不應該是:

@Override public SmallCNode visitTerm(SmallCParser.TermContext ctx) {
if (ctx.factor().size() == 2) {
    SmallCFactor lhs = (SmallCFactor) ctx.factor(0).accept(this);
    SmallCFactor rhs = (SmallCFactor) ctx.factor(1).accept(this);
    String op = ctx.op.getText();
    return new SmallCTerm(lhs,rhs,op);
} else {
    visitFactor(ctx.factor(0));
}

但:

@Override public SmallCNode visitTerm(SmallCParser.TermContext ctx) {
if (ctx.factor().size() == 2) {
    SmallCFactor lhs = (SmallCFactor) ctx.factor(0).accept(this);
    SmallCFactor rhs = (SmallCFactor) ctx.factor(1).accept(this);
    String op = ctx.op.getText();
    return new SmallCTerm(lhs,rhs,op);
} else {
    return visitFactor(ctx.factor(0));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM