Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to query the name of the chatbot #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
26 changes: 26 additions & 0 deletions labs/07/chatbot.l
Original file line number Diff line number Diff line change
@@ -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[' ']the[' ']time { return TIME; }
what[' ']time[' ']is[' ']it { return TIME; }
what[' ']is[' ']your[' ']name { return NAME; } /* Ask for your name */
whats[' ']your[' ']name { return NAME; }
What[' ']your[' ']name { return NAME; }
\n { return 0; } /* End of input on newline */

. { return yytext[0]; }

%%

int yywrap() {
return 1;
}
43 changes: 43 additions & 0 deletions labs/07/chatbot.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
%{
#include <stdio.h>
#include <time.h>

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

%token HELLO GOODBYE TIME 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);
} | NAME { printf("My name is Monty!"); } /* Return my name */
;

%%

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