![](/img/trans.png)
[英]how to make a recursive function use while loops if the original function had many recursive calls?
[英]Make a while parsing function recursive
我有一個基本的 function 來解析一個 lisp 表達式。 它使用了一個while
循環,但作為一個練習,我想將它轉換為遞歸function。 但是,這對我來說有點棘手。 到目前為止,這是我所擁有的:
def build_ast(self, tokens=None):
# next two lines example input to make self-contained
LEFT_PAREN, RIGHT_PAREN = '(', ')'
tokens = ['(', '+', '2', '(', '*', '3', '4', ')', ')']
while RIGHT_PAREN in tokens:
right_idx = tokens.index(RIGHT_PAREN)
left_idx = right_idx - tokens[:right_idx][::-1].index(LEFT_PAREN)-1
extraction = [tokens[left_idx+1:right_idx],]
tokens = tokens[:left_idx] + extraction + tokens[right_idx+1:]
ast = tokens
return ast
所以它會解析這樣的東西:
(+ 2 (* 3 4))
進入這個:
[['+', '2', ['*', '3', '4']]]
什么是我如何使上述 function 遞歸的例子? 到目前為止,我已經從以下內容開始:
def build_ast(self, ast=None):
if ast is None: ast=self.lexed_tokens
if RIGHT_PAREN not in ast:
return ast
else:
right_idx = ast.index(RIGHT_PAREN)
left_idx = right_idx - ast[:right_idx][::-1].index(LEFT_PAREN)-1
ast = ast[:left_idx] + [ast[left_idx+1:right_idx],] + ast[right_idx+1:]
return self.build_ast(ast)
但它只是有點奇怪(好像遞歸在這里沒有幫助)。 有什么更好的方法來構建它? 或者也許是一個更好/更優雅的算法來構建這個簡單的 ast?
您可以使用遞歸生成器 function:
def _build_ast(tokens):
LEFT_PAREN, RIGHT_PAREN = '(', ')'
#consume the iterator until it is empty or a right paren occurs
while (n:=next(tokens, None)) is not None and n != RIGHT_PAREN:
#recursively call _build_ast if we encounter a left paren
yield n if n != LEFT_PAREN else list(_build_ast(tokens))
def build_ast(tokens):
#pass tokens as an iterator to _build_ast
return list(_build_ast(iter(tokens)))
tokens = ['(', '+', '2', '(', '*', '3', '4', ')', ')']
print(build_ast(tokens))
Output:
[['+', '2', ['*', '3', '4']]]
與其他答案類似,我會將結束當前表達式的令牌傳遞給遞歸 function。 這通常是右括號,但對於第一次調用,它將是輸入結束(無)。
def build_ast(tokens):
LEFT_PAREN, RIGHT_PAREN = '(', ')'
it = iter(tokens) # Iterator over the input
# Recursive (generator) function that processes tokens until the close
# of the expression, i.e until the given token is encountered
def recur(until=RIGHT_PAREN):
# Keep processing tokens until closing token is encountered
while (token := next(it, None)) != until:
# If parenthesis opens, recur and convert to list
# otherwise just yield the token as-is
yield list(recur()) if token == LEFT_PAREN else token
# Main recursive call: process until end of input (i.e. until None)
return list(recur(None))
調用為:
ast = build_ast(['(', '+', '2', '(', '*', '3', '4', ')', ')'])
其他兩種方法都很棒,這里還有一種:
# Helper function: pop from left or return default
pops = lambda l, d=None: l.pop(0) if l else d
def read_from_tokens(tokens):
L = []
while (token := pops(tokens, ')')) != ')':
L.append(token if token!='(' else read_from_tokens(tokens))
return L
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.