This commit is contained in:
anon 2024-02-22 20:46:05 +01:00
commit 4d47c07fd2
6 changed files with 153 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.gdb_history
farmsay
object/

14
Makefile Normal file
View File

@ -0,0 +1,14 @@
object/%.yy.cpp: %.l
flex --header-file=object/$(basename $(notdir $<)).yy.h -o $@ $<
object/%.tab.cpp: %.y
bison --header=object/$(basename $(notdir $<)).tab.h -o $@ $<
farmsay: main.cpp object/farmsay.tab.cpp object/farmsay.yy.cpp
g++ -Iobject/ $+ -o farmsay -ggdb
clean:
-rm object/*
test:
-./farmsay < <(printf "I'me! Megmutatom ne'ktek az utolso' embert. Mi az, hogy szerelem? Mi az, hogy teremte's? Mi az, hogy va'gy? Mi az, hogy csillag? - i'gy ke'rdezo''sko:dik az utolso' ember e's vaksin pislog.")

19
farmsay.l Normal file
View File

@ -0,0 +1,19 @@
%{
#include "farmsay.tab.h"
extern void yyerror(...);
%}
%option noyywrap
%option nodefault
%%
<INITIAL>{
\.[[:space:]]* { return DOT; }
\![[:space:]]* { return BANG; }
\?[[:space:]]* { return QUESTIONMARK; }
. { yylval.strval = yytext; return STRING; }
\n { ; }
}
%%

43
farmsay.y Normal file
View File

@ -0,0 +1,43 @@
%code{
extern void yyerror(...);
}
%{
#include "farmsay.yy.h"
#include <string.h>
#include <string>
extern void bison_say(const char * const s);
extern void snake_say(const char * const s);
extern void sheep_say(const char * const s);
using namespace std;
%}
%union {
char * strval;
}
%token <strval> DOT BANG QUESTIONMARK
%token <strval> STRING
%type <strval> sentence snake sheep bison
%%
speech: %empty
| speech snake
| speech bison
| speech sheep
;
snake: sentence DOT { snake_say($$); };
bison: sentence BANG { bison_say($$); };
sheep: sentence QUESTIONMARK { sheep_say($$); };
sentence: STRING
| sentence STRING
;
%%
void yyerror(...) { puts("\033[31m\033[7m!!!\033[0m"); }

74
main.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <stdio.h>
#include <string.h>
#include "farmsay.tab.h"
inline
void line(const char c, const int n) {
putc(' ', stdout);
for (int i = 0; i < n-2; i++) {
putc(c, stdout);
}
putc('\n', stdout);
}
void frame(const char f, const int b, const char * s) {
const int WIDTH = 24;
line(f, WIDTH);
const int l = strlen(s);
for (int i = 0; i < (l / (WIDTH-4)) + 1; i++) {
printf("%c %-20.20s %c\n", f, s + (i*(WIDTH-4)), f);
}
line(f, WIDTH);
for (int i = 0; i < b; i++) {
putc(' ', stdout);
}
fputs("|/\n", stdout);
}
void bison_say(const char * const s) {
const char * const BISON =
" .xxx###;=>\n"
":xxxx####> \n"
" L p \n"
;
fputs("\033[31m", stdout);
frame('*', 12, s);
puts(BISON);
fputs("\033[0m", stdout);
}
void snake_say(const char * const s) {
const char * const SNAKE =
" .~-\n"
" '. \n"
" :..;\n"
;
fputs("\033[32m", stdout);
frame(';', 7, s);
puts(SNAKE);
fputs("\033[0m", stdout);
}
void sheep_say(const char * const s) {
const char * const SHEEP =
" ____ o=\n"
",######[ \n"
" l l \n"
;
fputs("\033[33m", stdout);
frame('o', 10, s);
puts(SHEEP);
fputs("\033[0m", stdout);
}
signed main(int argc, char * * argv) {
yyparse();
return 0;
}

0
object/.gitkeep Normal file
View File