Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48bdf12

Browse files
committed
examples added
1 parent 70037d4 commit 48bdf12

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

example2.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""This is an example of using a PDA to parse strings with
2+
well-formed parentheses."""
3+
from PDA import *
4+
from State import *
5+
6+
accept5 = State(state_type="Accept")
7+
pop4 = State(state_type="Pop")
8+
pop3 = State(state_type="Pop")
9+
push2 = State(state_type="Push")
10+
read1 = State(state_type="Read")
11+
12+
# add transitions for 'a', 'b', and the 'end' of the tape.
13+
read1.add_transition(push2, character="(")
14+
read1.add_transition(pop3, character=")")
15+
read1.add_transition(pop4, character="!")
16+
17+
push2.add_transition(read1, character="X")
18+
pop3.add_transition(read1, character="X")
19+
20+
pop4.add_transition(accept5, character="!")
21+
22+
start_state = State(state_type="Start", next_state=read1)
23+
24+
my_pda = PDA(start_state)
25+
26+
tape_word = "(((((((()))(((())))()()((())))))))!"
27+
my_pda.start(tape=tape_word, stack=['!'], verbose=False)

0 commit comments

Comments
 (0)