我试图创建一个 let 语法我的想法是这样的
start : let
let : "let" ID ("=" let)? in let | atom
atom : ANYTHING | "(" let ")"
ID : /[a-z]+/
这个想法是解析这样的表达式let A = B in C
或let A in B
两者都混合let f = let x in x + 1 in f(1)
。我也想支持括号来消除歧义let A = (let b in b + 1) in A(1) + 1
我正在使用 lark,带有 LALR 解析器,但我在语法上苦苦挣扎,无法为此定义明确的语法
我试过了
from lark import Lark, Transformer as LarkTransformer
grammar = """
start : expr
expr : LET ID (EQUAL exprcont)? IN exprcont | exprcont
exprcont : ANYTHING | LPAR expr RPAR | expr
ANYTHING.0 : /.+/
LET : "let"
IN : "in"
ID : /[a-z_][a-z0-9_]*/
EQUAL : "="
LPAR.10 : "("
RPAR.10 : ")"
%import common.WS
%ignore WS
"""
let_parser = Lark(grammar, parser="lalr")
print(let_parser.parse("let a = 1 in let b = 2 in a + b").pretty())
但我有很多减少减少错误
Traceback (most recent call last):
File "/Users/gecko/code/lampycode/letparser.py", line 55, in <module>
let_parser = Lark(grammar, parser="lalr")
File "/Users/gecko/.pyenv/versions/lampy/lib/python3.9/site-packages/lark/lark.py", line 339, in __init__
self.parser = self._build_parser()
File "/Users/gecko/.pyenv/versions/lampy/lib/python3.9/site-packages/lark/lark.py", line 373, in _build_parser
return self.parser_class(self.lexer_conf, parser_conf, options=self.options)
File "/Users/gecko/.pyenv/versions/lampy/lib/python3.9/site-packages/lark/parser_frontends.py", line 145, in __init__
self.parser = LALR_Parser(parser_conf, debug=debug)
File "/Users/gecko/.pyenv/versions/lampy/lib/python3.9/site-packages/lark/parsers/lalr_parser.py", line 17, in __init__
analysis.compute_lalr()
File "/Users/gecko/.pyenv/versions/lampy/lib/python3.9/site-packages/lark/parsers/lalr_analysis.py", line 304, in compute_lalr
self.compute_lalr1_states()
File "/Users/gecko/.pyenv/versions/lampy/lib/python3.9/site-packages/lark/parsers/lalr_analysis.py", line 279, in compute_lalr1_states
raise GrammarError('\n\n'.join(msgs))
lark.exceptions.GrammarError: Reduce/Reduce collision in Terminal('$END') between the following rules:
- <exprcont : expr>
- <start : expr>
Reduce/Reduce collision in Terminal('IN') between the following rules:
- <expr : exprcont>
- <expr : LET ID IN exprcont>
Reduce/Reduce collision in Terminal('RPAR') between the following rules:
- <expr : exprcont>
- <expr : LET ID IN exprcont>
Reduce/Reduce collision in Terminal('$END') between the following rules:
- <expr : exprcont>
- <expr : LET ID IN exprcont>
Reduce/Reduce collision in Terminal('IN') between the following rules:
- <expr : exprcont>
- <expr : LET ID EQUAL exprcont IN exprcont>
Reduce/Reduce collision in Terminal('RPAR') between the following rules:
- <expr : exprcont>
- <expr : LET ID EQUAL exprcont IN exprcont>
Reduce/Reduce collision in Terminal('$END') between the following rules:
- <expr : exprcont>
我不知道如何定义这个语法,这个想法很简单let : "let" ID ("=" let)? "in" let | atom
有什么想法吗?