Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added labs/07/chatbot
Binary file not shown.
29 changes: 29 additions & 0 deletions labs/07/chatbot.l
Original file line number Diff line number Diff line change
@@ -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; }
"what is your name" { return NAME; }
"name" { return NAME; }
"your name" { return NAME; }
"what is the weather" { return WEATHER; }
"weather" { return WEATHER; }
"how are you" { return HOWAREYOU; }
"mood" { return HOWAREYOU; }
"flip coin" { return FLIP_COIN; }

\n { return 0; } /* End of input on newline */

. { return yytext[0]; }

%%

83 changes: 83 additions & 0 deletions labs/07/chatbot.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
%{
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void yyerror(const char *s);
int yylex(void);
%}

%token HELLO GOODBYE TIME NAME WEATHER HOWAREYOU FLIP_COIN

%%

chatbot : greeting
| farewell
| query
| inquiry
| coinflip
;

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);
}
;

inquiry : NAME { printf("Chatbot: My name is Jeff.\n"); }
| WEATHER {
srand(time(NULL));
int temperature = 25 + rand() % 16;
printf("Chatbot: The weather today is %d degrees Celsius outside.\n", temperature);
}
| HOWAREYOU {
srand(time(NULL));
const char *moods[] = {
"happy 😊",
"sad 😢",
"excited 😆",
"angry 😠",
"bored 😐",
"confused 😕",
"surprised 😮",
"tired 😴",
"scared 😱",
"calm 😌"
};
int moodIndex = rand() % 10;
printf("Chatbot: I'm feeling %s\n", moods[moodIndex]);
}
;

coinflip: FLIP_COIN {
srand(time(NULL));
int result = rand() % 2;
if (result == 0) {
printf("Chatbot: It's heads! 🪙\n");
} else {
printf("Chatbot: It's tails! 🪙\n");
}
}
;

%%

int main() {
printf("Chatbot: Hi! You can greet me, ask for the time, ask my name, ask my mood, inquire about the weather, flip a coin, 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");
}