python 如何为带有等式的FOL向lark语法添加运算符优先级?

cvxl0en2  于 2022-11-21  发布在  Python
关注(0)|答案(1)|浏览(135)

如何修改此语法以匹配更远的括号?

wff: compound_wff
?compound_wff: quantifier | biconditional | atomic_wff
?quantifier: (quantifier_symbol LEFT_CURLY_BRACE variable RIGHT_CURLY_BRACE SPACE?)* bracketted_wff
?biconditional: conditional SPACE (biconditional_symbol SPACE conditional)*
?conditional: disjunction SPACE (conditional_symbol SPACE disjunction)*
?disjunction: conjunction SPACE (disjunction_symbol SPACE conjunction)*
?conjunction: negation SPACE (conjunction_symbol SPACE negation)*
?negation: negation_symbol* SPACE bracketted_wff
?bracketted_wff: LEFT_PARENTHESIS SPACE? compound_wff SPACE? RIGHT_PARENTHESIS SPACE?
?atomic_wff: predicate_name [LEFT_PARENTHESIS term (COMMA term)* RIGHT_PARENTHESIS]                          -> predicate
          | term equal_to term

term: function_name LEFT_PARENTHESIS term (COMMA SPACE? term)* RIGHT_PARENTHESIS                             -> function
    | name
    | variable

SPACE: /\s+/
COMMA: ","
equal_to: "="
LEFT_PARENTHESIS: "("
RIGHT_PARENTHESIS: ")"
LEFT_CURLY_BRACE: "{"
RIGHT_CURLY_BRACE: "}"
quantifier_symbol: universal_quantifier_symbol | existential_quantifier_symbol
universal_quantifier_symbol: "\\forall"
existential_quantifier_symbol: "\\exists"
name: /[a-t]/ | /[a-t]_[1-9]\d*/
variable: /[u-z]/ | /[u-z]_[1-9]\d*/
predicate_name: /[A-HJ-Z]/ | /[A-HJ-Z]_[1-9]\d*/
function_name: /[a-z]/ | /[a-z]_[1-9]\d*/
negation_symbol: "\\neg"
conjunction_symbol: "\\wedge"
disjunction_symbol: "\\vee"
exclusive_disjunction_symbol: "\\veebar"
conditional_symbol: "\\rightarrow" | "\\Rightarrow" | "\\Longrightarrow" | "\\implies"
biconditional_symbol: "\\leftrightarrow" | "\\iff"

我试着用我的语法来分析这个:

在乳胶中是:

\exists{x} \forall{y} (P(f(x, y)) \vee \forall{z}(V(z) \iff \neg R(a) \wedge B(a)))

我遵循calculator example并修改了我的original grammar以添加运算符优先级,这导致了这个结果。但是它不再接受输入字符串。
我得到这个错误:

lark.exceptions.UnexpectedCharacters: No terminal matches '\' in the current parser context, at line 1 col 35

\exists{x} \forall{y} (P(f(x, y)) \vee \forall{z}(V(z) \iff \neg R(a) \wed
                                  ^
Expected one of: 
    * RIGHT_PARENTHESIS

理想情况下,我希望在任何可能的地方强制使用括号,除了在没有括号的求反atomic_wff前面的情况。这是为了确保即使在显式歧义设置下也只生成一个解析树。我如何解决这个问题?

t30tvxxf

t30tvxxf1#

这是一个处理空格的问题。这些结果中的每一个都要求在SPACE后面附加一个SPACE,即使重复为空。这些加起来,所以在那个上下文中,语法不会接受一个term后面只跟着一个SPACE

?biconditional: conditional SPACE (biconditional_symbol SPACE conditional)*
?conditional: disjunction SPACE (conditional_symbol SPACE disjunction)*
?disjunction: conjunction SPACE (disjunction_symbol SPACE conjunction)*
?conjunction: negation SPACE (conjunction_symbol SPACE negation)*

相关问题