/* Lex part of a program to find out if the cat's in the well? No, it just recognizes the words ding, dong, and dell. Will Briggs Fall 2025 */ %{ #include "y.tab.h" %} %% "ding" return ( DING ); "dong" return ( DONG ); "dell" return ( DELL );
/* Yacc part of a program to find out if the cat's in the well? No, it just recognizes the words ding, dong, and dell, and prints a rhyme. Will Briggs Fall 2025 */ %{ #include <stdio.h> void yyerror(char *str); int yywrap (); int yylex (); int yyparse(); %} %token DING DONG DELL %% rhyme : sound place { printf ("Ding, dong, dell, pussy’s in the well.\n"); printf ("Who put her in? Little Johnny Thin.\n"); printf ("Who pulled her out? Little Tommy Stout.\n"); printf ("What a naughty boy was that, to try to drown poor pussy cat,\n"); printf ("Who never did him any harm, but killed all the mice in the farmer's barn.\n"); } ; sound : DING DONG ; place : DELL ; %% /* How yacc will print errors, if need be. Just leave this one as is. */ void yyerror(char *str){ fprintf(stderr,"error: %s\n",str); } /* Needed by yacc; just leave this one */ int yywrap() { return 1; } /* Needed by yacc; just leave this one */ int main() { yyparse(); }
#This Makefile will work for a lot of different yacc projects # Note the -i option on flex, which makes the program # case-insensitive # Will Briggs # Fall 2025 a.out: lex.yy.c y.tab.c gcc lex.yy.c y.tab.c y.tab.c: yacc.y bison -d yacc.y -o y.tab.c y.tab.h: yacc.y bison -d yacc.y -o y.tab.c lex.yy.c: lex.l flex -i lex.l #the -i makes it case-insensitive clean: rm -f *~ *.o lex.yy.c y.tab.c y.tab.h