Bison 野牛';新加坡元<;num>;$您是否>;?

Bison 野牛';新加坡元<;num>;$您是否>;?,bison,Bison,我研究了Bison的语法,有一个关于一些结构的问题,比如 bodystmt: { $<num>$ = parser->line; } bodystmt: { $$=解析器->行; } 我知道$$和$1,num是一种类型。。但这对我来说是一个新事物,它是一个显式的类型标记,覆盖该值的声明类型。因此,如果你有: %union { int num; char *str; } %type<st

我研究了Bison的语法,有一个关于一些结构的问题,比如

       bodystmt:
       {
           $<num>$ = parser->line;
       }
bodystmt:
{
$$=解析器->行;
}

我知道$$和$1,num是一种类型。。但这对我来说是一个新事物,它是一个显式的类型标记,覆盖该值的声明类型。因此,如果你有:

%union {
    int  num;
    char *str;
}

%type<str> bodystmt

%%

bodystmt: { $<num>$ = ... }
因此,上面的代码(除了做一堆无用的东西来进行说明)将,如果它得到输入
ABAB
,打印
53

%type<num> foo baz
%%
rule: { $<num>$ = 5; } foo { $<str>$ = "bar" } baz {
                     x = $<num>1;  /* gets the 5 stored in the first action */
                     y = $2;       /* the result of rule 'foo' */
                     z = $<str>3;  /* the string '"bar"' */
                     printf("%d %d\n", $2, $4); }

foo: A B { $$ = $<num>0; /* gets the value stored by the action just before the 'foo' in the rule that triggers this rule }

baz: { $<num>$ = strlen($<str>0); } foo  /* the <num> here is redundant and has no effect */