I have a rule like this:
A --> a B C d
, where a, d
are terminal symbols
and B, C
are non-terminal symbols.
B --> a1 | a2 | a3
C --> a4 | a5 | a6
I have write this rule in bison:
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 }
I would like wrtie this rule like this:
A --> a B
A --> errorCase
B --> a1 C | a2 C | a3 C
B --> errorCase
C --> a4 D | a5 D | a6D
D --> d
D -->errorCase
But I have no idea how to write it in bison. Can anyone help me to write it in bison? (I have no idea how I should get the value of the B and D)
The following grammar is accepted by yacc(BSD) without any problems. It should work with bison(Linux) as well.
By general convention, the tokens are usually capitalized and rules are in lower case.
%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);
}