gpt4 book ai didi

python - lark-parser 缩进 DSL 和多行文档字符串

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:19 29 4
gpt4 key购买 nike

我正在尝试使用 lark 实现记录定义 DSL .它基于缩进,这让事情变得有点复杂。

Lark 是一个很棒的工具,但我遇到了一些困难。

这是我正在实现的 DSL 的一个片段:

record Order :
"""Order record documentation
should have arbitrary size"""

field1 Int
field2 Datetime:
"""Attributes should also have
multiline documentation"""

field3 String "inline documentation also works"

这里是使用的语法:

?start: (_NEWLINE | redorddef)*

simple_type: NAME

multiline_doc: MULTILINE_STRING _NEWLINE
inline_doc: INLINE_STRING

?element_doc: ":" _NEWLINE _INDENT multiline_doc _DEDENT | inline_doc

attribute_name: NAME
attribute_simple_type: attribute_name simple_type [element_doc] _NEWLINE
attributes: attribute_simple_type+
_recordbody: _NEWLINE _INDENT [multiline_doc] attributes _DEDENT
redorddef: "record" NAME ":" _recordbody



MULTILINE_STRING: /"""([^"\\]*(\\.[^"\\]*)*)"""/
INLINE_STRING: /"([^"\\]*(\\.[^"\\]*)*)"/

_WS_INLINE: (" "|/\t/)+
COMMENT: /#[^\n]*/
_NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+

%import common.CNAME -> NAME
%import common.INT

%ignore /[\t \f]+/ // WS
%ignore /\\[\t \f]*\r?\n/ // LINE_CONT
%ignore COMMENT
%declare _INDENT _DEDENT

它适用于记录定义的多行字符串文档,适用于内联属性定义,但不适用于属性多行字符串文档。

我用来执行的代码是这样的:

import sys
import pprint

from pathlib import Path

from lark import Lark, UnexpectedInput
from lark.indenter import Indenter

scheman_data_works = '''
record Order :
"""Order record documentation
should have arbitrary size"""

field1 Int
# field2 Datetime:
# """Attributes should also have
# multiline documentation"""

field3 String "inline documentation also works"
'''

scheman_data_wrong = '''
record Order :
"""Order record documentation
should have arbitrary size"""

field1 Int
field2 Datetime:
"""Attributes should also have
multiline documentation"""

field3 String "inline documentation also works"
'''
grammar = r'''

?start: (_NEWLINE | redorddef)*

simple_type: NAME

multiline_doc: MULTILINE_STRING _NEWLINE
inline_doc: INLINE_STRING

?element_doc: ":" _NEWLINE _INDENT multiline_doc _DEDENT | inline_doc

attribute_name: NAME
attribute_simple_type: attribute_name simple_type [element_doc] _NEWLINE
attributes: attribute_simple_type+
_recordbody: _NEWLINE _INDENT [multiline_doc] attributes _DEDENT
redorddef: "record" NAME ":" _recordbody



MULTILINE_STRING: /"""([^"\\]*(\\.[^"\\]*)*)"""/
INLINE_STRING: /"([^"\\]*(\\.[^"\\]*)*)"/

_WS_INLINE: (" "|/\t/)+
COMMENT: /#[^\n]*/
_NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+

%import common.CNAME -> NAME
%import common.INT

%ignore /[\t \f]+/ // WS
%ignore /\\[\t \f]*\r?\n/ // LINE_CONT
%ignore COMMENT
%declare _INDENT _DEDENT

'''

class SchemanIndenter(Indenter):
NL_type = '_NEWLINE'
OPEN_PAREN_types = ['LPAR', 'LSQB', 'LBRACE']
CLOSE_PAREN_types = ['RPAR', 'RSQB', 'RBRACE']
INDENT_type = '_INDENT'
DEDENT_type = '_DEDENT'
tab_len = 4

scheman_parser = Lark(grammar, parser='lalr', postlex=SchemanIndenter())
print(scheman_parser.parse(scheman_data_works).pretty())
print("\n\n")
print(scheman_parser.parse(scheman_data_wrong).pretty())

结果是:

redorddef
Order
multiline_doc """Order record documentation
should have arbitrary size"""
attributes
attribute_simple_type
attribute_name field1
simple_type Int
attribute_simple_type
attribute_name field3
simple_type String
inline_doc "inline documentation also works"




Traceback (most recent call last):
File "schema_parser.py", line 83, in <module>
print(scheman_parser.parse(scheman_data_wrong).pretty())
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/lark.py", line 228, in parse
return self.parser.parse(text)
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/parser_frontends.py", line 38, in parse
return self.parser.parse(token_stream, *[sps] if sps is not NotImplemented else [])
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/parsers/lalr_parser.py", line 68, in parse
for token in stream:
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/indenter.py", line 31, in process
for token in stream:
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/lexer.py", line 319, in lex
for x in l.lex(stream, self.root_lexer.newline_types, self.root_lexer.ignore_types):
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/lexer.py", line 167, in lex
raise UnexpectedCharacters(stream, line_ctr.char_pos, line_ctr.line, line_ctr.column, state=self.state)
lark.exceptions.UnexpectedCharacters: No terminal defined for 'f' at line 11 col 2

field3 String "inline documentation also
^

我不明白缩进语法更复杂, lark 似乎让它变得更容易,但找不到这里的错误。

PS:我也尝试过 pyparsing,但在同样的情况下没有成功,考虑到可能需要的代码量,我很难转移到 PLY。

最佳答案

错误来自错误放置的 _NEWLINE 终端。通常,建议确保规则在语法中的作用方面是平衡的。所以这里是你应该如何定义 element_doc:

?element_doc:  ":" _NEWLINE _INDENT multiline_doc _DEDENT
| inline_doc _NEWLINE

注意添加的换行符,这意味着无论解析器采用两个选项中的哪一个,它都会以相似的状态结束,语法方面(_DEDENT 也匹配换行符)。

第二个变化是第一个变化的结果:

attribute_simple_type: attribute_name simple_type (element_doc|_NEWLINE)

由于 element_doc 已经处理换行符,我们不应该尝试匹配它两次。

关于python - lark-parser 缩进 DSL 和多行文档字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54669033/

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