This repository contains a command-line tool and associated scripts for converting a regular expression to its corresponding Non-deterministic Finite Automaton (NFA) using Thompson's construction algorithm, and subsequently minimizing the NFA to a Deterministic Finite Automaton (DFA). The tool is designed as part of a programming assignment for the Compilers and Languages course at Cairo University's Faculty of Engineering, Computer Engineering Department.
This project implements a command-line tool to:
- Convert a regular expression into a Non-deterministic Finite Automaton (NFA) using Thompson's construction algorithm.
- Convert the generated NFA into a minimized Deterministic Finite Automaton (DFA).
Both conversions produce JSON files representing the states and transitions of the automata. Additionally, the tool outputs graphical representations of the NFA and DFA using a graphics library.
- Regular Expression to NFA Conversion: Converts a given regular expression into an NFA, validating the expression before transformation.
- NFA to DFA Minimization: Converts the NFA into a minimized DFA.
- JSON Output: Outputs the states and transitions of both NFA and DFA in a specified JSON format.
- Graphical Representation: Generates images of the NFA and DFA graphs, distinguishing between accepting and non-accepting states.
- Google Colab Integration: A ready-to-use Google Colab notebook for running the tool and generating outputs.
To install and run the tool locally, follow these steps:
-
Clone the repository:
git clone https://github.com/username/regex-to-nfa-dfa.git cd regex-to-nfa-dfa
-
Create a virtual environment and activate it:
python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required dependencies:
pip install -r requirements.txt
To convert a regular expression to an NFA and minimize it to a DFA, use the following commands:
-
Convert Regular Expression to NFA:
python convert_to_nfa.py "[A-Za-z]+[0-9]*"
-
Convert NFA to Minimized DFA:
python minimize_nfa.py path/to/nfa.json
To use the tool in Google Colab, follow these steps:
- Open the provided Colab notebook: NFA to DFA Converter
- Run the notebook cells to upload your input regular expression or NFA JSON file.
- The notebook will generate the JSON files and graphical representations for both the NFA and the DFA.
Output JSON (NFA):
{
"startingState": "S0",
"S0": {
"isTerminatingState": false,
"A": "S1",
"B": "S0"
},
"S1": {
"isTerminatingState": true,
"A": "S1",
"B": "S1"
}
}
Output JSON (DFA):
{
"startingState": "S0",
"S0": {
"isTerminatingState": false,
"A": "S1",
"B": "S0"
},
"S1": {
"isTerminatingState": true,
"A": "S1",
"B": "S1"
}
}