gpt4 book ai didi

python - Lark解析器: No terminal defined for ':' (Seeming bias against colon character ":")

转载 作者:行者123 更新时间:2023-12-01 06:46:07 25 4
gpt4 key购买 nike

我有以下规则(取自 SMTP - RFC5321 ):

!path : "<" [ a_d_l ":" ] mailbox ">"

当我尝试解析这一行时:

<test.com:test.test@testtest.com>

我收到以下错误:

No terminal defined for ':'

不寻常的是,如果我简单地将 ":" 更改为 "_",它就会以某种方式起作用:

!path : "<" [ a_d_l "_" ] mailbox ">"
<test.com_test.test@testtest.com>

同样有效的是不包含该部分的行[ a_d_l ":"](这是可选的,如[]所示)

!path : "<" [ a_d_l ":" ] mailbox ">"
<test.test@testtest.com>

我已经尝试为冒号定义终端规则,但这也不起作用:

!path : "<" [ a_d_l COLON ] mailbox ">"
COLON : ":"
<test.test@testtest.com>

最小可重现示例:

按照评论中的要求。

from lark import Lark

grammar = r'''
!path : "<" [ a_d_l ":" ] mailbox ">"
a_d_l : at_domain ( "," at_domain )*
at_domain : "@" domain

domain : sub_domain ("." sub_domain)*
sub_domain : let_dig [ldh_str]
let_dig : ALPHA | DIGIT
!ldh_str : ( ALPHA | DIGIT | "-" )* let_dig
address_literal : "[" ( ipv4_address_literal | ipv6_address_literal | general_address_literal ) "]"
ipv4_address_literal : snum ("." snum)~3
snum : DIGIT~1..3
ipv6_address_literal : "ipv6:" ipv6_addr
ipv6_addr : ipv6_full | ipv6_comp | ipv6v4_full | ipv6v4_comp
ipv6_full : ipv6_hex (":" ipv6_hex)~7
ipv6_hex : HEXDIG~1..4
!ipv6_comp : [ipv6_hex (":" ipv6_hex)~0..5] "::" [ipv6_hex (":" ipv6_hex)~0..5]
!ipv6v4_full : ipv6_hex (":" ipv6_hex)~5 ":" ipv4_address_literal
!ipv6v4_comp : [ipv6_hex (":" ipv6_hex)~0..3] "::" [ipv6_hex (":" ipv6_hex)~0..3 ":"] ipv4_address_literal
!general_address_literal : standardized_tag ":" dcontent+
standardized_tag : ldh_str
dcontent : /[\x21-\x5A|\x5E-\x7E]/

mailbox : local_part /[\x40]/ ( domain | address_literal )
local_part : dot_string | quoted_string

dot_string : atom ("." atom)*
atom : atext+
quoted_string : /[\x22]/ qcontentsmtp* /[\x22]/
qcontentsmtp : qtextsmtp | quoted_pairsmtp
quoted_pairsmtp : /[\x5C\x5C]/ /[\x20-\x7E]/
qtextsmtp : /[\x20-\x21|\x23-\[\]-\x7E]/
atext : /[\x21|\x23-\x27|\x2A|\x2B|\x2D|\x2F-\x39|\x3D|\x3F|\x41-\x5A|\x5E-\x7E]/

command : [ path ]
%import common.WS -> SP
%import common.NEWLINE -> CRLF
%import common.DIGIT
%import common.LETTER -> ALPHA
%import common.HEXDIGIT -> HEXDIG'''

input = "<test.com:test.test@testtest.com>"

try:
result = Lark(grammar, start="command").parse(input)
except Exception as ex:
print('####### Parsing Failed')
print(ex)
traceback.print_exc()
result = None
return result

最佳答案

!path               : "<" [ a_d_l ":" ] mailbox ">"
a_d_l : at_domain ( "," at_domain )*
at_domain : "@" domain

仅匹配 "<@test.com:test.test@testtest.com>" 。它无法匹配"<test.com:test.test@testtest.com>"因为它不是以 "<" at_domain 开头或"<" mailbox .

关于python - Lark解析器: No terminal defined for ':' (Seeming bias against colon character ":"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59216854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com