From 42a5e8df3f8c62e104dd5e8d3c8f81a9aecd58af Mon Sep 17 00:00:00 2001 From: Rodo Date: Fri, 31 May 2024 23:16:23 -0600 Subject: [PATCH] A01250513 --- labs/07/chatbot.l | 26 ++++++++++++++++++++++++ labs/07/chatbot.y | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 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..62cecf45 --- /dev/null +++ b/labs/07/chatbot.l @@ -0,0 +1,26 @@ +%{ +#include "y.tab.h" +%} + +%% + +hello { return HELLO; } +hi { return HELLO; } +hey { return HELLO; } +goodbye { return GOODBYE; } +bye { return GOODBYE; } +time { return TIME; } +what[' ']is[' ']your[' ']name { return NAME; } +what[' ']is[' ']the[' ']weather { return WEATHER; } +how[' ']are[' ']you { return FEELINGS; } +what[' ']is[' ']the[' ']time { return TIME; } +what[' ']time[' ']is[' ']it { return TIME; } +\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..6ae6b854 --- /dev/null +++ b/labs/07/chatbot.y @@ -0,0 +1,52 @@ +%{ +#include +#include + +void yyerror(const char *s); +int yylex(void); +%} + +%token HELLO GOODBYE TIME WEATHER FEELINGS NAME + +%% + +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 { + 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); + } + | WEATHER { + printf("Va llover! Corre a tu casa."); + } + | FEELINGS { + printf("Ya me quiero graduar :c"); + } + | NAME { + printf("Soy RODO, soy tu..."); + } + ; + +%% + +int main() { + printf("Chatbot: Hi! You can greet me, ask for the time, or say goodbye.\n"); + while (yyparse() == 0) { + // Loop until end of input + } + return 0; +} + +void yyerror(const char *s) { + fprintf(stderr, "Chatbot: I didn't understand that.\n"); +} \ No newline at end of file