Skip to content

Commit

Permalink
calculator grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
Stacy Everett committed Jul 11, 2015
1 parent 0c2e7f8 commit 102f895
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 0 deletions.
9 changes: 9 additions & 0 deletions calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Calculator Grammar

# Summary

A simple ANTLR4 calculator grammar based on the khubla.com arithmetic grammar. Calculator expands on arithmetic by adding functions, such as sin() and ln()

The calculator example is intended of an example of how to parse geometric and algebraic expressions with ANTLR


174 changes: 174 additions & 0 deletions calculator/calculator.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
BSD License
Copyright (c) 2013, Tom Everett
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Tom Everett nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

grammar calculator;

expression
: addingExpression ((relop) addingExpression)*
;

addingExpression
: multiplyingExpression ((PLUS|MINUS) multiplyingExpression)*
;

multiplyingExpression
: powExpression ((TIMES|DIV) powExpression)*
;

powExpression
: expExpression (POW expExpression)?
;

expExpression
: constantExpression (E number)?
;

constantExpression
: number
| variable
| LPAREN expression RPAREN
| func
;

func
: funcname LPAREN expression RPAREN
;

funcname
: COS | TAN | SIN | ACOS | ATAN | ASIN | LOG | LN
;

relop
: EQ | GT | LT
;

number
: MINUS? digits (POINT digits)?
;

digits
: DIGIT+;

variable
: MINUS? LETTER (LETTER | DIGIT)*;

COS
: 'cos'
;

SIN
: 'sin'
;

TAN
: 'tan'
;

ACOS
: 'acos'
;

ASIN
: 'asin'
;

ATAN
: 'atan'
;
LN
: 'ln'
;

LOG
: 'log'
;

LPAREN
: '('
;

RPAREN
: ')'
;

PLUS
: '+'
;

MINUS
: '-'
;

TIMES
: '*'
;

DIV
: '/'
;

GT
: '>'
;

LT
: '<'
;

EQ
: '='
;

POINT
: '.'
;

E
: 'e'
| 'E'
;

POW
: '^'
;

LETTER
: ('a'..'z') | ('A'..'Z')
;

DIGIT
: ('0'..'9')
;

WS
: [ \r\n\t]+ -> channel(HIDDEN)
;
2 changes: 2 additions & 0 deletions calculator/examples/pyth.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sin (x)^2 + cos(x)^2 = 1

3 changes: 3 additions & 0 deletions calculator/examples/quadratic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = (-b + (b^2 -4*a*c)^0.50) / 4*a*c


2 changes: 2 additions & 0 deletions calculator/examples/snell.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sin (a1) / sin (a2) = v1 / v2

2 changes: 2 additions & 0 deletions calculator/examples/tan.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tan (x) = sin(x) / cos (x)

52 changes: 52 additions & 0 deletions calculator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>calculator</artifactId>
<packaging>jar</packaging>
<name>khubla.com Calculator grammar</name>
<parent>
<groupId>com.antlr.grammarsv4</groupId>
<artifactId>grammarsv4</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr.version}</version>
<configuration>
<sourceDirectory>${basedir}</sourceDirectory>
<grammars>calculator.g4</grammars>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4test-maven-plugin</artifactId>
<configuration>
<verbose>false</verbose>
<showTree>true</showTree>
<entryPoint>expression</entryPoint>
<grammarName>calculator</grammarName>
<packageName></packageName>
<exampleFiles>examples/</exampleFiles>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>erlang</module>
<module>lua</module>
<module>arithmetic</module>
<module>calculator</module>
</modules>
<properties>
<target.jvm>1.6</target.jvm>
Expand Down

0 comments on commit 102f895

Please sign in to comment.