-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample2.py
More file actions
27 lines (20 loc) · 803 Bytes
/
example2.py
File metadata and controls
27 lines (20 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""This is an example of using a PDA to parse strings with
well-formed parentheses."""
from PDA import *
from State import *
accept5 = State(state_type="Accept")
pop4 = State(state_type="Pop")
pop3 = State(state_type="Pop")
push2 = State(state_type="Push")
read1 = State(state_type="Read")
# add transitions for 'a', 'b', and the 'end' of the tape.
read1.add_transition(push2, character="(")
read1.add_transition(pop3, character=")")
read1.add_transition(pop4, character="!")
push2.add_transition(read1, character="X")
pop3.add_transition(read1, character="X")
pop4.add_transition(accept5, character="!")
start_state = State(state_type="Start", next_state=read1)
my_pda = PDA(start_state)
tape_word = "(((((((()))(((())))()()((())))))))!"
my_pda.start(tape=tape_word, stack=['!'], verbose=False)