Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Yacc

Date:04/20/05
Author:James Copher
URL:n/a
Comments:1
Info:n/a
Score: (2.96 in 27 votes)
%{
/*
** 99 bottles of beer yacc-like
** by: James Copher jec@netcom.com
*/

#include<stdio.h>
static int bottles=99;
%}

%union{ int bottle; }
%token <bottle> BOTTLES NOMORE
%type <bottle> 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(); }

Download Source | Write Comment

Alternative Versions

Comments

>>  barrym said on 06/09/10 07:16:04

barrym Wow, that's a bunch of newlines!
Who's gonna go to the store....?

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: