2013-10-11 76 views
0

我有這樣的規則:野牛如何編寫規則?

A --> a B C d,其中a, d是終端符號 和B, C是非終端符號。

B --> a1 | a2 | a3 
C --> a4 | a5 | a6 

我寫這條規則在野牛:

my_rule: 
      a B C d { handler->handle_B_C(handle_B($2), handle_C($3)); } 
    B : 
     a1 { $$ = ONE; } 
    | a2 { $$ = TWO; } 
    | a3 { $$ = THREE; } 
    ; 
    C: 
     a4 { $$ = FOUR; } 
     | a5 { $$ = FIVE; } 
     | a6 { $$ = SIX } 

我想wrtie這個規則是這樣的:

A --> a B 
    A --> errorCase 
    B --> a1 C | a2 C | a3 C 
    B --> errorCase 
    C --> a4 D | a5 D | a6D 
    D --> d 
    D -->errorCase 

但我不知道如何把它寫在野牛。任何人都可以幫我把它寫在野牛身上嗎? (我不知道該如何獲得B和D的值)

+0

小心標記; flex-lexer用於詞法分析器; flex是針對Adobe/Apache UI框架的。 – JeffryHouser

+0

http://www.gnu.org/software/bison/manual/html_node/Error-Recovery.html#Error-Recovery。不要修改你的語法。只需插入錯誤恢復規則。 – rici

+0

[richi](http://stackoverflow.com/users/1566221/rici)我不想在錯誤發生時繼續,我只想報告適當的錯誤消息。 –

回答

1

yacc(BSD)接受以下語法沒有任何問題。它應該與bison(Linux)一起工作。

按照一般約定,標記通常是大寫,規則是小寫。

%token A A1 A2 A3 A4 A5 A6 A7 D 

%% 

a 
    : A b { 
     $$ = node($1, $2); 
    } 
    ; 

b 
    : A1 c { 
     $$ = node($1, $2); 
    } 
    | A2 c { 
     $$ = node($1, $2); 
    } 
    | A3 c { 
     $$ = node($1, $2); 
    } 
    ; 

c 
    : A4 d { 
     $$ = node($1, $2); 
    } 
    | A5 d { 
     $$ = node($1, $2); 
    } 
    | A6 d { 
     $$ = node($1, $2); 
    } 
    ; 

d 
    : D { 
     $$ = node($1); 
    } 
    ; 
%% 

#include <stdio.h> 

void yyerror(const char *s) 
{ 
    fflush(stdout); 
    fprintf(stderr, "*** %s\n", s); 
}