Commit c5293a5b authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

prednaska7

parent 307b3279
grammar Expr;
prog : expr;
expr : expr op=('*'|'/') expr #Mul
| expr op=('+'|'-') expr #Add
| NUMBER #Number
| '(' expr ')' #Paren
| IDENT #Ident
;
NUMBER : [0-9]+;
IDENT : [a-zA-Z]+;
WS : [ \r\t\n]+ -> skip;
\ No newline at end of file
WS : [ \r\t\n]+ -> skip;
package antlr4.examples.calc;
import antlr4.examples.calc.model.Expression;
import antlr4.examples.calc.parser.ASTBuilderVisitor;
import antlr4.examples.calc.parser.EvalVisitor;
import examples.calc.ExprLexer;
import examples.calc.ExprParser;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
ExprLexer lexer = new ExprLexer(CharStreams.fromString("10 + 20 * 3 / 2 - 10"));
CommonTokenStream tokens = new CommonTokenStream(lexer);
ExprParser parser = new ExprParser(tokens);
ParseTree tree = parser.expr();
// EvalVisitor visitor = new EvalVisitor();
// System.out.println(visitor.visit(tree));
ASTBuilderVisitor visitor = new ASTBuilderVisitor();
Expression expression = visitor.visit(tree);
System.out.println(expression.eval());
}
}
package antlr4.examples.calc.model;
public class Addition extends BinaryOperation {
public Addition(Expression expression1, Expression expression2) {
super(expression1, expression2);
}
//eval [[ expression1 '+' expression2 ]] = eval [[ expression1 ]] + eval [[ expression2 ]]
@Override
public int eval() {
return getExpression1().eval() + getExpression2().eval();
}
}
package antlr4.examples.calc.model;
public abstract class BinaryOperation extends Expression {
private final Expression expression1;
private final Expression expression2;
public BinaryOperation(Expression expression1, Expression expression2) {
this.expression1 = expression1;
this.expression2 = expression2;
}
public Expression getExpression1() {
return expression1;
}
public Expression getExpression2() {
return expression2;
}
}
package antlr4.examples.calc.model;
public class Division extends BinaryOperation {
public Division(Expression expression1, Expression expression2) {
super(expression1, expression2);
}
//eval [[ expression1 '/' expression2 ]] = eval [[ expression1 ]] / eval [[ expression2 ]]
@Override
public int eval() {
return getExpression1().eval() / getExpression2().eval();
}
}
package antlr4.examples.calc.model;
public abstract class Expression {
//eval :: Expression -> int
public abstract int eval();
}
package antlr4.examples.calc.model;
public class Multiplication extends BinaryOperation {
public Multiplication(Expression expression1, Expression expression2) {
super(expression1, expression2);
}
//eval [[ expression1 '*' expression2 ]] = eval [[ expression1 ]] * eval [[ expression2 ]]
@Override
public int eval() {
return getExpression1().eval() * getExpression2().eval();
}
}
package antlr4.examples.calc.model;
public class Number extends Expression {
private final int value;
// public Number(String text) {
// value = Integer.parseInt(text);
// }
public Number(int value) {
this.value = value;
}
public int getValue() {
return value;
}
//eval [[ value ]] = N[[ value ]]
@Override
public int eval() {
return value;
}
}
package antlr4.examples.calc.model;
public class Subtraction extends BinaryOperation {
public Subtraction(Expression expression1, Expression expression2) {
super(expression1, expression2);
}
//eval [[ expression1 '-' expression2 ]] = eval [[ expression1 ]] - eval [[ expression2 ]]
@Override
public int eval() {
return getExpression1().eval() - getExpression2().eval();
}
}
package antlr4.examples.calc.parser;
import antlr4.examples.calc.model.Number;
import antlr4.examples.calc.model.*;
import examples.calc.ExprBaseVisitor;
import examples.calc.ExprParser;
public class ASTBuilderVisitor extends ExprBaseVisitor<Expression> {
@Override
public Expression visitNumber(ExprParser.NumberContext ctx) {
return new Number(Integer.parseInt(ctx.NUMBER().getText()));
}
@Override
public Expression visitAdd(ExprParser.AddContext ctx) {
Expression expression1 = visit(ctx.expr(0));
Expression expression2 = visit(ctx.expr(1));
return "+".equals(ctx.op.getText()) ? new Addition(expression1, expression2) : new Subtraction(expression1, expression2);
}
@Override
public Expression visitMul(ExprParser.MulContext ctx) {
Expression expression1 = visit(ctx.expr(0));
Expression expression2 = visit(ctx.expr(1));
return "*".equals(ctx.op.getText()) ? new Multiplication(expression1, expression2) : new Division(expression1, expression2);
}
}
package antlr4.examples.calc.parser;
import examples.calc.ExprBaseVisitor;
import examples.calc.ExprParser;
// E :: Expr -> Integer
public class EvalVisitor extends ExprBaseVisitor<Integer> {
//E[[ N ]] = N[[ n ]]
@Override
public Integer visitNumber(ExprParser.NumberContext ctx) {
return Integer.valueOf(ctx.NUMBER().getText());
}
//E[[ e0 '+' e1 ]] = E[[ e0 ]] + E[[ e1 ]]
//E[[ e0 '-' e1 ]] = E[[ e0 ]] - E[[ e1 ]]
@Override
public Integer visitAdd(ExprParser.AddContext ctx) {
Integer i1 = visit(ctx.expr(0));
Integer i2 = visit(ctx.expr(1));
return "+".equals(ctx.op.getText()) ? Integer.valueOf(i1 + i2) : Integer.valueOf(i1 - i2);
}
//E[[ e0 '*' e1 ]] = E[[ e0 ]] * E[[ e1 ]]
//E[[ e0 '/' e1 ]] = E[[ e0 ]] / E[[ e1 ]]
@Override
public Integer visitMul(ExprParser.MulContext ctx) {
Integer i1 = visit(ctx.expr(0));
Integer i2 = visit(ctx.expr(1));
return "*".equals(ctx.op.getText()) ? Integer.valueOf(i1 * i2) : Integer.valueOf(i1 / i2);
}
}
package sk.tuke.dsl.testlang.parser;
public class AnswerCountListener extends Test2BaseListener {
private int answerCount;
@Override
public void exitIncorrect(Test2Parser.IncorrectContext ctx) {
answerCount++;
}
@Override
public void exitCorrect(Test2Parser.CorrectContext ctx) {
answerCount++;
}
public int getAnswerCount() {
return answerCount;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment