Skip to content

add new code #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .idea/deployment.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

316 changes: 316 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions project01/Digraph.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
digraph {
graph [rankdir=LR]
"" [color=white shape=circle]
node4 [shape=doublecircle]
node5 [shape=doublecircle]
node13 [shape=doublecircle]
node14 [shape=doublecircle]
node1 [shape=circle]
node1 -> node2 [label=a]
node2 [shape=circle]
node2 -> node5 [label="ε"]
node3 [shape=circle]
node3 -> node4 [label=a]
node4 -> node3 [label="ε"]
node5 -> node3 [label="ε"]
node6 [shape=circle]
node6 -> node7 [label=a]
node7 [shape=circle]
node7 -> node8 [label="ε"]
node8 [shape=circle]
node8 -> node9 [label=b]
node9 [shape=circle]
node9 -> node14 [label="ε"]
node10 [shape=circle]
node10 -> node11 [label=a]
node11 [shape=circle]
node11 -> node12 [label="ε"]
node12 [shape=circle]
node12 -> node13 [label=b]
node13 -> node10 [label="ε"]
node14 -> node10 [label="ε"]
node15 [shape=circle]
node15 -> node1 [label="ε"]
node15 -> node6 [label="ε"]
"" -> node15
}
Binary file added project01/Digraph.gv.pdf
Binary file not shown.
Binary file added project01/__pycache__/nfa.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file added project01/__pycache__/test_REtoNFA.cpython-36.pyc
Binary file not shown.
19 changes: 12 additions & 7 deletions project01/ip.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
292.25.1.9
192.168.56.256
1.1.1.256
192.168.1.1
192.
28.
1.
192.168.1
255.255.255.255
64.11.5
5.
19.23.35.6
123.55.2.3
198.162.2.3
1.1.1.1
0.2.1.6
255.255.255.256
32.266.21.2
899.23.2.2

122 changes: 87 additions & 35 deletions project01/nfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ def __repr__(self):
return str(self)

def union_t_function(t_function1, t_function2):
t_function = t_function1.copy()
t_function = {}
for key in t_function1.keys():
t_function[key] = t_function1[key].copy()

for key in t_function2.keys():
if key not in t_function:
t_function[key] = t_function2[key]
t_function[key] = t_function2[key].copy()
else:
t_function[key] += t_function2[key]
t_function[key] += t_function2[key].copy()
return t_function

class NFA():
"""Summary of class here.

Defination of the regular language.
"""Definition of the Nondeterministic Finite Automaton.

Attributes:
alphabet: list object to store finate alphabet
Expand Down Expand Up @@ -115,12 +116,12 @@ def set_start_state(self, state):
Returns:
Raises:
"""
if state not in self.states:
self.states.insert(0, state)
self.t_function[state] = dict()
else:
self.states.remove(state)
self.states.insert(0, state)
# if state not in self.states:
# self.states.insert(0, state)
# self.t_function[state] = dict()
# else:
# self.states.remove(state)
# self.states.insert(0, state)
self.s_state = state

#------------------------------------------------------------------------------
Expand Down Expand Up @@ -218,7 +219,7 @@ def or_nfa(self, other_nfa):

# then new_s_state is added to be the states of new_nfa
# set to be new_s_state (all included in nfa.set_start_state method)
nfa.set_start_state(new_s_state)
nfa.add_state(new_s_state)
nfa.add_function_item(new_s_state, EMPTY_STRING, s_state1)
nfa.add_function_item(new_s_state, EMPTY_STRING, s_state2)
return nfa
Expand Down Expand Up @@ -265,27 +266,17 @@ def repeat_nfa(self):
Raises:
"""
# TODO(ShipXu): XiaoHanHou implement this function.
# new_s_state = generate_state()
# old_s_state = self.get_start_state()
# new_f_states = self.f_states + [new_s_state]
# nfa = NFA(self.alphabet, self.states, new_s_state, new_f_states, self.t_function)
# nfa.add_state(new_s_state)
# nfa.set_start_state(new_s_state)
# nfa.add_function_item(new_s_state, EMPTY_STRING, old_s_state)

# for f_state in self.f_states:
# nfa.add_function_item(f_state, EMPTY_STRING, old_s_state)

new_s_state = generate_state()
old_s_state = self.get_start_state()
nfa = NFA(self.alphabet, self.states, new_s_state, self.f_states, self.t_function)
new_f_states = self.f_states.copy() + [new_s_state]
nfa = NFA(self.alphabet, self.states.copy(), new_s_state, new_f_states, self.t_function)
nfa.add_state(new_s_state)
nfa.set_start_state(new_s_state)
nfa.add_f_state(new_s_state)
nfa.add_function_item(new_s_state, EMPTY_STRING, old_s_state)

for f_state in self.f_states:
nfa.add_function_item(f_state, EMPTY_STRING, old_s_state)


return nfa

#------------------------------------------------------------------------------
Expand All @@ -312,24 +303,78 @@ def __str__(self):
ret += item_str + '\n'
return ret

#------------------------------------------------------------------------------
# Recogize if string is legal
#------------------------------------------------------------------------------
def run(self, s):
""" provide a method for using nfa to recognize string,

nfa will determine if the string is belong to the grammar of nfa.

Args:
s : string that is needed to be judged
Returns:
if nfa recognized a string, return True
if string is illegal to this nfa, return False
Raises:
"""
return self._run(s, self.s_state)

