aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/Python/tests/t015calc.g
diff options
context:
space:
mode:
authorBen Gruver <bgruv@google.com>2011-11-15 16:02:09 -0800
committerBen Gruver <bgruv@google.com>2011-11-16 11:07:51 -0800
commit324c4644fee44b9898524c09511bd33c3f12e2df (patch)
tree1e3285425b3b39500a7ec82b47535e354587d67b /antlr-3.4/runtime/Python/tests/t015calc.g
parent939f4a47010d361698e8f81a67e2337e180fc086 (diff)
downloadantlr-jb-mr1-dev-plus-aosp.tar.gz
This replaces the existing source for an unknown version of the antlr-runtime with the full source for the antlr tool. However, we are still building just the runtime jar, not the full tool. The full tool will be included as a prebuilt jar, due to the complexities of building this tool. Since we will have both the full tool and the runtime jar in the Android tree, the module name for the runtime jar has been changed from "antlr" to "antlr-runtime" Change-Id: I38d5f3e5e82392dc122f46bf7961aab5b42e40c5 Signed-off-by: Ben Gruver <bgruv@google.com>
Diffstat (limited to 'antlr-3.4/runtime/Python/tests/t015calc.g')
-rw-r--r--antlr-3.4/runtime/Python/tests/t015calc.g54
1 files changed, 54 insertions, 0 deletions
diff --git a/antlr-3.4/runtime/Python/tests/t015calc.g b/antlr-3.4/runtime/Python/tests/t015calc.g
new file mode 100644
index 0000000..f08e3ce
--- /dev/null
+++ b/antlr-3.4/runtime/Python/tests/t015calc.g
@@ -0,0 +1,54 @@
+grammar t015calc;
+options {
+ language = Python;
+}
+
+@header {
+import math
+}
+
+@parser::init {
+self.reportedErrors = []
+}
+
+@parser::members {
+def emitErrorMessage(self, msg):
+ self.reportedErrors.append(msg)
+}
+
+evaluate returns [result]: r=expression {result = r};
+
+expression returns [result]: r=mult (
+ '+' r2=mult {r += r2}
+ | '-' r2=mult {r -= r2}
+ )* {result = r};
+
+mult returns [result]: r=log (
+ '*' r2=log {r *= r2}
+ | '/' r2=log {r /= r2}
+// | '%' r2=log {r %= r2}
+ )* {result = r};
+
+log returns [result]: 'ln' r=exp {result = math.log(r)}
+ | r=exp {result = r}
+ ;
+
+exp returns [result]: r=atom ('^' r2=atom {r = math.pow(r,r2)} )? {result = r}
+ ;
+
+atom returns [result]:
+ n=INTEGER {result = int($n.text)}
+ | n=DECIMAL {result = float($n.text)}
+ | '(' r=expression {result = r} ')'
+ | 'PI' {result = math.pi}
+ | 'E' {result = math.e}
+ ;
+
+INTEGER: DIGIT+;
+
+DECIMAL: DIGIT+ '.' DIGIT+;
+
+fragment
+DIGIT: '0'..'9';
+
+WS: (' ' | '\n' | '\t')+ {$channel = HIDDEN};