Skip to content

Commit

Permalink
Complete the interpretation of the NFA to parse the regular expressio…
Browse files Browse the repository at this point in the history
…n, and fix a few fucking bugs
  • Loading branch information
dejavudwh committed Sep 21, 2019
1 parent affbf36 commit 376a497
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
35 changes: 33 additions & 2 deletions nfa/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def pattern(pattern_string):
lexer = Lexer(pattern_string)
lexer.advance()
nfa_pair = NfaPair()
expr(nfa_pair)
group(nfa_pair)
log_nfa(nfa_pair.start_node)

return nfa_pair.start_node
Expand Down Expand Up @@ -139,6 +139,7 @@ def factor_conn(pair_out):

def is_conn(token):
nc = [
Token.OPEN_PAREN,
Token.CLOSE_PAREN,
Token.AT_EOL,
Token.EOS,
Expand Down Expand Up @@ -237,4 +238,34 @@ def expr(pair_out):
pair_out.end_node.next_2 = end
pair_out.end_node = end

return True
return True


def group(pair_out):
if lexer.match(Token.OPEN_PAREN):
lexer.advance()
expr(pair_out)
if lexer.match(Token.CLOSE_PAREN):
lexer.advance()
elif lexer.match(Token.EOS):
return False
else:
expr(pair_out)

while True:
pair = NfaPair()
if lexer.match(Token.OPEN_PAREN):
lexer.advance()
expr(pair)
pair_out.end_node.next_1 = pair.start_node
pair_out.end_node = pair.end_node
if lexer.match(Token.CLOSE_PAREN):
lexer.advance()
elif lexer.match(Token.EOS):
return False
else:
expr(pair)
pair_out.end_node.next_1 = pair.start_node
pair_out.end_node = pair.end_node


8 changes: 6 additions & 2 deletions parse/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
EPSILON,
CCL,
)
from nfa.construction import pattern


def compiler(input_string, start_node):
def match(input_string, pattern_string):
start_node = pattern(pattern_string)

current_nfa_set = [start_node]
next_nfa_set = closure(current_nfa_set)

Expand Down Expand Up @@ -56,7 +59,7 @@ def closure(input_set):
def move(input_set, ch):
out_set = []
for nfa in input_set:
print('debug move 1****', ch, nfa.status_num, nfa.edge, nfa.input_set)
print('debug move no 1****', ch, nfa.status_num, nfa.edge, nfa.input_set)
if nfa.edge == ch or (nfa.edge == CCL and ch in nfa.input_set):
print('debug in move 1****', ch, nfa.status_num, nfa.edge, nfa.input_set)
out_set.append(nfa.next_1)
Expand All @@ -68,4 +71,5 @@ def move(input_set, ch):
def has_accepted_state(nfa_set):
for nfa in nfa_set:
if nfa.next_1 is None and nfa.next_2 is None:
print('fuck******* ', nfa.status_num)
return True
18 changes: 15 additions & 3 deletions regex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
from parse.parse import compiler
from parse.parse import match

def regex(pattern, input_string):
pass

class Regex(object):
def __init__(self, input_string, pattern_string):
self.input_string = input_string
self.pattern_string = pattern_string

def match(self):
return match(self.input_string, self.pattern_string)

def replace():
pass

def search():
pass

0 comments on commit 376a497

Please sign in to comment.