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

A01639224-homework-07 #199

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
30 changes: 30 additions & 0 deletions labs/07/chatbot.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%{
#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; }
name { return NAME; }
what[' ']is[' ']your[' ']name { return NAME; }
weather { return WEATHER; }
what[' ']is[' ']the[' ']weather { return WEATHER; }
how[' ']are[' ']you { return MOOD; }
how[' ']are[' ']you[' ']doing { return MOOD; }
help { return HELP; }
\n { return 0; } /* End of input on newline */

. { return yytext[0]; }

%%

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

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

%token HELLO GOODBYE TIME NAME WEATHER MOOD HELP

%%

chatbot : greeting
| farewell
| time_query
| name_query
| weather_query
| mood_query
| help_query
;

greeting : HELLO { printf("MarIA: Hello! How can I help you today?\n"); }
;

farewell : GOODBYE { printf("MarIA: Goodbye! Have a great day!\n"); }
;

time_query : TIME {
time_t now = time(NULL);
struct tm *local = localtime(&now);
printf("MarIA: The current time is %02d:%02d.\n", local->tm_hour, local->tm_min);
}
;

name_query : NAME {
printf("MarIA: My name is MarIA, nice to meet you!\n");
}
;

weather_query : WEATHER {
int status = system("curl -s wttr.in > /dev/null");
if (status == 0) {
printf("MarIA: Here's the weather in Guadalajara, Mexico:\n");
system("curl -s wttr.in/Guadalajara,Mexico | sed -n '3,7p'");
} else {
printf("MarIA: There seems to be an issue. Maybe try again later?\n");
}
}
;

mood_query : MOOD {
printf("MarIA: I'm fine, thanks for asking.\n");
}
;

help_query : HELP {
printf("MarIA: You can\n");
printf(" • Greet me\n");
printf(" • Ask my name\n");
printf(" • Ask how I am doing\n");
printf(" • Ask for the time\n");
printf(" • Ask for the weather\n");
printf(" • Say goodbye\n");
}
;

%%

int main() {
printf("MarIA: Hi, I'm MarIA! Type 'help' to see what you can ask me.\n");
while (yyparse() == 0) {
// Loop until end of input
}
return 0;
}

void yyerror(const char *s) {
fprintf(stderr, "MarIA: I didn't understand that.\n");
}