【问题标题】:shift/reduce because of a recursion rule由于递归规则而移位/减少
【发布时间】:2014-04-17 13:51:50
【问题描述】:

我正在尝试使用 flex & bison 编写给定语言的迷你编译器。 它工作得很好,直到我发现我忘记了一条规则 其中有递归,规则是:

 liste_data : liste_data declar | declar;

当我添加它时,我遇到了我不理解的 shift/reduce 冲突。我的语法没有歧义

这是我的语法的简化版本:

s:idf bloc_data mc_end   { printf ("programme juste (lexique+syntaxe)\n"); YYACCEPT;}
;
bloc_data:mc_data liste_data mc_end
|mc_data mc_end
;
liste_data : declar
|liste_data declar
;
declar: liste_const
|liste_type
;
liste_type:liste_type def_type
|def_type
;
def_type:mc_char ':' liste_var ';'
;
liste_var:idf
|liste_var '|' idf
;
liste_const:liste_const constante
|constante
;
constante:mc_const ':' idf affect entier ';'
;

它基本上说我可以在 DATA bloc 中定义字符和常量

这是我的输出

État 11 conflits: 1 décalage/réduction
État 13 conflits: 1 décalage/réduction


Grammaire

0 $accept: s $end

1 s: idf bloc_data mc_end

2 bloc_data: mc_data liste_data mc_end
3          | mc_data mc_end

4 liste_data: declar
5           | liste_data declar

6 declar: liste_const
7       | liste_type

8 liste_type: liste_type def_type
9           | def_type

10 def_type: mc_char ':' liste_var ';'

11 liste_var: idf
12          | liste_var '|' idf

13 liste_const: liste_const constante
14            | constante

15 constante: mc_const ':' idf affect entier ';'
.
.
.

état 11

7 declar: liste_type .
8 liste_type: liste_type . def_type

mc_char  décalage et aller à l'état 7

mc_char   [réduction par utilisation de la règle 7 (declar)]
$défaut  réduction par utilisation de la règle 7 (declar)

def_type  aller à l'état 20


état 13

6 declar: liste_const .
13 liste_const: liste_const . constante

mc_const  décalage et aller à l'état 8

mc_const  [réduction par utilisation de la règle 6 (declar)]
$défaut  réduction par utilisation de la règle 6 (declar)

constante  aller à l'état 21

它说我的 shift/reduce 冲突处于状态 11 和 13,但我无法弄清楚究竟是为什么。 它应该能够识别这样的东西:

DATA
CONST: Er=5;
CONST: H=56;
CHAR: Hg|rt;
END

【问题讨论】:

    标签: recursion bison flex-lexer shift-reduce-conflict


    【解决方案1】:

    liste_const 只是constante 的列表,中间没有标点符号:

    constante constante constante constante
    

    declar 可能是liste_const

    如果你有一个liste_data(实际上是一个liste_declar,不是吗?),会发生什么。这可能是constante 列表的列表,但无法知道constante 的第一个列表在哪里结束,下一个从哪里开始。所以上面可以解析为

    <list_const <constante constante>> <list_const <constante>> <list_const <constante>>
    

    <list_const <constante constante constante constante>>
    

    或大量其他可能性。

    liste_type 的情况类似。

    换句话说,您不希望liste_data 成为常​​量和类型列表的列表;您希望它是(常量或类型)的列表。

    就个人而言,我只是改变:

    declar: def_type | constante;
    

    并摆脱 liste_typelist_const

    【讨论】:

    • 哦,有道理,我什至没有意识到,因为我在编写其他规则后添加了 liste_data 规则。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 2011-06-09
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多