diff --git a/labs/07/chatbot.l b/labs/07/chatbot.l new file mode 100644 index 00000000..b8d68201 --- /dev/null +++ b/labs/07/chatbot.l @@ -0,0 +1,42 @@ +%{ +#include "y.tab.h" +%} + +%% +hola { return HELLO; } +hello { return HELLO; } +hi { return HELLO; } +hey { return HELLO; } + +adios { return GOODBYE; } +goodbye { return GOODBYE; } +bye { return GOODBYE; } + +time { return TIME; } +what[' ']is[' ']the[' ']time { return TIME; } +what[' ']time[' ']is[' ']it { return TIME; } + +what[' ']is[' ']your[' ']name { return NAME; } +give[' ']me[' ']your[' ']name { return NAME; } +do[' ']you[' ']have[' ']a[' ']name { return NAME; } + +what[' ']is[' ']the[' ']weather { return WEATHER; } +how[' ']is[' ']the[' ']weather { return WEATHER; } + +how[' ']are[' ']you { return HOWAREYOU; } +how[' ']you[' ']doing { return HOWAREYOU; } + +what[' ']is[' ']your[' ']favorite[' ']color { return COLOR; } + +how[' ']old[' ']are[' ']you { return AGE; } + +what[' ']is[' ']your[' ']hobby { return HOBBY; } + +\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..95f68487 --- /dev/null +++ b/labs/07/chatbot.y @@ -0,0 +1,67 @@ +%{ +#include +#include + +void yyerror(const char *s); +int yylex(void); +%} + +%token HELLO GOODBYE TIME NAME WEATHER HOWAREYOU COLOR AGE HOBBY + +%% + +chatbot : greeting + | farewell + | query + | name + | weather + | howareyou + | color + | age + | hobby + ; + +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); + } + ; + +name : NAME { printf("Chatbot: My name is Bailey.\n"); } + ; + +weather : WEATHER { printf("Chatbot: I'm not sure about the weather, but I hope it's nice!\n"); } + ; + +howareyou : HOWAREYOU { printf("Chatbot: I'm functioning as expected. How about you?\n"); } + ; + +color : COLOR { printf("Chatbot: My favorite color is pink!\n"); } + ; + +age : AGE { printf("Chatbot: I don't age, I'm just a program.\n"); } + ; + +hobby : HOBBY { printf("Chatbot: I enjoy chatting with users like you!\n"); } + ; + +%% + +int main() { + printf("Chatbot: Hi! Ask me something.\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