1

这是第一次编写的标记器的优化版本,它工作得相当好。辅助标记器可以解析此函数的输出以创建更具体的分类标记。

def tokenize(source):
    return (token for token in (token.strip() for line
            in source.replace('\r\n', '\n').replace('\r', '\n').split('\n')
            for token in line.split('#', 1)[0].split(';')) if token)

我的问题是:如何简单地用re模块编写?以下是我无效的尝试。

def tokenize2(string):
    search = re.compile(r'^(.+?)(?:;(.+?))*?(?:#.+)?$', re.MULTILINE)
    for match in search.finditer(string):
        for item in match.groups():
            yield item

编辑:这是我从标记器中寻找的输出类型。解析文本应该很容易。

>>> def tokenize(source):
    return (token for token in (token.strip() for line
            in source.replace('\r\n', '\n').replace('\r', '\n').split('\n')
            for token in line.split('#', 1)[0].split(';')) if token)

>>> for token in tokenize('''\
a = 1 + 2; b = a - 3 # create zero in b
c = b * 4; d = 5 / c # trigger div error

e = (6 + 7) * 8
# try a boolean operation
f = 0 and 1 or 2
a; b; c; e; f'''):
    print(repr(token))


'a = 1 + 2'
'b = a - 3 '
'c = b * 4'
'd = 5 / c '
'e = (6 + 7) * 8'
'f = 0 and 1 or 2'
'a'
'b'
'c'
'e'
'f'
>>> 
4

2 回答 2

1

我可能离这里很远-

>>> def tokenize(source):
...     search = re.compile(r'^(.+?)(?:;(.+?))*?(?:#.+)?$', re.MULTILINE)
...     return (token.strip() for line in source.split('\n') if search.match(line)
...                   for token in line.split('#', 1)[0].split(';') if token)
... 
>>> 
>>> 
>>> for token in tokenize('''\
... a = 1 + 2; b = a - 3 # create zero in b
... c = b * 4; d = 5 / c # trigger div error
... 
... e = (6 + 7) * 8
... # try a boolean operation
... f = 0 and 1 or 2
... a; b; c; e; f'''):
...     print(repr(token))
... 
'a = 1 + 2'
'b = a - 3'
'c = b * 4'
'd = 5 / c'
'e = (6 + 7) * 8'
'f = 0 and 1 or 2'
'a'
'b'
'c'
'e'
'f'
>>> 

如果适用,我会将其re.compile排除在def范围之外。

于 2012-08-16T19:13:05.017 回答
1

这是基于您的 tokenize2 函数的一个:

def tokenize2(source):
    search = re.compile(r'([^;#\n]+)[;\n]?(?:#.+)?', re.MULTILINE)
    for match in search.finditer(source):
        for item in match.groups():
            yield item

>>> for token in tokenize2('''\
... a = 1 + 2; b = a - 3 # create zero in b
... c = b * 4; d = 5 / c # trigger div error
... 
... e = (6 + 7) * 8
... # try a boolean operation
... f = 0 and 1 or 2
... a; b; c; e; f'''):
...     print(repr(token))
... 
'a = 1 + 2'
' b = a - 3 '
'c = b * 4'
' d = 5 / c '
'e = (6 + 7) * 8'
'f = 0 and 1 or 2'
'a'
' b'
' c'
' e'
' f'
>>> 
于 2012-08-16T20:01:23.277 回答