This project implements a tool that converts regular expressions to Finite Automata (DFA & NFA) with visualization capabilities. It provides a step-by-step conversion process from regular expressions to NFA (Non-deterministic Finite Automaton) and then to DFA, including minimization of the resulting DFA.
- Regular expression parsing and AST construction
- Thompson's construction algorithm for converting regex to NFA
- Subset construction algorithm for converting NFA to DFA
- Hopcroft's algorithm for DFA minimization
- Visualization of both NFA and DFA using Graphviz
- Support for basic regular expression operators:
- Concatenation (implicit)
- Union (
U
) - Kleene star (
*
) - Parentheses for grouping
- Epsilon transitions (
ε
) - Binary alphabet (
0
and1
)
- Python 3.x
- Graphviz (for visualization)
- Clone the repository:
git clone https://github.com/CS-Astronaut/Regex-To-Automata
cd Regex-To-Automata
- Install the required Python package:
pip install graphviz
Run the program:
python main.py
When prompted, enter a regular expression using the following syntax:
- Use
U
for union (e.g.,0U1
for "0 or 1") - Use
*
for Kleene star (e.g.,0*
for "zero or more 0s") - Use
ε
for epsilon transitions - Use parentheses for grouping (e.g.,
(0U1)*
)
The program will generate two visualization files:
output_nfa.png
: Visual representation of the NFAoutput_dfa.png
: Visual representation of the minimized DFA
Input regular expression: (0U1)*
This will generate:
- An NFA using Thompson's construction
- A DFA using subset construction
- A minimized DFA using Hopcroft's algorithm
- Visual representations of both the NFA and DFA
The project consists of several key components:
- Parser: Converts regular expressions into an Abstract Syntax Tree (AST)
- Thompson Construction: Converts AST to NFA
- Subset Construction: Converts NFA to DFA
- Hopcroft Minimization: Minimizes the DFA
- Visualization: Generates visual representations using Graphviz
(0U1)*(0(0U1)*0)(0U1)*
= Representing A Language That Contains The Strings With At least two 0s
(0U1)*1011(0U1)*
= Representing A Language That Contains The Strings With Substring of 1011
1*01*01*
= Representing A Language That Contains The Strings With Exactly two 0s
Also Can Draw The Machine That Accepts The Union Of Two Languages
(1*01*01*)U((0U1)*1011(0U1)*)
= Representing A Language That Contains The Strings With Substring of 1011 or Exactly two 0s
This project is licensed under the MIT License - see the LICENSE file for details.