Skip to content

nbaquino/propositional-logic-interpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CMSC 124 - Propositional Logic Evaluator

A command-line tool that evaluates propositional logic expressions and generates truth tables using recursive descent parsing.

Technical Overview

The evaluator uses a multi-stage processing pipeline:

Tokens

  • Variables: P, Q (Boolean variables)
  • Operators:
    • AND: Logical conjunction
    • OR: Logical disjunction
    • NOT: Logical negation
    • ->: Logical implication
  • Parentheses: (, ) for grouping expressions

Processing Stages

  1. Scanner (Lexical Analysis): Breaks down input into tokens
  2. Parser (Syntax Analysis): Constructs Abstract Syntax Tree (AST)
  3. Evaluator: Generates truth tables and evaluates expressions

Prerequisites

  • 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

Installation

# Clone the repository
git clone https://github.com/nbaquino/propositional-logic-interpreter.git
cd propositional-logic-interpreter

Building the Program

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.

Windows

cd src
g++ -o LOGIC .\Main.cpp .\Scanner.cpp .\Parser.cpp .\Evaluator.cpp

macOS

  1. Navigate to the source directory:
cd propositional-logic-interpreter/src
  1. 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 
  1. In the terminal, install LLVM using Homebrew or typing this command:
brew install llvm
  1. 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
  1. Compile the program:
clang++ -o LOGIC Main.cpp Scanner.cpp Parser.cpp Evaluator.cpp

Usage

Interactive Mode

# Windows
.\LOGIC.exe

# macOS/Linux
./LOGIC

File Input Mode

# Windows
.\LOGIC.exe sentence.pl

# macOS/Linux
./LOGIC sentence.pl

Input Format

  • Operators: AND, OR, NOT, ->
  • Variables: P and Q
  • Example: P AND Q, NOT P, P -> Q

Input File Format

  • One expression per line
  • Lines starting with '#' are comments
  • Empty lines are ignored

Example Usage

Interactive Mode

$ ./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

File Input Mode

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 Handling

Lexical Errors (Scanner)

Error: Invalid character: '$' at position 3
Error: Invalid token: 'X' at position 0

Syntax Errors (Parser)

Error: Binary operator 'AND' must have both left and right operands
Error: Missing closing parenthesis
Error: NOT operator can only appear before its operand

Evaluation Errors (Evaluator)

Error: Invalid expression: Empty node encountered
Error: Unknown operator: 'XOR'

File Handling Errors

Error: Could not open input file: input.txt

Contributing

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published