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/04/example.ac
Binary file not shown.
41 changes: 41 additions & 0 deletions labs/04/lab_04.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
%{
#include <stdio.h>
%}

%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);
}
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[' ']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;
}
68 changes: 68 additions & 0 deletions labs/07/chatbot.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

%{
#include <stdio.h>
#include <time.h>

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