【问题标题】:passing up multiple attributes in yacc/bison在 yacc/bison 中传递多个属性
【发布时间】:2013-04-16 18:22:20
【问题描述】:

为了处理语法规则:

type            : ARRAY '[' integer_constant RANGE integer_constant ']' OF stype { $$ = $8 }
                | stype { $$ = $1 }
                ;

我需要传递类型属性(就像我现在所做的那样),但我还需要传递数组的范围以检查数组边界。 我已经尝试了各种使用结构的方法来实现类似的目标:

type            : ARRAY '[' integer_constant RANGE integer_constant ']' OF stype { $$.type = $8; $$.range[0] = $3; $$.range[1] = $5; }
                | stype { $$.type = $1 }
                ;

但一切都会导致错误和段错误,我很难找到正确的方法来处理这个问题

谁能指出我正确的方向? 提前致谢。

parse.y:http://pastebin.com/XUUqG35s

【问题讨论】:

    标签: parsing attributes bison yacc


    【解决方案1】:

    typestype 被声明为联合的 attr 成员,其类型为 node*。因此,在对这些非终结符中的任何一个进行操作的上下文中,$$ 将被替换为 x.attr 之类的东西(这只是一个说明,不要从字面上理解它。) .同样,在第一个动作中,$8 将被替换为 yystack[top-8].attr,因为 $8 也有标签 attr,是一个 stype

    所以$$.type(或者,实际上,$$.<anything>)一定是一个语法错误。 attr 是一个指针,所以 $$-><something> 可能是正确的,但如果没有看到 node 的定义,我就无法判断。

    此外,在stype 规则中,您设置了例如$$ = "INT",但$$ 的类型为node*,而不是char*(当然,除非nodechar,但这似乎是错误的。)当您稍后将该值视为指向 node 的指针时,似乎最终会导致段错误。

    我真的不清楚你认为$$.range 可能意味着什么。也许您需要显示更多标题。

    【讨论】:

      【解决方案2】:

      除了 rici 的回答和更直接地回答您标题中的问题之外,多个属性通常通过 struct 传递,因此如果您将与 type 非终端关联的值更改为例如 @ 987654323@ (%type <ctype> type) 和 stypetype (%type <type> stype) (我认为这是您的意图),然后将以下内容添加到您的 %union

      struct { int is_array, low, high; char * type; } ctype;
      

      那么您可以将type非终端的定义更改为

      type
      :    ARRAY '[' integer_constant RANGE integer_constant ']' OF stype
           { $$.is_array = 1; $$.low = $3; $$.high = $5; $$.type = $8; }
      |    stype
           { $$.is_array = 0; $$.low = $$.high = -1; $$.type = $1; }
      ;
      

      当然,必须进行更多更改才能正确处理新的ctype,但这通常是在解析器堆栈中传播多个属性的方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多