Skip to content
Snippets Groups Projects
Commit 4fe3f000 authored by Arnaud LABOUREL's avatar Arnaud LABOUREL
Browse files

corrected expression grammar

parent b2f15981
No related branches found
No related tags found
No related merge requests found
......@@ -7,9 +7,10 @@ package sample;
expression: expression PLUS expression # Addition
| expression TIMES expression # Multiplication
| INT # Integer
| '(' expression ')' # ParenthesedExpression ;
| OPENING_PARENTHESIS expression CLOSING_PARENTHESIS # InParenthesesExpression ;
OPENING_PARENTHESIS: '(';
CLOSING_PARENTHESIS: ')';
PLUS : '+';
TIMES : '*';
INT : [0-9]+ ;
\ No newline at end of file
......@@ -5,6 +5,11 @@ import sample.ExpressionParser;
public class EvaluationVisitor extends ExpressionBaseVisitor<Integer> {
@Override
public Integer visitInParenthesesExpression(ExpressionParser.InParenthesesExpressionContext ctx) {
return ctx.expression().accept(this);
}
@Override
public Integer visitAddition(ExpressionParser.AdditionContext ctx) {
return ctx.expression().getFirst().accept(this) + ctx.expression().getLast().accept(this);
......
......@@ -24,7 +24,8 @@ public class Main {
EvaluationVisitor visitor = new EvaluationVisitor();
System.out.println(visitor.visit(tree));
Integer value = visitor.visit(tree);
System.out.println(value);
//show AST in console
System.out.println(tree.toStringTree(parser));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment