简体   繁体   English

解析Python中的令牌列表

[英]Parsing a list of tokens in Python

GOAL: I have a list of tokens. 目标:我有一张代币清单。 Whenever there is a section of tokens surrounded by brackets, such as {t1,t2,etc.}, I need to make that into a new sublist. 每当有一段用括号括起来的标记(例如{t1,t2等)}时,我都需要将其放入新的子列表中。 An example is provided below of my desired result. 下面提供了我想要的结果的一个示例。


parse:['(facto)', 'dup', 'length', '/n', 'exch', 'def', '/fact', '{', '0', 'dict', 'begin', '/n', 'exch', 'def', 'n', '2', 'lt', '{', '1', '}', '{', 'n', '1', 'sub', 'fact', 'n', 'mul', '}', 'ifelse', 'end', '}', 'def', 'n', 'fact', 'stack']) 解析:['(facto)','dup','length','/ n','exch','def','/ fact','{','0','dict','begin' ,'/ n','exch','def','n','2','lt','{','1','}','{','n','1', 'sub','fact','n','mul','}','ifelse','end','}','def','n','fact','stack']))

returns: ['(facto)', 'dup', 'length', '/n', 'exch', 'def', '/fact', [0, 'dict', 'begin', '/n', 'exch', 'def', 'n', 2, 'lt', [1], ['n', 1, 'sub', 'fact', 'n', 'mul'], 'ifelse', 'end'], 'def', 'n', 'fact', 'stack'] 返回:['(facto)','dup','length','/ n','exch','def','/ fact',[0,'dict','begin','/ n' ,'exch','def','n',2,'lt',[1],['n',1,'sub','事实','n','mul'],'ifelse' ,'end'],'def','n','fact','stack']


Here is my code so far: 到目前为止,这是我的代码:

def parse1(L):
    newL = []
    for x in L:
        if x == '}':
            return newL
        elif x == '{': 
            newL.append(parse1(L[1:]))
        else:
            newL.append(x)
    return newL

It works to the extent that whenever a { occurs, I reclusively pass the rest of the list into the function again, with a base case being when } occurs. 它的工作范围是,每当{出现时,我就会分别将列表的其余部分再次传递给函数,基本情况是}发生。 This is working okay, but once it exits the recursion and creates a sublist, the element "x" it's iterating through has not gone past this section. 这样做可以,但是一旦退出递归并创建了子列表,则要遍历的元素“ x”并没有超出本节。 So for example, if our list is: ['{', '1', '}'], the result should simply be [[1]]. 因此,例如,如果我们的列表为:['{','1','}'],则结果应简单为[[1]]。 However, what is happening is it's returning [[1],'1'], because once it creates the sublist (which seems to be working fine) the next element "x" the loop is going through is actually an element of that sublist, it's the element right after the '{', and according to my code, it's then appended to the list. 但是,正在发生的事情是它返回[[1],'1'],因为一旦创建了子列表(似乎工作正常),循环要经过的下一个元素“ x”实际上就是该子列表的元素,它是'{'之后的元素,根据我的代码,它随后被附加到列表中。

I feel like this is a really easy fix, I have spent very long trying to figure it out though. 我觉得这是一个非常容易解决的问题,尽管我花了很长时间试图弄清楚。 I understand the issues, as I have explained, but cannot for the life of me figure out how to fix it. 正如我已经解释的那样,我理解这些问题,但是我一生无法解决该问题。 Any help would be greatly appreciated! 任何帮助将不胜感激!

This is modeled on your attempt at a solution (and also handles the integers): 这是基于您尝试解决方案的模型(并处理整数):

# This assumes brackets are properly balanced
def parse1(L):
    newL = []
    i = 0
    while i<len(L):
        x = L[i]
        if x == '}':
            # Return the parsed list & the unparsed portion of the original list
            print(newL, L[i:])
            return newL, L[i+1:]
        elif x == '{': 
            # Split rest of L into parsed & unparsed portions
            parsed, unparsed = parse1(L[i+1:])
            # Insert parsed portion into current list
            newL.append(parsed)
            # Reset i & L for unparsed portion
            i, L = 0, unparsed
        else:
            # Convert x to an integer if possible
            try:
                x = int(x)
            except:
                pass
            newL.append(x)
            i += 1
    return newL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM