编写一个脚本,将字符串输入匹配到非确定性有限自动机,我希望能够从命令行接收字符串,例如
python regex.py -nfa a.b|c -string ab
Result: True
但是当我尝试时,我得到了错误
Traceback (most recent call last): File "regex.py", line 144, in print(" Result:", format(match(str(args['nfa']), str(args['string'])))) TypeError: 'Namespace' object is not subscriptable
发生错误的代码如下所示:
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-nfa", required=True, help="NFA",type=str)
parser.add_argument("-string", required=True, help="String to match to nfa",type=str)
parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS, help='Default Help Message of Project Command line')
args = parser.parse_args()
print(" Result:", match(str(args['nfa']), str(args['string'])))
匹配():
def match(regex, s):
"""Match a string to an NFA"""
#this function will return true if and only if the regular expression
#regex (fully) matches the string s. It returns false otherwise
#compile the regular expression into nfa
nfa = compile(regex)
# try to match the regular expression to the string s.
#the current set of states
current = set()
#add the first state, and follow all of epsilon arrows
followEs(nfa.start, current)
#the previuos set of states
previous = set()
#loop through characters in s
for c in s:
previous = current
#creat a new empty set for states we're about to be in
current = set()
#loop through the previous states
for state in previous:
#only follow arrows not labeled by E (epsilon)
if state.label is not None:
#if the label of the state is = to the character we've read
if state.label == c:
#add the state(s) at the end of the arrow to current.
followEs(state.edges[0], current)
#ask the nfa if it matches the string s.
return nfa.accept in current
编译():
def compile(infix):
"""Return Fragment of NFA that represents infix regex"""
#convert infix to postfix
postfix = shunting.shunt(infix)
#make a listfix stack of chars and invert it
postfix=list(postfix)[::-1]
#stack for nfa fragments
nfa_stack = []
while(postfix):
#pop character from postfix
c= postfix.pop()
if(c=='.'):#pop two frags off stack
frag1=nfa_stack.pop()
frag2=nfa_stack.pop()
#point from end of frag2 to start of frag1
frag2.accept.edges.append(frag1.start)
#frag2's start state is new start state
start = frag2.start
#frag1s new accept state
accept = frag1.accept
elif(c=='|' ):
#pop 2 frags off stack
frag1=nfa_stack.pop()
frag2=nfa_stack.pop()
#create new start and accept states
accept = State()
start= State(edges=[frag2.start,frag1.start])
#point old accept states at new one
frag2.accept.edges.append(accept)
frag1.accept.edges.append(accept)
elif(c=='*'):
#pop single fragment off stack
frag= nfa_stack.pop()
#create new start and accept state
accept = State()
#point arrows i
start= State(edges=[frag.start,accept])
frag.accept.edges=[frag.start,accept]
else:
accept = State()
start = State(label=c,edges=[accept])
#create new instance of fragment to represent the new nfa
newfrag = Fragment(start,accept)
#push new nfa to stack
nfa_stack.append(newfrag)
#the nfa stack should have exactly one nfa one it - the answer
return nfa_stack.pop()
args
是命名空间对象,而不是字典您应该访问作为属性提供的参数:
args.arg1, args.arg2, etc..
因此,改变:
致:
如果要将
args
转换为字典,可以执行以下操作:相关问题 更多 >
编程相关推荐