%{ /* ** 99 bottles of beer yacc-like ** by: James Copher jec@netcom.com */ #include static int bottles=99; %} %union{ int bottle; } %token BOTTLES NOMORE %type beer nomore %start round %% round : beer nomore { YYACCEPT; }; beer : BOTTLES { printf("%d bottles of beer on the wall\n%d bottles of beer\n" "Take one down,\npass it around\n",$1,$1); } | beer BOTTLES { printf("%d bottle%s of beer on the wall\n\n" "%d bottle%s of beer on the wall\n%d bottle%s of beer\n" "Take one down\npass it around\n", $2,$2!=1?"s":"",$2,$2!=1?"s":"",$2,$2!=1?"s":""); }; nomore : NOMORE { printf("No more bottles of beer on the wall\n"); }; %% yyerror(){} yylex(){ if(bottles){ yylval.bottle=bottles--; return BOTTLES; } return NOMORE; } main(){ yyparse(); }