用于解析到lin结尾的Lexer

2024-10-03 13:19:03 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有一个关键字,当它遇到一个关键字时,我如何才能让它只抓取行的其余部分并将其作为字符串返回?一旦遇到行尾,返回该行的所有内容。在

我要看的是:

  description here is the rest of my text to collect

因此,当lexer遇到description时,我希望以字符串的形式返回“这是要收集的其余文本”

我定义了以下内容,但似乎抛出了一个错误:

^{pr2}$

这是返回的错误的一部分:

  File "/Users/me/Coding/wm/wm_parser/ply/lex.py", line 393, in token
raise LexError("Illegal character '%s' at index %d" % (lexdata[lexpos],lexpos), lexdata[lexpos:])
ply.lex.LexError: Illegal character ' ' at index 40

最后,如果我想为不止一个令牌使用这个功能,我该如何实现呢?在

谢谢你的时间


Tags: 字符串index错误关键字descriptionatplywm
2条回答

你的代码没有什么大问题,事实上,我只是复制你的代码并运行它,它运行得很好

import ply.lex as lex 

states = ( 
     ('bcdescription', 'exclusive'),
)

tokens = ("BCDESCRIPTION",)

def t_bcdescription(t):
    r'\bdescription\b'
    t.lexer.code_start = t.lexer.lexpos
    t.lexer.level = 1 
    t.lexer.begin('bcdescription')

def t_bcdescription_close(t):
    r'\n'
    t.value = t.lexer.lexdata[t.lexer.code_start:t.lexer.lexpos+1]
    t.type="BCDESCRIPTION"
    t.lexer.lineno += t.value.count('\n')
    t.lexer.begin('INITIAL')
    return t

def t_bcdescription_content(t):
    r'[^\n]+'

lexer = lex.lex()
data = 'description here is the rest of my text to collect\n'
lexer.input(data)

while True:
    tok = lexer.token()
    if not tok: break      
    print tok 

结果是:

^{pr2}$

所以也许你可以检查代码的其他部分

如果我想让这个功能用于多个令牌,那么您可以简单地捕获单词,当这些令牌中出现一个单词时,开始通过上面的代码捕获其余内容。在

不清楚为什么需要使用lexer/parser来完成这个任务,而不需要进一步的信息。在

>>> x = 'description here is the rest of my text to collect'
>>> a, b = x.split(' ', 1)
>>> a
'description'
>>> b
'here is the rest of my text to collect'

相关问题 更多 >