如何将 EOL(换行符)与百灵鸟相匹配?
how does one match EOL (newline) with lark?
我在 python 中使用 lark 解析器。我想使用 EOL 作为语法的一部分,因为它是面向行的。当我尝试将正则表达式放入匹配 EOL 时出现错误。我看到一些这样的例子:
CR : /\r/
LF : /\n/
NEWLINE: (CR? LF)+
但它们对我不起作用。这是我的代码:
import sys
import lark
class Parser:
grammar = '''
start : STRING EOL
STRING : /\w+/
EOL : /\n/x
'''
parser = lark.Lark(grammar)
def __init__(self, fname):
self.fname = fname
self.ast(open(fname))
def ast(self, fh):
tree = self.parser.parse(fh.read())
print(tree.pretty())
def main():
x = Parser(sys.argv[1])
main()
这是我得到的错误:
Traceback (most recent call last):
File "./p2.py", line 6, in <module>
class Parser:
File "./p2.py", line 13, in Parser
parser = lark.Lark(grammar)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/lark.py", line 413, in __init__
self.parser = self._build_parser()
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/lark.py", line 456, in _build_parser
return parser_class(self.lexer_conf, parser_conf, options=self.options)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/parser_frontends.py", line 242, in __call__
return ParsingFrontend(lexer_conf, parser_conf, options)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/parser_frontends.py", line 61, in __init__
self.parser = create_parser(lexer_conf, parser_conf, options)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/parser_frontends.py", line 209, in create_earley_parser
return f(lexer_conf, parser_conf, options, resolve_ambiguity=resolve_ambiguity, debug=debug, tree_class=tree_class, **extra)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/parser_frontends.py", line 186, in create_earley_parser__dynamic
earley_matcher = EarleyRegexpMatcher(lexer_conf)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/parser_frontends.py", line 172, in __init__
raise GrammarError("Dynamic Earley doesn't allow zero-width regexps", t)
lark.exceptions.GrammarError: ("Dynamic Earley doesn't allow zero-width regexps", TerminalDef('EOL', '(?x:\n)'))
我忘了需要使用原始字符串。不得不加一个“r”。新代码如下所示:
class Parser:
grammar = r'''
start : STRING EOL
STRING : /\w+/
EOL : /\n/
'''
我在 python 中使用 lark 解析器。我想使用 EOL 作为语法的一部分,因为它是面向行的。当我尝试将正则表达式放入匹配 EOL 时出现错误。我看到一些这样的例子:
CR : /\r/
LF : /\n/
NEWLINE: (CR? LF)+
但它们对我不起作用。这是我的代码:
import sys
import lark
class Parser:
grammar = '''
start : STRING EOL
STRING : /\w+/
EOL : /\n/x
'''
parser = lark.Lark(grammar)
def __init__(self, fname):
self.fname = fname
self.ast(open(fname))
def ast(self, fh):
tree = self.parser.parse(fh.read())
print(tree.pretty())
def main():
x = Parser(sys.argv[1])
main()
这是我得到的错误:
Traceback (most recent call last):
File "./p2.py", line 6, in <module>
class Parser:
File "./p2.py", line 13, in Parser
parser = lark.Lark(grammar)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/lark.py", line 413, in __init__
self.parser = self._build_parser()
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/lark.py", line 456, in _build_parser
return parser_class(self.lexer_conf, parser_conf, options=self.options)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/parser_frontends.py", line 242, in __call__
return ParsingFrontend(lexer_conf, parser_conf, options)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/parser_frontends.py", line 61, in __init__
self.parser = create_parser(lexer_conf, parser_conf, options)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/parser_frontends.py", line 209, in create_earley_parser
return f(lexer_conf, parser_conf, options, resolve_ambiguity=resolve_ambiguity, debug=debug, tree_class=tree_class, **extra)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/parser_frontends.py", line 186, in create_earley_parser__dynamic
earley_matcher = EarleyRegexpMatcher(lexer_conf)
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/site-packages/lark/parser_frontends.py", line 172, in __init__
raise GrammarError("Dynamic Earley doesn't allow zero-width regexps", t)
lark.exceptions.GrammarError: ("Dynamic Earley doesn't allow zero-width regexps", TerminalDef('EOL', '(?x:\n)'))
我忘了需要使用原始字符串。不得不加一个“r”。新代码如下所示:
class Parser:
grammar = r'''
start : STRING EOL
STRING : /\w+/
EOL : /\n/
'''