dimanche 22 février 2015

ANTLR4 Exrpression evaluator error



I wanted to try this sample code out but i cannot get it to run because i am having compiling errors. I also added an image of my eclipse.


enter image description here


which whos where the errors are.


Here is the Grammar:



grammar Expr;
eval
: expr EOF
;
expr
: expr '*' expr #mult_expr
| expr '/' expr #div_expr
| expr '+' expr #add_expr
| expr '-' expr #min_expr
| '(' expr ')' #par_expr
| NUMBER #num_expr
| IDENTIFIER #id_expr
;
MULT : '*';
DIV : '/';
ADD : '+';
MIN : '-';
OPAR : '(';
CPAR : ')';
NUMBER
: [0-9]+ ('.' [0-9]*)?
| '.' [0-9]+
;
IDENTIFIER
: [a-zA-Z_] [a-zA-Z_0-9]*
;
SPACES
: [ \t\n\r]+ -> skip
;


Here is the EvalVisitor.Java class i am having problems with, its says: The method visit(ExprParser.ExprContext) is undefined for the type EvalVisitor.



package nl.bigo.antlr4template;
import org.antlr.v4.runtime.misc.NotNull;
import java.util.Map;
public class EvalVisitor extends ExprBaseVisitor<Double> {
private final Map<String, Double> variables;
public EvalVisitor(Map<String, Double> variables) {
this.variables = variables;
}
public Double visitMin_expr(@NotNull ExprParser.Min_exprContext ctx) {
return this.visit(ctx.expr(0)) - this.visit(ctx.expr(1));
}
public Double visitId_expr(@NotNull ExprParser.Id_exprContext ctx) {
String id = ctx.IDENTIFIER().getText();
Double value = variables.get(id);
if (value == null) {
throw new RuntimeException("unknown variable: " + id);
}
return value;
}
public Double visitDiv_expr(@NotNull ExprParser.Div_exprContext ctx) {
return this.visit(ctx.expr(0)) / this.visit(ctx.expr(1));
}

public Double visitMult_expr(@NotNull ExprParser.Mult_exprContext ctx) {
return this.visit(ctx.expr(0)) * this.visit(ctx.expr(1));
}

public Double visitEval(@NotNull ExprParser.EvalContext ctx) {
return super.visit(ctx.expr());
}

public Double visitNum_expr(@NotNull ExprParser.Num_exprContext ctx) {
return Double.valueOf(ctx.NUMBER().getText());
}

public Double visitPar_expr(@NotNull ExprParser.Par_exprContext ctx) {
return super.visit(ctx.expr());
}

public Double visitAdd_expr(@NotNull ExprParser.Add_exprContext ctx) {
return this.visit(ctx.expr(0)) + this.visit(ctx.expr(1));
}
}


grammar Expr;
parse : definition+;
definition : VARIABLE '=' expression ';' ;
expression
: expression expression # Application
| '(' expression ')' # Bracket
| expression INFIX_OP expression # Infix
| PREFIX_OP '(' expression ')' # Prefix
| '{' (expression (',' expression)*)? '}' # Set
| '(' expression ',' expression ')' # Pair
| NUMBER # Number
| VARIABLE # Variable
| <assoc=right> expression '=' expression # Equality
;
INFIX_OP : 'in' | 'U' | 'I' | '\\';
PREFIX_OP : [@][a-z-]+;
VARIABLE : [x][0-9]+ ;
NUMBER : [0-9]+ ;
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
LINE_COMMENT : '#' ~[\r\n]* -> skip;



Aucun commentaire:

Enregistrer un commentaire