// Include some declarations that we need %{ #include #include using namespace std; %} // Define the return types %union { list* text_list; string* text; } // Define the grammar production return types %type node_list %type 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; $$->push_back(*$1); delete $1; }; node : A { $$ = new string("a"); } | B { $$ = new string("b"); } ;