【问题标题】:Bison loop for conflict野牛循环冲突
【发布时间】:2018-05-19 17:57:19
【问题描述】:

为了解决悬空的 else 问题,我使用了以下解决方案:

stmt            : stmt_matched
                | stmt_unmatched
                ;
stmt_unmatched  : IF '(' exp ')' stmt
                | IF '(' exp ')' stmt_matched ELSE stmt_unmatched
                ;
stmt_matched    : IF '(' exp ')' stmt_matched ELSE stmt_matched
                | stmt_for
                | ...
                ;

为了定义for循环的语法规则,由于同样的问题,我产生了一个冲突移位/减少:

stmt_for        : FOR '(' exp ';' exp ';' exp ')' stmt
            ;

我该如何解决这个问题?

【问题讨论】:

    标签: c if-statement bison flex-lexer


    【解决方案1】:

    并非所有for 语句都匹配。例如,考虑

     if (c) for (;;) if (d) ; else ;
    

    所以需要将for语句分为for_matchedfor_unmatched。 (与其他复合语句类似,例如while。)

    【讨论】:

    • 所以我必须在stmt match中添加for_matched,在stmt unmatched中添加for_unmatched?因此 stmt_for_matched : FOR '(' exp ';' exp ';' exp ')' stmt_matched ; 和 stmt_for_unmatched : FOR '(' exp ';' exp ';' exp ')' stmt_unmatched ;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2021-11-20
    相关资源
    最近更新 更多