Added 10 more gists.
This commit is contained in:
1
piglatin.l/README.md
Normal file
1
piglatin.l/README.md
Normal file
@@ -0,0 +1 @@
|
||||
A Lex program that converts an input text to piglatin.
|
46
piglatin.l/piglatin.l
Normal file
46
piglatin.l/piglatin.l
Normal file
@@ -0,0 +1,46 @@
|
||||
%{
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
|
||||
bool isconsonant(int);
|
||||
|
||||
%}
|
||||
|
||||
%option outfile="piglatin.c"
|
||||
%option always-interactive
|
||||
%option nounput noinput
|
||||
|
||||
word [a-zA-Z]+
|
||||
|
||||
%%
|
||||
|
||||
{word} { if (isconsonant(yytext[0])) {
|
||||
int numcons = 0;
|
||||
while (isconsonant(yytext[numcons++]) && numcons < strlen(yytext))
|
||||
yytext[numcons - 1] = tolower(yytext[numcons - 1]);
|
||||
printf("%s", &yytext[--numcons]);
|
||||
yytext[numcons] = '\0';
|
||||
printf("%say", yytext);
|
||||
yytext[numcons] = ' ';
|
||||
} else
|
||||
printf("%sway", yytext);
|
||||
}
|
||||
"\n" { putc('\n', stdout); }
|
||||
. { putc(yytext[0], stdout); }
|
||||
|
||||
%%
|
||||
|
||||
bool isconsonant(int c) {
|
||||
if (!isalpha(c))
|
||||
return false;
|
||||
|
||||
c = tolower(c);
|
||||
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user