From 899cfd7416b2f38e85d847a7e8a73c827366fe2e Mon Sep 17 00:00:00 2001 From: Fausto Palma <7fausto.palm@gmail.com> Date: Fri, 31 May 2024 21:14:06 -0600 Subject: [PATCH] Add files via upload --- labs/07/chatbot.l | 30 ++++++++++++++++++ labs/07/chatbot.y | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 labs/07/chatbot.l create mode 100644 labs/07/chatbot.y diff --git a/labs/07/chatbot.l b/labs/07/chatbot.l new file mode 100644 index 00000000..6d7ea167 --- /dev/null +++ b/labs/07/chatbot.l @@ -0,0 +1,30 @@ +%{ +#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; } +name { return NAME; } +what[' ']is[' ']your[' ']name { return NAME; } +weather { return WEATHER; } +what[' ']is[' ']the[' ']weather { return WEATHER; } +how[' ']are[' ']you { return MOOD; } +how[' ']are[' ']you[' ']doing { return MOOD; } +help { return HELP; } +\n { return 0; } /* End of input on newline */ + +. { return yytext[0]; } + +%% + +int yywrap() { + return 1; +} \ No newline at end of file diff --git a/labs/07/chatbot.y b/labs/07/chatbot.y new file mode 100644 index 00000000..41c3182c --- /dev/null +++ b/labs/07/chatbot.y @@ -0,0 +1,80 @@ +%{ +#include +#include +#include + +void yyerror(const char *s); +int yylex(void); +%} + +%token HELLO GOODBYE TIME NAME WEATHER MOOD HELP + +%% + +chatbot : greeting + | farewell + | time_query + | name_query + | weather_query + | mood_query + | help_query + ; + +greeting : HELLO { printf("MarIA: Hello! How can I help you today?\n"); } + ; + +farewell : GOODBYE { printf("MarIA: Goodbye! Have a great day!\n"); } + ; + +time_query : TIME { + time_t now = time(NULL); + struct tm *local = localtime(&now); + printf("MarIA: The current time is %02d:%02d.\n", local->tm_hour, local->tm_min); + } + ; + +name_query : NAME { + printf("MarIA: My name is MarIA, nice to meet you!\n"); + } + ; + +weather_query : WEATHER { + int status = system("curl -s wttr.in > /dev/null"); + if (status == 0) { + printf("MarIA: Here's the weather in Guadalajara, Mexico:\n"); + system("curl -s wttr.in/Guadalajara,Mexico | sed -n '3,7p'"); + } else { + printf("MarIA: There seems to be an issue. Maybe try again later?\n"); + } + } + ; + +mood_query : MOOD { + printf("MarIA: I'm fine, thanks for asking.\n"); + } + ; + +help_query : HELP { + printf("MarIA: You can\n"); + printf(" • Greet me\n"); + printf(" • Ask my name\n"); + printf(" • Ask how I am doing\n"); + printf(" • Ask for the time\n"); + printf(" • Ask for the weather\n"); + printf(" • Say goodbye\n"); + } + ; + +%% + +int main() { + printf("MarIA: Hi, I'm MarIA! Type 'help' to see what you can ask me.\n"); + while (yyparse() == 0) { + // Loop until end of input + } + return 0; +} + +void yyerror(const char *s) { + fprintf(stderr, "MarIA: I didn't understand that.\n"); +} \ No newline at end of file