diff --git a/labs/07/Makefile b/labs/07/Makefile new file mode 100644 index 00000000..5ca32838 --- /dev/null +++ b/labs/07/Makefile @@ -0,0 +1,11 @@ +all: + yacc -d chatbot.y + lex chatbot.l + gcc y.tab.c lex.yy.c -o chatbot + +clean: + rm -rf chatbot + rm -rf lex.yy.c + rm -rf y.tab.c + rm -rf y.tab.h + rm -rf y.tab.h.gch diff --git a/labs/07/chatbot.l b/labs/07/chatbot.l new file mode 100644 index 00000000..217e5ea7 --- /dev/null +++ b/labs/07/chatbot.l @@ -0,0 +1,29 @@ +%{ +#include "y.tab.h" +%} + +%% + +hello { return HELLO; } +hi { return HELLO; } +hey { return HELLO; } +goodbye { return GOODBYE; } +bye { return GOODBYE; } +time { return TIME; } +what[' ']is[' ']the[' ']time { return TIME; } +what[' ']time[' ']is[' ']it { return TIME; } +weather { return WEATHER; } +give[' ']me[' ']the[' ']weather { return WEATHER; } +what[' ']is[' ']the[' ']weather {return WEATHER; } +how[' ']are[' ']you { return HOWAREYOU; } +what[' ']are[' ']you[' ']doing {return WRYD; } +sing[' ']a[' ']song {return SINGREQUEST; } +\n { return 0; } /* End of input on newline */ + +. { return yytext[0]; } + +%% + +int yywrap() { + return 1; +} diff --git a/labs/07/chatbot.y b/labs/07/chatbot.y new file mode 100644 index 00000000..bf12f2f0 --- /dev/null +++ b/labs/07/chatbot.y @@ -0,0 +1,80 @@ +%{ +#include +#include + +void yyerror(const char *s); +int yylex(void); +%} + +%token HELLO GOODBYE TIME WEATHER HOWAREYOU WRYD SINGREQUEST + +%% + +chatbot : greeting + | farewell + | query + ; + +greeting : HELLO { printf("Chatbot: Hello! How can I help you today?\n"); } + ; + +farewell : GOODBYE { printf("Chatbot: Goodbye! Have a great day!\n"); } + ; + +query : TIME { + setenv("TZ", "CST6", 1); + tzset(); + time_t now = time(NULL); + struct tm *local = localtime(&now); + printf("Chatbot: The current time is %02d:%02d.\n", local->tm_hour, local->tm_min); + } +; + +query : WEATHER { + printf("Chatbot: It seems like the weather in Zapopan, Jalisco is quite charming at the moment!\n"); + } +; + +query : HOWAREYOU { + printf("Chatbot: Feeling the heat, but still keeping it cool!\n"); + } +; + +query : WRYD { + printf("Chatbot: Answering your questions!\n"); + } +; + +query : SINGREQUEST { + printf("Chatbot:\n" + "Twinkle, twinkle little star\n" + "How I wonder what you are\n" + "Up above the world so high\n" + "Like a diamond in the sky\n\n" + + "Twinkle, twinkle little star\n" + "How I wonder what you are\n\n" + + "Twinkle, twinkle little star\n" + "How I wonder what you are\n" + "Up above the world so high\n" + "Like a diamond in the sky\n\n" + + "Twinkle, twinkle little star\n" + "How I wonder what you are\n"); + } +; + +%% + +int main() { + printf("Chatbot: Hi! You can greet me, ask for the time, ask me for the weather, ask me what I'm doing, ask me how I'm doing, ask me to sing a song or $ + while (yyparse() == 0) { + // Loop until end of input + } + return 0; +} + +void yyerror(const char *s) { + fprintf(stderr, "Chatbot: I didn't understand that.\n"); +}