if elsif else 语句解析

发布于 2025-01-03 03:42:21 字数 599 浏览 2 评论 0原文

我一直在使用 lex 和 yacc 开发命令式语言的编译器,今天我完成了语法,问题是我一直在网上读到每个语法都应该有一些移位/减少冲突,特别是如果它有 if/else 语句,通常称为悬空 if-else,而我的确实有 if/elsif/else 语句,但编译时不会引发任何冲突,问题是

“这是否意味着该语法有缺陷”只是因为它不会引发任何转变/减少冲突?我没有太多这样做的经验,但我找不到任何问题

如果您想要更多信息,此语法中 if/elsif/else 语句的产生式如下所示:

statement -> ... 
------------| initial_conditional_stmt

initial_conditional_stmt: conditional_stmt
-----------------------| conditional_stmt 'else' block


conditional_stmt -> 'if' '(' expression ')' block
------------------| conditional_stmt elsif  '(' expression ')' block

block 只是内部语句的列表括号{}

i've been working on a compiler for an imperative language using lex and yacc, and today i finished the grammar, the thing is that i've been reading on the web that every grammar is expected to have a few shift/reduce conflicts, specially if it has if/else statements, that's often known as dangling if-else, and mine does have if/elsif/else statements yet it doesn't throw any conflict when compiled, the question is

¿does that means this grammar has flaws just because it doesn't throw any shift/reduce conflicts? i dont have much experience doing this but i can't find any problem with it

In case you want some more information the productions for the if/elsif/else statements in this grammar are something like this:

statement -> ... 
------------| initial_conditional_stmt

initial_conditional_stmt: conditional_stmt
-----------------------| conditional_stmt 'else' block


conditional_stmt -> 'if' '(' expression ')' block
------------------| conditional_stmt elsif  '(' expression ')' block

block is just a list of statements inside brackets {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

甜味拾荒者 2025-01-10 03:42:21

没有shift/reduce冲突意味着你的语言设计做得非常好而且从来都不含糊。

干得好,拍拍自己的背,放松一下,去冰箱里拿杯啤酒。

No shift/reduce conflicts means you did a really good job of your language design and it's never ambiguous.

Well done, give yourself a pat on the back, relax, go grab a beer from the fridge.

苏佲洛 2025-01-10 03:42:21

您在这里没有得到悬空 else 的原因是因为您使用的是块而不是普通语句。

对于您的语法,您不能这样做

if (cond)
    if (cond) {
        [stuff]
    }
    else
    {
    }    

您必须这样做

if (cond) 
{
    if (cond) {
        [stuff]
    }
    else
    {
    }   
}

每个嵌套的 if 语句必须位于匹配的 { } 内。在您的情况下,这消除了悬而未决的 else,但代价是语法有点奇怪。与“正常”语法相比,“块”将是“语句”,它也可能是另一个 if 语句,从而导致经典的移位/归约冲突。

The reason you are not getting the dangling else here is because you're using a block instead of the normal statement.

For your grammar you cannot do

if (cond)
    if (cond) {
        [stuff]
    }
    else
    {
    }    

You would have to do this

if (cond) 
{
    if (cond) {
        [stuff]
    }
    else
    {
    }   
}

Every nested if statement must be inside matching { }. This in your case eliminates the dangling else at the expense of a bit stranger syntax. Compared to a "normal" grammar "block" would be "statement" which could also be another if statement, thus causing the classical shift/reduce conflict.

入怼 2025-01-10 03:42:21

您最好按如下方式使用这些代码:

%nonassoc XIF
%nonassoc ELSE

   stmt: IF expr stmt %prec XIF
       | IF expr stmt ELSE stmt

这是一种改变 if 和 else 之间的优先级冲突的方法。

希望有帮助。

You may better to use these code as following:

%nonassoc XIF
%nonassoc ELSE

   stmt: IF expr stmt %prec XIF
       | IF expr stmt ELSE stmt

That is a way to change precedence conflict between if and else.

Hope it helps.

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