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
42 changes: 42 additions & 0 deletions labs/07/chatbot.l
Original file line number Diff line number Diff line change
@@ -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;
}
67 changes: 67 additions & 0 deletions labs/07/chatbot.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
%{
#include <stdio.h>
#include <time.h>

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