serhioms / ExprInt

Antlr based math expressions enterpreter (str, int, real, boolean, ordered/unordered set)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expression interpreter

Evaluate arifmetic expression with Real, Integer, Boolean, String and Set. Able to declare variable by expression assignment. Set can contain any type include other sets.

What is special?

Interpretator developed based on ANTLR gramma, ANTLR visitor and implementation of each of 5 types .

Is not it clean and straight forward? I like ANTLR!

Special thanks to Shmatov and Mgrzeszczak for good examples of how to do ANTLR for numbers and simple sets.

Boolean and sets algebra following wiki below: Boolean algebra, De Morgan laws, Set math, Set symmetric difference, Complement set,Cartesian product, Ordered pair, univers, Inner product, Norm, Bit operations.

Usage

Download project and start run.cmd from command line.

C:\ExprInt>run.cmd
Enter expression, help or quit...
>2+2
4

Java 8 required as below

C:\ExprInt>java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

What is not covered

  • Interpretator gramma does not implement functions(args);
  • Powerset is not implemented.

Boolean expressions

Available operations are >=, >, <=, <, == (equals), != or <> (not equal), && (or), || (and), -> (implication), !& (nand), !| (nor), X| (xor), X! (xnor). There are two predefined boolean constants true and false.

Examples here: basic, boolean laws, the morgan laws.

Number expressions

Available operations with real and integer numbers are +, -, *, /, ** (power), ==, >=, >, <=, <.

There are two predefined real constants _pi (3.14) and _e (2.71).

Here are examples: arifmetika follows Shmatov example, set var follows Shmatov example

Bit operations with integer

There are couple of bit operations available for integers as ~ (inversion), >> (right shift), >>> (unsigned right shift), << (left shift), & (and), | (or), ^^(xor). Here is bit example.

String expressions

"This is a string constant in double quotation marks"

Available operations with strings are |"string length"|, + (concatenation), ==, !=. Here is string example.

Sets expressions

Available operations are |{"Cardinality"}|, == (equals), @ (belongs), & (intersection), && (recursive intersection), | (union), \ (complements), /\ (disjunctive union), ' (complement set), * (cartesian product), ** (n-ary Cartesian product if set3), ** (inner product if setset), ||{"Norm"}||.

There couple special sets are {} (empty unordered set), [] (empty ordered set), {}' and []' (universal sets).

Here are examples: Basic operations, Assignment, Cardinality, Cardinality laws, Union, Union laws, Intersection, Intersection laws, Complements, Complements laws, Empty set laws, Complex set, Cartesian product, Cartesian laws, Set membership, Morgan laws, Relative laws, Symmetric difference laws, Universal set, Card deck example, Comparison, Inner product and Norm.

About

Antlr based math expressions enterpreter (str, int, real, boolean, ordered/unordered set)


Languages

Language:Java 98.3%Language:ANTLR 1.6%Language:Batchfile 0.0%