Skip to content

⚡ A C implementation of the Cocke–Younger–Kasami (CYK) algorithm to determine if a string belongs to a context-free grammar in Chomsky Normal Form (CNF). Includes input parsing, validation, detailed parse table output, and a clean Makefile-based build system. Great for compiler theory enthusiasts and students! 💻📚

Notifications You must be signed in to change notification settings

InventedSarawak/CYK-Algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 CYK Algorithm Implementation

An efficient C implementation of the Cocke–Younger–Kasami (CYK) algorithm — a classic dynamic programming technique to determine if a given string belongs to a Context-Free Grammar (CFG) in Chomsky Normal Form (CNF). Whether you're parsing grammars or diving into compiler theory, this project has you covered 💡📚


🧠 Features

  • 📥 Input-friendly: Parses grammar rules from standard input
  • CNF Check: Converts grammar to CNF if not already in CNF
  • 📊 Parse Table Output: Displays the parsing matrix step-by-step
  • 🔍 Membership Test: Checks if a string can be generated by the grammar

⚙️ Build & Run

  1. Clone the repository and compile using the provided Makefile:
git clone <repository-url>
cd CYK_Algorithm
make
  1. a. To run the program after building on Unix-like systems:
./bin/cyk_algorithm.exe
  1. b. To run the program on Windows using Docker:
# Build the Docker image
docker build -t cyk-algorithm .

# Run interactively
docker run -it cyk-algorithm

# Or run with input from a file
docker run -i cyk-algorithm < your_input_file.txt
  1. To clean up build files:
make clean

🗂️ Project Structure

CYK_Algorithm/
├── include/         # 📁 Header files
│   ├── grammar.h    # 🧱 Grammar data structures and CNF utilities
│   └── utils.h      # 🛠️ Common utility functions
├── src/             # 🧠 Core source files
│   ├── main.c       # 🚪 Entry point
│   ├── input.c      # 📥 Input parsing logic
│   └── utils.c      # 🧰 Utility implementations
├── bin/             # 📦 Compiled executable
├── core/            # 🧮 Object files
└── Makefile         # 🛠️ Build configuration

🧾 Sample Grammar Input Format

S -> AB | BC
A -> BA | a
B -> CC | b
C -> AB | a

All grammar rules should strictly be in CNF.


💻 Example Usage

Given grammar and input string:

Enter number of productions: 4
Enter productions:
S -> AB
A -> a
B -> b
Enter input string: ab

Expected output:

✅ The string *ab* is derivable from the given grammar.

🧑‍💻 Author

Made for an assignment with ❤️ by Vedant Kesarwani (@InventedSarawak)
DMs always open for collabs or questions!


⭐️ Give it a star!

If you found this helpful or learned something new, consider starring ⭐ the repo!


About

⚡ A C implementation of the Cocke–Younger–Kasami (CYK) algorithm to determine if a string belongs to a context-free grammar in Chomsky Normal Form (CNF). Includes input parsing, validation, detailed parse table output, and a clean Makefile-based build system. Great for compiler theory enthusiasts and students! 💻📚

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published