【问题标题】:ANTLR4 - using Visitor to turn syntax tree into ASTANTLR4 - 使用访问者将语法树转换为 AST
【发布时间】:2015-02-08 15:19:23
【问题描述】:

我的任务是实现一个访问者,它将以下计算算术表达式的语法转换为 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”创建以下树:

program -> exp -> lexp -> term -> (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)

现在,我对这件事很陌生,我意识到我可能遗漏了很多东西。我不知道如何从这里继续,如果我能得到一份我遗漏的东西的清单,我将不胜感激:)

【问题讨论】:

    标签: java antlr antlr4


    【解决方案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));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多