#!/usr/bin/python
# -*- coding: UTF-8 -*-
def addcompile(nums, tokens):
# 对于异常的保护
add_stack = []
if nums == 0:
return
if tokens[0] == 'add':
add_stack.append(1)
if nums == 1 and tokens[0] != 'add':
return 'invalid input'
# 如果遇到add,则取出站顶元素,如果是数字则取和入站,
sum = 0
for token in tokens[1:nums]:
if token == 'add':
if isinstance(add_stack[-1], int):
top_value = add_stack.pop()
add_stack.append(top_value + 1)
else:
continue
if 'for' in token:
add_stack.append(token)
if 'end' == token:
# 找到前置终结符号for
print add_stack
top_value = add_stack.pop()
key_token = add_stack.pop()
print add_stack
key_tokenlist = key_token.split()
if key_tokenlist[0] == 'for':
add_stack.append(key_tokenlist[1] * top_value)
print add_stack
return add_stack[0]
def read_tokens(nums, text):
tokens = []
if nums == 0:
return
raw_list = text.split('\n')
for token in raw_list:
element = token.strip()
if element == '':
continue
tokens.append(element)
print tokens
return addcompile(nums, tokens)
if __name__ == '__main__':
case = """add
add
add
"""
case_1 = """add
"""
case_for = """
add
for 100
add
end
"""
print read_tokens(4, case_for)
版本2
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def addcompile(nums, tokens):
# 对于异常的保护
add_stack = []
if nums == 0:
return
# if tokens[0] == 'add':
# add_stack.append(1)
if nums == 1 and tokens[0] != 'add':
return 'invalid input'
# 如果遇到add,则取出站顶元素,如果是数字则取和入站,
# 如果首行非add未考虑
sum = 0
for token in tokens:
# 如果遇到add,前一个不是数字
if token == 'add':
if len(add_stack) == 0:
add_stack.append(1)
continue
if isinstance(add_stack[-1], int):
top_value = add_stack.pop()
add_stack.append(top_value + 1)
else:
add_stack.append(1)
if 'for' in token:
add_stack.append(token)
# 对于if条件为true,false做判断
# if 为false,不计算
if 'if' in token:
split_if_token = token.split()
if split_if_token[1]:
add_stack.append(token)
if 'end' == token:
# 找到前置终结符号for
top_value = add_stack.pop()
key_token = add_stack.pop()
key_tokenlist = key_token.split()
#print int(key_tokenlist[1]) * top_value
# 如果前面有数字,则加入:
tmp_value = 0
if len(add_stack) > 0 and isinstance(add_stack[-1], int):
tmp_value = add_stack.pop()
if key_tokenlist[0] == 'for':
add_stack.append(int(key_tokenlist[1]) * top_value + tmp_value)
if key_tokenlist[0] == 'if':
add_stack.append(top_value + tmp_value)
return add_stack[0]
def max_flow(data):
if data:
pass
return 'maxflow'
def read_tokens(nums, text):
tokens = []
if nums == 0:
return
raw_list = text.split('\n')
for token in raw_list:
element = token.strip()
if element == '':
continue
tokens.append(element)
return addcompile(nums, tokens)
if __name__ == '__main__':
case = """add
add
add
"""
case_1 = """add
"""
case_for = """
if 1
for 100
for 100
for 100
for 100
add
end
end
end
end
end
"""
print read_tokens(11, case_for)
版本3
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def addcompile(nums, tokens):
# 对于异常的保护
add_stack = []
if nums == 0:
return
# if tokens[0] == 'add':
# add_stack.append(1)
if nums == 1 and tokens[0] != 'add':
return 'invalid input'
# 如果遇到add,则取出站顶元素,如果是数字则取和入站,
# 如果首行非add未考虑
sum = 0
for token in tokens:
print 'element', token
# 如果遇到add,前一个不是数字
if token == 'add':
if len(add_stack) == 0:
add_stack.append(1)
continue
if isinstance(add_stack[-1], int):
top_value = add_stack.pop()
add_stack.append(top_value + 1)
else:
add_stack.append(1)
if 'for' in token:
add_stack.append(token)
# 对于if条件为true,false做判断
# if 为false,不计算
if 'if' in token:
print 'if', add_stack
# split_if_token = token.split()
# if split_if_token[1]:
add_stack.append(token)
if 'end' == token:
# 找到前置终结符号for
top_value = add_stack.pop()
key_token = add_stack.pop()
key_tokenlist = key_token.split()
print 'end',add_stack
# 如果前面有数字,则加入:
tmp_value = 0
if len(add_stack) > 0 and isinstance(add_stack[-1], int):
tmp_value = add_stack.pop()
if key_tokenlist[0] == 'for':
print key_tokenlist
add_stack.append(int(key_tokenlist[1]) * top_value + tmp_value)
if key_tokenlist[0] == 'if' and int(key_tokenlist[1]):
add_stack.append(top_value + tmp_value)
#站顶是数字还是for,if,需要做判断
if key_tokenlist[0] == 'if' and not int(key_tokenlist[1]):
add_stack.append(tmp_value)
print add_stack
return add_stack[0] if add_stack else 0
def max_flow(data):
if data:
pass
return 'maxflow'
def read_tokens(nums, text):
tokens = []
if nums == 0:
return
raw_list = text.split('\n')
for token in raw_list:
element = token.strip()
if element == '':
continue
tokens.append(element)
print 'tokens', tokens
return addcompile(nums, tokens)
if __name__ == '__main__':
case = """add
add
add
"""
case_1 = """add
"""
case_for = """
for 100
for 100
add
if 0
for 100
for 100
add
end
end
end
end
end
"""
case_9="""
add
if 0
for 100
add
end
end
for 2
add
end
"""
print read_tokens(9, case_9)
版本4,剪纸,去除计算,加个flag
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def token_add(add_satck):
pass
# 类似于四则运算
def addcompile(nums, tokens):
# 对于异常的保护
add_stack = []
if nums == 1 and tokens[0] != 'add':
return 'invalid input'
# 如果遇到add,则取出栈顶元素,如果是数字则取和入站,
# 如果首行非add未考虑
# false_flag = False
for token in tokens:
# 如果遇到add,前一个不是数字
if token == 'add':
# 首个关键字进行处理
if len(add_stack) == 0:
add_stack.append(1)
continue
# 前一个是数字,则取出前一个数字,相加入栈,否则取1入栈。
if isinstance(add_stack[-1], int):
top_value = add_stack.pop()
# false 不运算,但是入栈
# if false_flag:
# add_stack.append(0)
# continue
add_stack.append(top_value + 1)
else:
add_stack.append(1)
if 'for' in token:
add_stack.append(token)
# 对于if条件为true,false做判断
# if 为false,不计算
if 'if' in token:
print 'if', add_stack
# split_if_token = token.split()
# if not split_if_token[1]:
# false_flag = True
add_stack.append(token)
if 'end' == token:
# 取出前两个栈元素,按理说一个是整数,一个是符号,for or if
top_value = add_stack.pop()
key_token = add_stack.pop()
key_tokenlist = key_token.split()
print 'end', add_stack
# 如果前面有数字,则加入:
tmp_value = 0
# 考虑符号前面可能整数情况
if len(add_stack) > 0 and isinstance(add_stack[-1], int):
tmp_value = add_stack.pop()
# for 循环,需要乘以该循环倍数
if key_tokenlist[0] == 'for':
# if false_flag:
# add_stack.append(0)
# continue
add_stack.append(int(key_tokenlist[1]) * top_value + tmp_value)
# if true 和false的情况
if key_tokenlist[0] == 'if' and int(key_tokenlist[1]):
add_stack.append(top_value + tmp_value)
# 站顶是数字还是for,if,需要做判断
if key_tokenlist[0] == 'if' and not int(key_tokenlist[1]):
add_stack.append(tmp_value)
print add_stack
return add_stack[0] if add_stack else 0
def max_flow(data):
if data:
pass
return 'maxflow'
def read_tokens(nums, text):
tokens = []
if nums == 0: return
raw_list = text.split('\n')
for token in raw_list:
element = token.strip()
if element == '': continue
tokens.append(element)
return addcompile(nums, tokens)
if __name__ == '__main__':
case = """add
add
add
"""
case_1 = """add
"""
case_for = """
for 100
for 100
add
if 0
for 100
for 100
add
end
end
end
end
end
"""
case_9 = """
if 0
for 100
add
end
end
for 2
add
end
if 0
add
add
add
end
"""
print read_tokens(9, case_9)