我试图简化another question中提出的情况,并使用lark进行了以下解析尝试:
from lark.lark import Lark
text = """
start_thing {
loc int {
from 0,
to 1093,
strand plus,
id gi 384632836
}
}
"""
grammar = """\
thing: "start_thing" node
locus_info: "loc int" "{" int_info "," int_info "," STRAND_INFO "," int_info "}"
int_info: TAGS? INT
node: locus_info
| int_info
| TAGS? "{" nodes "}" -> subnodes
| TAGS -> onlytags
nodes: node?
| node ("," node)*
STRAND_INFO: "strand" SIGN
SIGN: "plus" | "minus"
TAGS: TAGWORD (WS TAGWORD)*
TAGWORD: ("_"|LETTER)("_"|"-"|LETTER|DIGIT)*
%import common.WS
%import common.LETTER
%import common.DIGIT
%import common.INT
%ignore WS
"""
parser = Lark(grammar, start="thing", ambiguity="explicit")
parsed = parser.parse(text)
print(parsed.pretty())
输出:
thing
subnodes
nodes
subnodes
loc int
nodes
node
int_info
from
0
node
int_info
to
1093
onlytags strand plus
node
int_info
id gi
384632836
如this example所示,ambiguity="explicit"
选项应允许显示替代匹配的可能性,并在其前加上_ambig
标签。这不会出现在上面的输出中。看来我没有什么含糊之处。
为什么“ strand plus”不被认为是模棱两可的?在我看来,它可以与STRAND_INFO
或onlytags
匹配。
类似地,我希望“ loc int {...}”与locus_info
或subnodes
匹配。