// Include some declarations that we need
%{
#include <list>
#include <string>
using namespace std;
%}
// Define the return types
%union {
list<string>* text_list;
string* text;
}
// Define the grammar production return types
%type <text_list> node_list
%type <text> node
%token A
%token B
%%
node_list :
node_list node
{
if ($1->size() > 0 && $1->back() == "b" && *$2 == "a")
{
yyerror("\"a\" can't follow \"b\"!");
$$ = $1;
}
else
{
$$ = $1;
$$->push_back(*$2);
}
delete $2;
} |
node
{
$$ = new list<string>;
$$->push_back(*$1);
delete $1;
};
node :
A { $$ = new string("a"); } |
B { $$ = new string("b"); } ;