Created
January 13, 2020 01:17
-
-
Save kahless62003/53437db25c087322a87a92efa4af55ec to your computer and use it in GitHub Desktop.
Computerphile yoda parsing examples converted to Klingon.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% | |
"the" {yylval=1; return(ARTICLE);} | |
"a" {yylval=2; return(ARTICLE);} | |
"dog" {yylval=3; return(NOUN);} | |
"cat" {yylval=4; return(NOUN);} | |
"man" {yylval=5; return(NOUN);} | |
"woman" {yylval=6; return(NOUN);} | |
"robot" {yylval=7; return(NOUN);} | |
"bit" {yylval=8; return(VERB);} | |
"kicked" {yylval=9; return(VERB);} | |
"stroked" {yylval=10; return(VERB);} | |
"two furry dice" {yylval=11; return(PHRASE);} | |
"the robot" {yylval=0; return (PHRASE);} | |
"&" {return('&');} | |
"\n" {return('\n');} | |
%% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
# include <stdio.h> | |
# include <ctype.h> | |
char *tokens[] = {"the robot", "the", "a", "dog", "cat", "man", "woman", \ | |
"robot", "bit", "kicked", "stroked", "two furry dice"}; | |
char *tlhIngantokens[] = {"qoq", "", "", "qovIj", "vIghro'", "loD", "be'", "qoq", \ | |
"chop", "pup", "yach", "cha' veD mI' nagh"}; | |
char subjstring[100],verbstring[100],objstring[100]; | |
void initparts () | |
{ /* initialises the three strings holding pointers into | |
various accumulated components of a yoda sentence. */ | |
int i; | |
for (i=0 ; i < 100; i++) | |
subjstring[i] = verbstring[i] = objstring[i] = '0'; | |
} | |
int dummy; | |
void printarg(x,y) | |
int x,y; | |
{ | |
if (x>0) {printf("%c",(char)x);y=0;} | |
} | |
%} | |
%start list | |
%token ID VERB ARTICLE NOUN PHRASE | |
%% | |
list : /*empty*/ {initparts();} | |
|list sentence '\n' | |
{ initparts(); /* clear out buffers for next sentence */ | |
printf("\n\nNext furry sentence please!\n\n"); | |
} | |
|list error '\n' | |
{ yyerror("Oh dear -- syntax error. Baling out now!");exit(1);} | |
; | |
sentence : '&' | |
{printf("End of input-- Bye!\n");exit(1);} | |
| subj verb obj | |
{ printf("\njatlh tlhIngan: %s %s %s.\n", objstring, verbstring, subjstring); | |
} | |
; | |
subj : ARTICLE NOUN | |
{ | |
printf("Rule 2: %s %s. ", tokens[$1],tokens[$2]); | |
strcpy(subjstring, tlhIngantokens[$2]); | |
} | |
| PHRASE | |
{printf("Rule 3: %s. ", tokens[$1]); | |
strcpy(subjstring, tlhIngantokens[$1]); | |
} | |
; | |
verb : VERB | |
{printf("Rule 4: %s. ", tokens[$1]); | |
strcpy(verbstring, tlhIngantokens[$1]); | |
} | |
; | |
obj : ARTICLE NOUN | |
{printf("Rule 5: %s %s.\n", tokens[$1], tokens[$2]); | |
strcpy(objstring, tlhIngantokens[$2]); | |
} | |
| PHRASE | |
{ printf("Rule 6: %s.\n", tokens[$1]); | |
strcpy(objstring, tlhIngantokens[$1]); | |
} | |
; | |
%% | |
#include "lex.yy.c" | |
main() { | |
return (yyparse() ); | |
} | |
yyerror(s) | |
char *s; | |
{ | |
fprintf (stderr, "%s\n", s); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% | |
"qovIj" {yylval=0; return(NOUN);} | |
"vIghro'" {yylval=1; return(NOUN);} | |
"loD" {yylval=2; return(NOUN);} | |
"be'" {yylval=3; return(NOUN);} | |
"qoq" {yylval=4; return(NOUN);} | |
"chop" {yylval=5; return(VERB);} | |
"pup" {yylval=6; return(VERB);} | |
"yach" {yylval=7; return(VERB);} | |
"cha' veD mI' nagh" {yylval=8; return(PHRASE);} | |
"lu" {yylval=9; return(VPREFIX);} | |
"&" {return('&');} | |
"\n" {return('\n');} | |
. {;/*Ignore unidentified characters*/} | |
%% | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
# include <stdio.h> | |
# include <ctype.h> | |
char *tokens[] = {"qovIj", "vIghro'", "loD", "be'", "qoq", \ | |
"chop", "pup", "yach", "cha' veD mI' nagh", "lu"}; | |
char *engtokens[] = {"dog", "cat", "man", "woman", "robot", \ | |
"bit", "kicked", "stroked", "two furry dice", ""}; | |
char subjstring[100],verbstring[100],objstring[100]; | |
void initparts () | |
{ /* initialises the three strings holding pointers into | |
various accumulated components of an English sentence. */ | |
int i; | |
for (i=0 ; i < 100; i++) | |
subjstring[i] = verbstring[i] = objstring[i] = '0'; | |
} | |
int dummy; | |
void printarg(x,y) | |
int x,y; | |
{ | |
if (x>0) {printf("%c",(char)x);y=0;} | |
} | |
%} | |
%start list | |
%token VERB NOUN PHRASE VPREFIX | |
%% | |
list : /*empty*/ | |
|list sentence '\n' | |
{ | |
initparts(); | |
printf("\n\nveD mu'tlhegh veb HIja'!\n\n"); | |
} | |
|list error '\n' | |
{ yyerror("Do'Ha' qaSpu' pab Qagh. DaH jImej!");exit(1);} | |
; | |
sentence : '&' | |
{ | |
printf("rIn 'elbogh mu'mey-- Qapla'!\n"); | |
exit(1); | |
} | |
| obj verb subj | |
{ | |
printf("\n\njatlh pIqarD HoD: %s %s %s.\n", \ | |
subjstring, verbstring, objstring); | |
} | |
; | |
obj : NOUN | |
{ | |
printf("'ovmay DIp: %s. ", tokens[$1]); | |
strcpy(objstring, "the "); | |
strcat(objstring, engtokens[$1]); | |
} | |
| PHRASE | |
{ | |
printf("'ovmay DIpmey: %s. ", tokens[$1]); | |
strcpy(objstring, engtokens[$1]); | |
} | |
| error { fprintf(stderr, "'ovmay DIp vIngu'be'. "); } | |
; | |
verb : VERB | |
{ | |
printf("wot: %s. ", tokens[$1]); | |
strcpy(verbstring, engtokens[$1]); | |
} | |
| VPREFIX VERB | |
{ | |
printf("wot: %s. ", tokens[$2]); | |
strcpy(verbstring, engtokens[$2]); | |
} | |
| error { fprintf(stderr, "wot vIngu'be'. "); } | |
; | |
subj : NOUN | |
{ | |
printf("SeSor DIp: %s.\n", tokens[$1]); | |
strcpy(subjstring, "The "); | |
strcat(subjstring, engtokens[$1]); | |
} | |
| PHRASE | |
{ | |
printf("SeSor DIpmey: %s.\n", tokens[$1]); | |
strcpy(subjstring, "The "); | |
strcat(subjstring, engtokens[$1]); | |
} | |
| error { fprintf(stderr, "SeSor DIp vIngu'be'. "); } | |
; | |
%% | |
#include "lex.yy.c" | |
main() { | |
printf("I understand these nouns:\nqovIj, vIghro', loD, be' and qoq.\n"); | |
printf("And these verbs:\nchop, pup and yach.\n"); | |
printf("And this noun-phrase:\ncha' veD mI' nagh.\n\n\n"); | |
return (yyparse() ); | |
} | |
yyerror(s) | |
char *s; | |
{ | |
//fprintf (stderr, "%s\n", s); /*Suppress default "syntax error" message*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment