使这个表达式语法对于 LL(1) 来说是明确的
我们怎样才能使这个 Expression 语法对于 LL(1) 解析来说是明确的?
语法与大多数 C 类语言中使用的表达式非常相似。
注意:<>中的字符串是非终结符,而大写的则是终结符。
<expression> --> <arithmeticExpr> | <booleanExpr>
<arithmeticExpr> --> <arithmeticExpr> <op1> <term> | <term>
<term> --> <term> <op2> <factor>
<term> --> <factor>
<factor> --> BO <arithmeticExpr> BC
<factor> --> <var>
<op1> --> PLUS | MINUS
<op2> --> MUL | DIV
<booleanExpr> --> <booleanExpr> <logicalOp> <booleanExpr>
<booleanExpr> --> <arithmeticExpr> <relationalOp> <arithmeticExpr>
<booleanExpr> --> BO <booleanExpr> BC
<logicalOp> --> AND | OR
<relationalOp> --> LT | LE | GT | GE | EQ | NE
<var> --> ID <whichId> | NUM | RNUM
<whichId> --> SQBO ID SQBC | ε
PS:我在 Stackoverflow 上找不到任何处理布尔表达式的问题。
How can we make this Expression grammar unambiguous for LL(1) parsing?
The grammar is very similar to expressions used in most C like languages.
Note: Strings in <> are non-terminals, while those in Upper Case are terminals.
<expression> --> <arithmeticExpr> | <booleanExpr>
<arithmeticExpr> --> <arithmeticExpr> <op1> <term> | <term>
<term> --> <term> <op2> <factor>
<term> --> <factor>
<factor> --> BO <arithmeticExpr> BC
<factor> --> <var>
<op1> --> PLUS | MINUS
<op2> --> MUL | DIV
<booleanExpr> --> <booleanExpr> <logicalOp> <booleanExpr>
<booleanExpr> --> <arithmeticExpr> <relationalOp> <arithmeticExpr>
<booleanExpr> --> BO <booleanExpr> BC
<logicalOp> --> AND | OR
<relationalOp> --> LT | LE | GT | GE | EQ | NE
<var> --> ID <whichId> | NUM | RNUM
<whichId> --> SQBO ID SQBC | ε
PS: I coudn't find any question on Stackoverflow that handled Boolean Expressions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要消除规则的歧义,
它应该如何处理像
a AND b OR c
和a OR b AND c
这样的输入?有多种可能的解释;你需要决定你想要哪个。然后,您将得到一个明确的语法,但不是 LL(1)。要使其为 LL(1),您需要 左因子 它。
First you need to disambiguate the rule
How should it handle inputs like
a AND b OR c
anda OR b AND c
? There are multiple possible interpretations; you need to decide which you want.Then, you'll have a grammar that is unambiguous, but not LL(1). To make it LL(1) you need to left-factor it.
@Chris,您的问题可能已纠正如下。然而完整语法的歧义并没有消失。
此外,标准形式的左因式分解在这里也是不可能的。
仅当我们尝试找到第一组
时,我们才会得到左公因数(例如
中的BO
)代码> 和 <代码><表达式>@Chris, your issue is probably rectified as following. Yet ambiguity of the complete grammar does not vanish.
Also, left factoring in its standard form is not possible here.
We get left common factors (like
BO
in<booleanExpr>
) only when we try to find the FIRST sets of<booleanExpr>
and<expression>