Crate logical_expression_pest_parser

Crate logical_expression_pest_parser 

Source
Expand description

§Logical Expression Pest Parser Documentation

This crate parses strings of logical expression using pest crate, such as:

(A AND B) OR (NOT (A XOR C))

(A | C) & !B

(X XOR Y) | A

It returns a truth table showing all input combinations with corresponding output results.


It can handle those logic operators:

OperatorCorresponding symbols
NOTNOT, not, !
ANDAND, and, &
NANDNAND, nand, !&
OROR, or, |
NORNOR, nor, !|
XORXOR, xor, ^
XNORXNOR, xnor, !^

After parsing, the resulting three is analyzed and converted into an Abstract Syntax Tree. The next step is to evaluate the logical expression for all possible combinations of input values, allowing the generation of a complete truth table.

For example, we have an expression:

(A & B) | C

The resulting truth table is going to be

ABCOutput
0000
1000
0100
1101
0011
1011
0111
1111

§Usage


cargo install logical-expression-parser
logical-expression-pest-parser.exe parse -f .\input.txt --ast

§input.txt

In order to analyze multiple expressions, they must be defined in different lines and there must be an empty line at the end.

A and !B or C
X !| Y
(K XOR L) AND M

§Output
Processing file: .\input.txt

Expression 1
Input: "A and !B or C"

AST: Or(And(Identifier('A'), Not(Identifier('B'))), Identifier('C'))

| A | B | C | Output |
|---|---|---|--------|
| 0 | 0 | 0 |   0    |
| 1 | 0 | 0 |   1    |
| 0 | 1 | 0 |   0    |
| 1 | 1 | 0 |   0    |
| 0 | 0 | 1 |   1    |
| 1 | 0 | 1 |   1    |
| 0 | 1 | 1 |   1    |
| 1 | 1 | 1 |   1    |

Expression 2
Input: "X !| Y"

AST: Nor(Identifier('X'), Identifier('Y'))

| X | Y | Output |
|---|---|--------|
| 0 | 0 |   1    |
| 1 | 0 |   0    |
| 0 | 1 |   0    |
| 1 | 1 |   0    |

Expression 3
Input: "(K XOR L) AND M"

AST: And(Xor(Identifier('K'), Identifier('L')), Identifier('M'))

| K | L | M | Output |
|---|---|---|--------|
| 0 | 0 | 0 |   0    |
| 1 | 0 | 0 |   0    |
| 0 | 1 | 0 |   0    |
| 1 | 1 | 0 |   0    |
| 0 | 0 | 1 |   0    |
| 1 | 0 | 1 |   1    |
| 0 | 1 | 1 |   1    |
| 1 | 1 | 1 |   0    |

§Use this command for help

logical-expression-pest-parser.exe help

§grammar.pest

The grammar is structured in such a way that the parser can determine priorities for boolean operators.

WHITESPACE = _{ " " | "\t" }

not_operator  = { "NOT" | "not" | "!" }
and_operator  = { "AND" | "and" | "&" }
nand_operator = { "NAND" | "nand" | "!&" }
or_operator   = { "OR" | "or" | "|" }
nor_operator  = { "NOR" | "nor" | "!|" }
xor_operator  = { "XOR" | "xor" | "^" }
xnor_operator = { "XNOR" | "xnor" | "!^" }

left_parenthesis  = { "(" }
right_parenthesis = { ")" }

identifier = @{ ASCII_ALPHA_UPPER ~ !(ASCII_ALPHA_UPPER) }

term       = { not_operator* ~ (identifier | left_parenthesis ~ expression ~ right_parenthesis) }
xor_clause = { term ~ ((xor_operator | xnor_operator) ~ term)* }
and_clause = { xor_clause ~ ((and_operator | nand_operator) ~ xor_clause)* }
expression = { and_clause ~ ((or_operator | nor_operator) ~ and_clause)* }

NEWLINE = _{ "\n" | "\r\n" }

file = { SOI ~ expression ~ NEWLINE ~ (expression ~ NEWLINE)* ~ EOI }

Modules§

ast
Abstract Syntax Tree (AST) Module
parser
Parser Module
truth_table
Truth Table Module