logical-expression-pest-parser

Crates.iological-expression-pest-parser
lib.rslogical-expression-pest-parser
version0.1.4
created_at2025-11-10 16:27:09.922782+00
updated_at2025-11-11 20:44:31.486608+00
descriptionThis crate parses strings of logical expression using `pest` crate. It returns a truth table showing all input combinations with corresponding output results.
homepage
repositoryhttps://github.com/mzabrodin/logical-expression-parser
max_upload_size
id1925790
size48,119
Maksym Zabrodin (mzabrodin)

documentation

README

Logical Expression Pest Parser

GitHub repo Crates.io

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:

Operator Corresponding symbols
NOT NOT, not, !
AND AND, and, &
NAND NAND, nand, !&
OR OR, or, |
NOR NOR, nor, !|
XOR XOR, xor, ^
XNOR XNOR, 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

A B C Output
0 0 0 0
1 0 0 0
0 1 0 0
1 1 0 1
0 0 1 1
1 0 1 1
0 1 1 1
1 1 1 1

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 }
Commit count: 0

cargo fmt