我有这样的规矩:
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 }我希望这条规则是这样的:
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的值)
发布于 2013-10-16 03:16:26
以下语法被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);
}https://stackoverflow.com/questions/19311187
复制相似问题