From 4200d0a62f3ef014eb06f1761488cb3d2fc3ee63 Mon Sep 17 00:00:00 2001 From: shing19m Date: Tue, 23 Jul 2013 20:31:04 +0200 Subject: [PATCH] Adding the original version from the blog. --- .gitignore | 1 + pom.xml | 34 +++++++++++++++++++----------- src/main/antlr3/package/b1/b1.g | 37 +++++++++++++++++++++++++++++++++ src/main/java/b1/Main.java | 23 ++++++++++++++++++++ src/main/java/b1/Math.java | 8 +++++++ src/test/java/b1/MathTest.java | 12 +++++++++++ 6 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 src/main/antlr3/package/b1/b1.g create mode 100644 src/main/java/b1/Main.java create mode 100644 src/main/java/b1/Math.java create mode 100644 src/test/java/b1/MathTest.java diff --git a/.gitignore b/.gitignore index 0f182a0..8685af6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.jar *.war *.ear +/target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml index 15b5359..bd824d6 100644 --- a/pom.xml +++ b/pom.xml @@ -11,20 +11,24 @@ http://maven.apache.org/xsd/maven-4.0.0.xsd"> - - org.apache.maven.plugins - - - maven-compiler-plugin - + org.antlr + antlr3-maven-plugin + 3.1.3-1 + + + + antlr + + + + + + org.apache.maven.plugins + maven-compiler-plugin 2.1 - - 1.5 - - - 1.5 - + 1.5 + 1.5 UTF-8 @@ -62,6 +66,12 @@ http://maven.apache.org/xsd/maven-4.0.0.xsd"> jline 0.9.9 + + org.antlr + antlr-runtime + 3.2 + jar + diff --git a/src/main/antlr3/package/b1/b1.g b/src/main/antlr3/package/b1/b1.g new file mode 100644 index 0000000..3d3fd6a --- /dev/null +++ b/src/main/antlr3/package/b1/b1.g @@ -0,0 +1,37 @@ +// Name der Grammatik +grammar b1; + +// einfache, feste Tokens +tokens { + PLUS = '+' ; + MINUS = '-' ; + MULT = '*' ; + DIV = '/' ; +} + +@header { + package b1; +} + +@lexer::header { + package b1; +} + +ausdruck: + term ( ( PLUS | MINUS ) term )* ; + +term : + faktor ( ( MULT | DIV ) faktor )* ; + +faktor : + ZAHL ; + +ZAHL : + (DIGIT)+ ; + +WHITESPACE : + ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ + { $channel = HIDDEN; } ; + +fragment DIGIT : + '0'..'9' ; \ No newline at end of file diff --git a/src/main/java/b1/Main.java b/src/main/java/b1/Main.java new file mode 100644 index 0000000..acb4bcd --- /dev/null +++ b/src/main/java/b1/Main.java @@ -0,0 +1,23 @@ +package b1; + +import java.io.IOException; +import java.io.PrintWriter; +import jline.ConsoleReader; + +public class Main { + + public static void main(String[] args) throws IOException { + ConsoleReader reader = new ConsoleReader(); + String line; + PrintWriter out = new PrintWriter(System.out); + while ((line = reader.readLine("number or quit> ")) != null) { + if (line.equalsIgnoreCase("quit")) { + break; + } + int i = Integer.parseInt(line); + System.out.println( + Math.square(i) + " ist " + i + " quadriert"); + out.flush(); + } + } +} \ No newline at end of file diff --git a/src/main/java/b1/Math.java b/src/main/java/b1/Math.java new file mode 100644 index 0000000..e111fe5 --- /dev/null +++ b/src/main/java/b1/Math.java @@ -0,0 +1,8 @@ +package b1; + +public class Math { + + public static int square(int a) { + return a * a; + } +} diff --git a/src/test/java/b1/MathTest.java b/src/test/java/b1/MathTest.java new file mode 100644 index 0000000..21a5999 --- /dev/null +++ b/src/test/java/b1/MathTest.java @@ -0,0 +1,12 @@ +package b1; + +import org.junit.Assert; +import org.junit.Test; + +public class MathTest { + + @Test + public void testSqure() { + Assert.assertEquals(16, Math.square(4)); + } +} \ No newline at end of file