def _run(self, s, present_node):
""" provide a method for using nfa to recognize string,

The method provides a bfs-like method for recognizing string s,
if s is empty and present_node is in the self.f_states:
we can conclude that string is recognizable.
if not:
first we deals with the EMPTY STRING situation, the functions will
pass s directly to the next search;
secondly, we can check if s[0] is the item of present_node's transition
function in the nfa, and we pass s[1:] (s[0] is used) to the next search;

Args:
s : string that is needed to be judged
present_node : the current state of current search turn
Returns:
if nfa recognized a string, return True
if string is illegal to this nfa, return False
"""

if not s:
if present_node in self.f_states:
return True
else:
return False

if EMPTY_STRING in self.t_function[present_node]:
for to_node in self.t_function[present_node][EMPTY_STRING]:
if self._run(s, to_node):
return True

if s[0] in self.t_function[present_node]:
for to_node in self.t_function[present_node][s[0]]:
if self._run(s[1:], to_node):
return True

return False

if __name__ == '__main__':
alphabet = ['a', 'b']

# test01: generate_state
print('test01: generate_state')
s1 = generate_state()
s2 = generate_state()
s3 = generate_state()
print(s1, s2, s3)

# test02: nfa
print('test02: generate nfa')
states = [s1, s2]
s_state = s1
f_states = [s1, s2]
nfa = NFA(alphabet, states, s_state, f_states)
nfa.add_function_item(s1, 'a', s2)
nfa.add_function_item(s2, 'a', s2)
print(nfa)

# generate states variable
Expand All @@ -339,6 +384,7 @@ def __str__(self):
print(states)

# test03: nfa a ; nfa b
print('test03: nfa a ; nfa b')
nfa1 = NFA(alphabet, states[0:2], states[0], [states[1]])
nfa1.add_function_item(states[0], 'a', states[1])
print('-------nfa1--------')
Expand All @@ -348,7 +394,14 @@ def __str__(self):
print('-------nfa2--------')
print(nfa2)

new_states = generate_states(2)
nfa3 = NFA(alphabet, new_states[0:2], new_states[0], [new_states[1]])
nfa3.add_function_item(new_states[0], 'a', new_states[1])
print('-------nfa3--------')
print(nfa3)

# test04: test for t_function_union
print('test04: for t_function_union')
t_function1 = nfa1.get_t_function()
t_function2 = nfa2.get_t_function()

Expand All @@ -358,18 +411,17 @@ def __str__(self):
print(union_t_function(t_function1, t_function2))

# test05: test for '+'
print('test05: test for +')
print('-------nfa1 + nfa2--------')
print(nfa1 + nfa2)

# test05: test for '|'
print('test05: test for |')
print('-------(nfa1 + nfa2) | nfa3--------')
new_states = generate_states(2)

nfa3 = NFA(alphabet, new_states[0:2], new_states[0], [new_states[1]])
nfa3.add_function_item(new_states[0], 'a', new_states[1])
print((nfa1 + nfa2) | nfa3)

# test06: test for '*'
# print('-------((nfa1 + nfa2) | nfa1).repeat()--------')
# print(((nfa1 + nfa2) | nfa1).repeat())
print(nfa2.repeat())
print('test06: test for *')
print('-------((nfa1 + nfa2) | nfa3).repeat()--------')
print(((nfa1 + nfa2) | nfa3).repeat())
nfa4 = ((nfa1 + nfa2) | nfa3).repeat()
38 changes: 19 additions & 19 deletions project01/regular_expression.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
EMPTY_STRING = ''

class RE():
"""Defination of the regular language.
"""Definition of the regular language.

Attributes:
s: the string to describe the regular language

s: the string that describes the regular language
alphabet: list object to store finite alphabet
"""
def __init__(self, alphabet, s):
# TODO check is word in s is in the alphabeta
# TODO check is word in s is in the alphabet
self.alphabet = alphabet
self.s = s

Expand Down Expand Up @@ -40,32 +40,32 @@ def __str__(self):
return self.s

# a function object that can used to produce a re object
# when alphabelt is given
EMPTY_RE = (lambda alphabelt: RE(alphabelt, EMPTY_STRING))
# when alphabet is given
EMPTY_RE = (lambda alphabet: RE(alphabet, EMPTY_STRING))

def get_alphabelt_re(alphabelt):
ret = EMPTY_RE(alphabelt)
def get_alphabelt_re(alphabet):
ret = EMPTY_RE(alphabet)

if alphabelt:
ret = RE(alphabelt, alphabelt[0])
for a in alphabelt[1:]:
ret |= RE(alphabelt, a)
if alphabet:
ret = RE(alphabet, alphabet[0])
for a in alphabet[1:]:
ret |= RE(alphabet, a)
return ret()

if __name__ == '__main__':
alphabelt = ['0', '1']
alphabet = ['0', '1']

re_01 = (lambda s: RE(alphabelt, s))
re_01 = (lambda s: RE(alphabet, s))
print(re_01('0').repeat())

# test pratices in P65 of book
# test practices in P65 of book
# test01: 0*10*
# print(RE(alphabelt, '0'))
# print(RE(alphabet, '0'))
print(re_01('0').repeat() + re_01('1') + re_01('0').repeat())

# test02: (alphabelt)*1(alphabelt)*
re_alphabeta = get_alphabelt_re(alphabelt)
print(re_alphabeta.repeat() + re_01('1') + re_alphabeta.repeat())
# test02: (alphabet)*1(alphabet)*
re_alphabet = get_alphabelt_re(alphabet)
print(re_alphabet.repeat() + re_01('1') + re_alphabet.repeat())

# test03: 01 | 10
print(re_01('01') | re_01('10'))
Loading