[英]Ambiguous grammar when parsing boolean expressions with Peg.js
我正在编写一个从布尔表达式生成抽象语法树的解析器。
我有以下支持^
和∨
Peg.js语法,分别为&
和|
分别:
start
= operation
// optional whitespace
_ = [ \t\r\n]*
operation "operation"
= "("? _ left:(operand / operation) _ operator:operator _ right:(operand / operation) _ ")"?
{
return {
operation: operator,
between: [ left, right ]
};
}
operator "operator"
= operator:["&"|"|"]
{
return operator;
}
operand "operand"
= operand:[a-z]
{
return { operand: operand };
}
它可以成功解析a & b
和a & (b | c)
之类的表达式,但是如果该表达式以一个运算符开头,则会失败:
(a | b) & c
Line 1, column 8: Expected end of input but " " found.
如果使用括号将表达式正确解析,则可以:
((a | b) & c)
我的猜测是Peg.js仅将(a | b)
作为操作,而不是父操作的操作数,因此在看到& c
时失败。
我想念什么?
您的操作规则说,方括号是可选的,但没有一个方括号会强制使用另一个方括号。 例如, (a & b
已成功解析。
您需要将其分解成较小的部分。 and
和or
的单独规则允许运算符优先级发挥作用。
尝试这个:
start
= sentence
sentence
= orSentence
orSentence
= lhs:andSentence __ '|' __ rhs:orSentence { return { operation: '|', between: [lhs, rhs] }; }
/ andSentence
andSentence
= lhs:primarySentence __ '&' __ rhs:andSentence { return { operation: '&', between: [lhs, rhs] }; }
/ primarySentence
primarySentence
= '(' _ sentence:sentence _ ')' { return sentence; }
/ operand
operand
= operand:[a-z] { return { operand: operand }; }
_ "optionalWhiteSpace"
= whiteSpace *
__ "mandatoryWhiteSpace"
= whiteSpace +
whiteSpace
= [ \t\n\r]+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.