算术表达式语法
我被分配了一项任务,为算术表达式(带有括号和一元运算符)创建解析器。 所以我只是想知道这个语法是否正确,是否是 LL(1) 形式,并且在构建这个
S -> TS'
S' -> +TS' | -TS' | epsilon
T -> UT'
T' -> *UT' | /UT' | epsilon
U -> VX
X -> ^U | epsilon
V -> (W) | -W | W | epsilon
W -> S | number
优先级(从高到低)
(), unary –
^
*, /
+, -
二元运算符的关联性的 解析表时遇到真正的问题
^ = right
+, -, *, / = left
I was assigned a task for creating a parser for Arithmetic Expressions (with parenthesis and unary operators). So I just wanna know if this grammar correct or not and is it in LL(1) form and having real problems constructing the parse table for this
S -> TS'
S' -> +TS' | -TS' | epsilon
T -> UT'
T' -> *UT' | /UT' | epsilon
U -> VX
X -> ^U | epsilon
V -> (W) | -W | W | epsilon
W -> S | number
Precedence (high to low)
(), unary –
^
*, /
+, -
Associativity for binary operators
^ = right
+, -, *, / = left
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要判断文法是否为 LL(1),您需要扩展产生式规则。 如果您可以生成任何产生式序列,从而导致左侧首先出现在右侧,则该语法不是 LL(1)。
例如,考虑这个规则:
这显然不能成为 LL(1) 语法的一部分,因为如果应用最左边的产生式,它就是左递归的。 但这又如何呢?
这也不是 LL(1) 语法,但它更微妙:首先你必须应用 X -->; Y,然后应用 Y --> X + X 看看你现在有 X --> X + X,这是左递归。
To tell if the grammar is LL(1) or not, you need to expand the production rules out. If you can generate any sequence of productions which results in the left-hand-side appearing as the first thing on the right-hand-side, the grammar is not LL(1).
For example, consider this rule:
This clearly can't be part of an LL(1) grammar, since it's left-recursive if you apply the leftmost production. But what about this?
This isn't an LL(1) grammar either, but it's more subtle: first you have to apply X --> Y, then apply Y --> X + X to see that you now have X --> X + X, which is left-recursive.
您似乎缺少一元加运算符的任何内容。 试试这个...
V -> (西)| -W | +W | 厄普西隆
You seem to be missing anything for unary plus operator. Try this instead...
V -> (W) | -W | +W | epsilon