我的任务是实现一个访问者,该访问者将把以下计算算术表达式的语法转换为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->term->(因子*因子)->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));
}