算术表达式语法

发布于 2024-07-14 23:58:52 字数 479 浏览 7 评论 0原文

我被分配了一项任务,为算术表达式(带有括号和一元运算符)创建解析器。 所以我只是想知道这个语法是否正确,是否是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳男子 2024-07-21 23:58:53

是LL(1)形式吗?

要判断文法是否为 LL(1),您需要扩展产生式规则。 如果您可以生成任何产生式序列,从而导致左侧首先出现在右侧,则该语法不是 LL(1)。

例如,考虑这个规则:

X --> X | x | epsilon

这显然不能成为 LL(1) 语法的一部分,因为如果应用最左边的产生式,它就是左递归的。 但这又如何呢?

X --> Y | x
Y --> X + X

这也不是 LL(1) 语法,但它更微妙:首先你必须应用 X -->; Y,然后应用 Y --> X + X 看看你现在有 X --> X + X,这是左递归。

Is it in LL(1) form?

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:

X --> X | x | epsilon

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?

X --> Y | x
Y --> X + X

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.

眼藏柔 2024-07-21 23:58:53

您似乎缺少一元加运算符的任何内容。 试试这个...

V -> (西)| -W | +W | 厄普西隆

You seem to be missing anything for unary plus operator. Try this instead...

V -> (W) | -W | +W | epsilon

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文