Skip to content

Commit 41eaaa9

Browse files
committed
ANTLR Hello World Calculator
1 parent d9dbc0c commit 41eaaa9

4 files changed

Lines changed: 130 additions & 0 deletions

File tree

antlr/antlr-demo/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.javahelps.antlr</groupId>
8+
<artifactId>antlr-demo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.antlr</groupId>
14+
<artifactId>antlr4-runtime</artifactId>
15+
<version>4.7.2</version>
16+
</dependency>
17+
</dependencies>
18+
19+
<build>
20+
<plugins>
21+
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
<version>3.8.0</version>
26+
<configuration>
27+
<source>11</source>
28+
<target>11</target>
29+
</configuration>
30+
</plugin>
31+
32+
<plugin>
33+
<groupId>org.antlr</groupId>
34+
<artifactId>antlr4-maven-plugin</artifactId>
35+
<version>4.7.2</version>
36+
37+
<executions>
38+
<execution>
39+
<goals>
40+
<goal>antlr4</goal>
41+
</goals>
42+
<configuration>
43+
<listener>false</listener>
44+
<visitor>true</visitor>
45+
</configuration>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
50+
</plugins>
51+
</build>
52+
53+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
grammar Calculator;
3+
4+
5+
operation
6+
: left=NUMBER operator='+' right=NUMBER
7+
| left=NUMBER operator='-' right=NUMBER
8+
| left=NUMBER operator='*' right=NUMBER
9+
| left=NUMBER operator='/' right=NUMBER
10+
;
11+
12+
NUMBER
13+
: ('0' .. '9') + ('.' ('0' .. '9') +)?
14+
;
15+
16+
WS : (' ' | '\t')+ -> channel(HIDDEN);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.javahelps.antlrdemo.calculator;
2+
3+
import org.antlr.v4.runtime.CharStream;
4+
import org.antlr.v4.runtime.CharStreams;
5+
import org.antlr.v4.runtime.CodePointCharStream;
6+
import org.antlr.v4.runtime.CommonTokenStream;
7+
import org.antlr.v4.runtime.tree.ParseTree;
8+
9+
public class Calculator {
10+
11+
public static void main(String[] args) {
12+
13+
Calculator calculator = new Calculator();
14+
System.out.println(calculator.calculate("2 + 5")); // 7.0
15+
System.out.println(calculator.calculate("2 * 5")); // 10.0
16+
System.out.println(calculator.calculate("5 - 3")); // 2.0
17+
System.out.println(calculator.calculate("5 / 3")); // 1.6666666666666667
18+
System.out.println(calculator.calculate("5 # 3")); // Error: line 1:2 token recognition error at: '#'
19+
}
20+
21+
private Double calculate(String source) {
22+
CodePointCharStream input = CharStreams.fromString(source);
23+
return compile(input);
24+
}
25+
26+
private Double compile(CharStream source) {
27+
CalculatorLexer lexer = new CalculatorLexer(source);
28+
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
29+
CalculatorParser parser = new CalculatorParser(tokenStream);
30+
ParseTree tree = parser.operation();
31+
CalculatorVisitorImpl visitor = new CalculatorVisitorImpl();
32+
return visitor.visit(tree);
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javahelps.antlrdemo.calculator;
2+
3+
public class CalculatorVisitorImpl extends CalculatorBaseVisitor<Double> {
4+
5+
@Override
6+
public Double visitOperation(CalculatorParser.OperationContext ctx) {
7+
if (ctx.operator == null) {
8+
throw new UnsupportedOperationException("An operator of +, -, /, * is required to perform the operation");
9+
}
10+
String operator = ctx.operator.getText();
11+
double left = Double.parseDouble(ctx.left.getText());
12+
double right = Double.parseDouble(ctx.right.getText());
13+
14+
switch (operator) {
15+
case "+":
16+
return left + right;
17+
case "-":
18+
return left - right;
19+
case "/":
20+
return left / right;
21+
case "*":
22+
return left * right;
23+
default:
24+
throw new UnsupportedOperationException("Calculator does not support " + operator);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)