2014-02-23 45 views
1

我探索野牛的語法和存在的一些建築像

 bodystmt: 
     { 
      $<num>$ = parser->line; 
     } 

的問題,我知道$$和$ 1,而num是一個類型..但是這是新的東西對我來說

回答

2

這是一個顯式類型標籤,覆蓋該值的聲明類型。所以,如果您有:

%union { 
    int num; 
    char *str; 
} 

%type<str> bodystmt 

%% 

bodystmt: { $<num>$ = ... } 

它設置在工會,而不是str領域num場的標頭中的%type delcaration的聲明。當然,該值的其他用途(例如$1在某些使用rhs上的bodystmt的規則)將訪問str字段,因此它們需要更改爲$<num>1,以避免未定義的行爲。

如果這是非常有用的是當你有嵌入有不同類型的動作:

%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 */ 

所以上面的代碼(除了做了說明一堆無用的東西)會的,如果它得到的輸入ABAB,打印5 3