From aadd9631ad1c1d01384837a75d8eefcf8f4b9452 Mon Sep 17 00:00:00 2001 From: Luis Alcantara Date: Mon, 6 May 2024 19:46:19 -0600 Subject: [PATCH 1/2] A01634185-homework-04 Signed-off-by: Luis Alcantara --- labs/04/example.ac | Bin 0 -> 100 bytes labs/04/lab_04.l | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 labs/04/example.ac create mode 100644 labs/04/lab_04.l diff --git a/labs/04/example.ac b/labs/04/example.ac new file mode 100644 index 0000000000000000000000000000000000000000..c83876af78dee2bdeca3da6a3725f12a80d1db77 GIT binary patch literal 100 zcmezWPoF`b!HvO_A(Fv^!4ybGF!%xa1`NCmTnuRp3Jm#RRwj^@2xgT4S++oI$zTZP XmqPjZKw1}wjTy|q;sro<1(*c@ +%} + +%option noyywrap + +%% +"//".* { printf("comment "); } +"f" { printf("floatdcl "); } +"i" { printf("intdcl "); } +"p" { printf("print "); } +"+" { printf("plus "); } +"-" { printf("minus "); } +"/" { printf("division "); } +"*" { printf("times "); } +"=" { printf("assign "); } +[0-9]+ { printf("integer "); } +[0-9]+"."[0-9]+ { printf("fnum "); } +[a-eghj-oq-z] { printf("id "); } +[ \t]+ /* ignore whitespace */ +. /* ignore other characters */ +%% + +int main(int argc, char **argv) { + FILE *fd; + + if (argc == 2) + { + if (!(fd = fopen(argv[1], "r"))) + { + perror("Error: "); + return (-1); + } + yyset_in(fd); + yylex(); + fclose(fd); + } + else + printf("Usage: a.out filename\n"); + return (0); +} \ No newline at end of file From 983d1e626f86d2e5f095907cd498b6f92d58e36c Mon Sep 17 00:00:00 2001 From: Luis Alcantara Date: Fri, 31 May 2024 23:48:28 -0600 Subject: [PATCH 2/2] A01634185-homework-07 Signed-off-by: Luis Alcantara --- labs/07/chatbot.l | 29 ++++++++++++++++++++ labs/07/chatbot.y | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 labs/07/chatbot.l create mode 100644 labs/07/chatbot.y diff --git a/labs/07/chatbot.l b/labs/07/chatbot.l new file mode 100644 index 00000000..f4018aae --- /dev/null +++ b/labs/07/chatbot.l @@ -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[' ']in[' ']Mexico { return TIME; } +what[' ']time[' ']is[' ']it[' ']in[' ']Mexico { return TIME; } +what[' ']is[' ']the[' ']time[' ']in[' ']Japan { return TIMEJ; } +what[' ']time[' ']is[' ']it[' ']in[' ']Japan { return TIMEJ; } +how[' ']are[' ']you {return STATUS; } +what[' ']is[' ']your[' ']name {return NAME; } +name { return NAME; } + +\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..29d7bb84 --- /dev/null +++ b/labs/07/chatbot.y @@ -0,0 +1,68 @@ + +%{ +#include +#include + +void yyerror(const char *s); +int yylex(void); +%} + +%token HELLO GOODBYE TIME STATUS NAME TIMEJ + +%% + +chatbot : greeting + | farewell + | querytime + | wellbeing + | name + ; + +greeting : HELLO { printf("Chatbot: Hey Bro! How can I help you today?\n"); } + ; + +farewell : GOODBYE { printf("Chatbot: Goodbye! Have a great day!\n"); } + ; + +querytime : TIME { + /*time zone established in Mexico*/ + setenv("TZ", "America/Mexico_City", 1); + tzset(); + time_t now = time(NULL); + struct tm *local = localtime(&now); + printf("Chatbot: The current time in Mexico is %02d:%02d.\n", local->tm_hour, local->tm_min); + } + ; +wellbeing : STATUS { + /*Response to status question*/ + printf("Chatbot: I'm doing well, thanks for asking\n"); + } + ; +name : NAME { + /*Response to name question*/ + printf ("Chatbot: I'm BeatBot\n"); + } + ; +querytime : TIMEJ { + /*time zone established in Japan*/ + setenv("TZ", "Asia/Tokyo", 1); + tzset(); + time_t now = time(NULL); + struct tm *local = localtime(&now); + printf("Chatbot: The current time in Japan is %02d:%02d.\n", local->tm_hour, local->tm_min); + } + ; + +%% + +int main() { + printf("Chatbot: Hi! I'm BeatBot. You can ask me for the time in Mexico and Japan, my name, greet me, 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"); +} \ No newline at end of file