This repository contains a Python implementation of a Deterministic Pushdown Automaton (DPDA). A DPDA is a type of automaton used in formal language theory, capable of recognizing context-free languages.
The DPDA implementation includes the following components:
- DPDA Class: Represents the deterministic pushdown automaton with methods for initialization, state transition, acceptance checking, and string processing.
- parse_input Function: Parses an input file to extract alphabet, states, transitions, acceptance states, and test strings.
- main Function: Entry point that reads an input file, initializes a DPDA, processes test strings, and prints results.
-
__init__(self, transitions, initial_state, acceptance_states)
- Initializes the DPDA with transition rules, initial state, and acceptance states.
-
reset(self)
- Resets the stack to its initial state ('Z').
-
transition(self, state, symbol, stack_top)
- Retrieves the next state and stack symbols to push based on current state, input symbol, and top of the stack.
-
is_accepting(self, state)
- Checks if a given state is an acceptance state.
-
process_string(self, string)
- Processes an input string to determine if it is accepted by the DPDA.
parse_input(file_path)
- Reads an input file and extracts alphabet symbols, states, transitions, acceptance states, and test strings.
main(file_path)
- Entry point of the script. It reads an input file, initializes a DPDA, processes each test string, and prints the acceptance result.
The input file format is structured as follows:
- Number of symbols in the alphabet.
- Alphabet symbols (space-separated).
- Number of states.
- Number of transitions.
- Transition rules in the format:
current_state, input_symbol, stack_top -> next_state, stack_push
. - Number of acceptance states.
- Acceptance states (space-separated).
- Test strings (one per line), terminated by a line containing only
$
.
2
a b
3
5
0, a, Z -> 1, AZ
1, a, A -> 1, AA
1, b, A -> 2, @
2, b, A -> 2, @
2, @, Z -> 2, @
1
2
aaabbb
aabb
$
To use the DPDA implementation:
-
Ensure Python 3.x is installed.
-
Clone the repository or download the
dpda.py
file. -
Create an input file following the specified format (like
test.txt
). -
Run the script with the command:
python dpda.py test.txt
-
View the results for each test string printed to the console.
The output displays each test string followed by whether it was accepted or rejected by the DPDA.
aab: Accepted
abb: Rejected
This project is licensed under the MIT License. See the LICENSE file for details.