ANTLR4 - 使用Visitor将语法树转换为AST

时间:2015-02-08 15:19:23

标签: java antlr antlr4

我的任务是实现一个访问者,它将以下用于计算算术表达式的语法转换为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为表达式创建了以下树&#34; 3 * 6&#34;:

程序 - &gt; exp - &gt; lexp - &gt;术语 - &gt; (因子*因子) - &gt;数字 - &gt; (3 * 6)

我想要的是这样的:

程序 - &gt;乘法(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)

现在,我对这件事情很陌生,我意识到我可能会失踪很多。我不知道如何从这里继续,我真的很感激,如果我能得到一些我缺少的东西:)

1 个答案:

答案 0 :(得分:1)

代码中的许多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));
}
相关问题