Recursive parsing of flat list as nested structure时转义零处理异常求助
Recursive parsing of flat list as nested structure时转义零处理异常求助
这是之前一个关于「如何将扁平列表解析为嵌套结构」问题的后续,了解那个问题的详细说明能帮助你更好理解当前的场景。
当前代码
def parse(code: list, top=True) -> tuple: print(code) result = [] stack = [] i = 0 while i < len(code): nonzero_tokens = [] for index, token in enumerate(code[i:], start=i): if not top or (token[0] != 0 or (len(code) <= index + 1 or code[index + 1][0] == 0)): nonzero_tokens.append((token, index)) if len(nonzero_tokens) == 4: break key1, key2, key3, pos = nonzero_tokens if stack: if top and (len(code) > pos[1] + 1 and code[pos[1] + 1][0] == 0) and (len(code) <= pos[1] + 2 or code[pos[1] + 2][0] != 0): result[-1][-1].append(key1[0]) i = key1[1] + 1 elif pos[0][0] == 1: stack.append((key1[0][0], key2[0][0], key3[0][0])) result[-1][-1] += [key1[0], key2[0], key3[0], pos[0]] i = pos[1] + 1 elif (key1[0][0], key2[0][0], key3[0][0]) == stack[-1]: if pos[0][0] == -1: if len(stack) > 1: result[-1][-1] += [key1[0], key2[0], key3[0], pos[0]] stack.pop() i = pos[1] + 1 else: if len(stack) == 1: result[-1].append([pos[0]]) else: result[-1][-1] += [key1[0], key2[0], key3[0], pos[0]] i = pos[1] + 1 else: result[-1][-1].append(key1[0]) i = key1[1] + 1 else: if pos[0][0] == 1: result.append([(key1[0], key2[0], key3[0]), [1]]) stack.append((key1[0][0], key2[0][0], key3[0][0])) i = pos[1] + 1 else: raise SyntaxError(f'function {key1[0][0]}.{key2[0][0]}.{key3[0][0]} at line {key1[0][1][0]}, position {key1[0][1][1]} has no opening instruction {key1[0][0]}.{key2[0][0]}.{key3[0][0]}.1') for i, call in enumerate(result): new_call = [call[0], {}] for argument in call[1:]: try: new_call[1][argument[0]] = Parser.parse(argument[1:], top=False)[0] except: new_call[1][argument[0]] = argument[1:] result[i] = new_call return result, stack
示例输入
[(3, (1, 1)), (2, (1, 3)), (1, (1, 5)), (1, (1, 7)), (5, (2, 2)), (3, (2, 4)), (1, (2, 6)), (1, (2, 8)), (72, (2, 12)), (101, (2, 16)), (108, (2, 21)), (108, (2, 26)), (111, (2, 31)), (44, (2, 36)), (32, (2, 40)), (87, (2, 44)), (111, (2, 48)), (114, (2, 53)), (108, (2, 58)), (109, (2, 63)), (33, (2, 68)), (5, (3, 2)), (3, (3, 4)), (1, (3, 6)), (0, (3, 8)), (-1, (3, 11)), (3, (4, 1)), (2, (4, 3)), (1, (4, 5)), (-1, (4, 8))]
参考源文件
3.2.1.1 = Start of print 5.3.1.1 = 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 109, 33 Makes the string "Hello, World!" 5.3.1.0.-1 End of string 3.2.1.-1 ^ End of print # Escape characters so that 33.5.3.1 is not interpreted as a function call ^ Comment marker because the above comment contains numbers
预期输出
[[((3, (1, 1)), (2, (1, 3)), (1, (1, 5))), {1: [[((5, (2, 2)), (3, (2, 4)), (1, (2, 6))), {1: (72, (2, 12)), (101, (2, 16)), (108, (2, 21)), (108, (2, 26)), (111, (2, 31)), (44, (2, 36)), (32, (2, 40)), (87, (2, 44)), (111, (2, 48)), (114, (2, 53)), (108, (2, 58)), (109, (2, 63)), (33, (2, 68))}]]}]]
实际输出
[[((3, (1, 1)), (2, (1, 3)), (1, (1, 5))), {1: [(5, (2, 2)), (3, (2, 4)), (1, (2, 6)), (1, (2, 8)), (72, (2, 12)), (101, (2, 16)), (108, (2, 21)), (108, (2, 26)), (111, (2, 31)), (44, (2, 36)), (32, (2, 40)), (87, (2, 44)), (111, (2, 48)), (114, (2, 53)), (108, (2, 58)), (109, (2, 63)), (33, (2, 68)), (5, (3, 2)), (3, (3, 4)), (1, (3, 6)), (-1, (3, 11))]}]]
问题说明
函数的结构是:每个函数都有一个三位数字的标识符,后面跟着一个位置指示器,位置为-1时表示关闭该函数。我的这个解析函数会先解析顶层的函数,然后将每个函数的参数递归传递给自身,以解析嵌套的函数。
零作为转义字符的规则是:
- 任何类函数序列后面跟一个零,都不会被当作函数处理
- 零在其他情况下会被跳过
- 两个连续的零(0 0)会转义零本身,不会用于转义函数,而是作为普通的0数据处理
现在的问题很明显:有一个零用来转义33.5.3.1,但这个零没有被传递到递归调用中。总的来说,转义用的零只被顶层函数处理一次后就被移除了,在递归调用中完全无法生效。
这个函数已经叠了好几个修复逻辑,现在变得极其复杂,实在不知道该怎么解决这个问题了,恳请各位帮忙!
备注:内容来源于stack exchange,提问作者Eric Wang
相关产品推荐
相关产品推荐

