[net.sources] Pig-Latin Meets LEX

zemon@felix.UUCP (08/29/83)

In response to Mark B's Pig-Latin translator, I feel I must
post the following version which does the same.  Note that
by using LEX, this is *much* shorter.

PLEASE, I am not trying to start a contest for the shortest
Pig-Latin translator program.  I just want to show how much
easier this is with LEX.

Compile it with:
	lex pig.c
	cc lex.yy.c -o pig -ll

Cheers,
	Art Zemon
	FileNet Corporation
	...!{ucbvax, decvax}!trw-unix!felix!zemon

----------------------------------------------------------------------
%{
/*
 * pig
 *
 * Translate C comments into Pig-Latin.
 */

static char rcsid[] =
"$Header:   RCS/pig.v  Revision 1.2  83/08/29  13:01:19  zemon  Exp$";

#include <stdio.h>
%}

%START	TRANS

%%
"/*"					{ECHO; BEGIN TRANS;}
<TRANS>"*/"				{ECHO; BEGIN 0;}
<TRANS>[A-Za-z]+			{word(yytext);}
%%

main()
{
	yylex();
}

word(s)
	char	*s;
{
	char	*root;
	char	first;
	
	switch( s[0] ) {
	case 'a':	case 'A':
	case 'e':	case 'E':
	case 'i':	case 'I':
	case 'o':	case 'O':
	case 'u':	case 'U':
		root = s;
		first = 'y';
		break;
		
	default:
		first = s[0];
		root = &s[1];
		break;
	}
	printf("%s-%cay", root, first);
	return;
}