A command-line tool that evaluates propositional logic expressions and generates truth tables using recursive descent parsing.
The evaluator uses a multi-stage processing pipeline:
- Variables:
P
,Q
(Boolean variables) - Operators:
AND
: Logical conjunctionOR
: Logical disjunctionNOT
: Logical negation->
: Logical implication
- Parentheses:
(
,)
for grouping expressions
- Scanner (Lexical Analysis): Breaks down input into tokens
- Parser (Syntax Analysis): Constructs Abstract Syntax Tree (AST)
- Evaluator: Generates truth tables and evaluates expressions
- C++ compiler (g++ or equivalent)
- Windows: MinGW-w64 or Visual Studio with C++ support
- macOS: Xcode Command Line Tools or Homebrew's GCC
- Linux: GCC/G++
To check gcc installation:
gcc --version
# Clone the repository
git clone https://github.com/nbaquino/propositional-logic-interpreter.git
cd propositional-logic-interpreter
NOTE THAT You may skip this step if you are using Windows because there is already a build LOGIC.exe when you clone this repository. So you may proceed with the Usage section.
cd src
g++ -o LOGIC .\Main.cpp .\Scanner.cpp .\Parser.cpp .\Evaluator.cpp
- Navigate to the source directory:
cd propositional-logic-interpreter/src
- Install Homebrew (skip if already installed):
# Install Homebrew if not already installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Check if the Homebrew package manager was sucessfully installed by entering this in the terminal
brew
- In the terminal, install LLVM using Homebrew or typing this command:
brew install llvm
- After llvm installation, configure environment variables by entering the following command:
echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc
echo 'export CC="/opt/homebrew/opt/llvm/bin/clang"' >> ~/.zshrc
echo 'export CXX="/opt/homebrew/opt/llvm/bin/clang++"' >> ~/.zshrc
source ~/.zshrc
- Compile the program:
clang++ -o LOGIC Main.cpp Scanner.cpp Parser.cpp Evaluator.cpp
# Windows
.\LOGIC.exe
# macOS/Linux
./LOGIC
# Windows
.\LOGIC.exe sentence.pl
# macOS/Linux
./LOGIC sentence.pl
- Operators:
AND
,OR
,NOT
,->
- Variables:
P
andQ
- Example:
P AND Q
,NOT P
,P -> Q
- One expression per line
- Lines starting with '#' are comments
- Empty lines are ignored
$ ./LOGIC or ./LOGIC.exe
Enter a propositional logic statement: P AND Q
Truth Table:
P Q P AND Q
F F F
T F F
F T F
T T T
Enter a propositional logic statement: exit
Create a file named input.txt
with the following content:
# Sample expressions
P AND Q
P OR (Q AND R)
NOT (P AND Q)
Run the program:
$ ./LOGIC.exe sentence.pl
Truth Table:
P Q P AND Q
F F F
T F F
F T F
T T T
Truth Table:
P Q R P OR (Q AND R)
F F F F
F F T F
F T F F
F T T T
T F F T
T F T T
T T F T
T T T T
Truth Table:
P Q NOT (P AND Q)
F F T
T F T
F T T
T T F
Error: Invalid character: '$' at position 3
Error: Invalid token: 'X' at position 0
Error: Binary operator 'AND' must have both left and right operands
Error: Missing closing parenthesis
Error: NOT operator can only appear before its operand
Error: Invalid expression: Empty node encountered
Error: Unknown operator: 'XOR'
Error: Could not open input file: input.txt
- Fork the repository
- Create a new branch (
git checkout -b feature/improvement
) - Make your changes
- Push to the branch (
git push origin feature/improvement
) - Open a Pull Request