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

Chatbot_Eva_A01568830 #205

Open
wants to merge 3 commits 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
4 changes: 4 additions & 0 deletions labs/04/lex_analyzer_a01568830_lab04/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lex_analyzer: lex_analyzer.l
lex lex_analyzer.l
gcc lex.yy.c -o lex_analyzer -ll

18 changes: 18 additions & 0 deletions labs/04/lex_analyzer_a01568830_lab04/code_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import random

variables = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

code = "// basic code\n"

code += "f {}\n".format(random.choice(variables))

code += "i {}\n".format(random.choice(variables))

code += "{} = {}\n".format(random.choice(variables), random.randint(0, 100))

code += "{} = {} + {}\n".format(random.choice(variables), random.choice(variables), round(random.uniform(0, 100), 5))

code += "p {}\n".format(random.choice(variables))

print(code)

30 changes: 30 additions & 0 deletions labs/04/lex_analyzer_a01568830_lab04/lexic_analyzer.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

%{
#include <stdio.h>
%}

%%

// Regular expressions for tokens
"f" { printf("floatdcl "); }
"i" { printf("intdcl "); }
"p" { printf("print "); }

[a-z] { printf("id "); }
[0-9]+ { printf("inum "); }
[0-9]+"."[0-9]+ { printf("fnum "); }

"=" { printf("assign "); }
"+" { printf("plus "); }

"//" { /* Comment, ignore */ }
\n { /* Newline, ignore */ }
[ \t] { /* Whitespace, ignore */ }
. { printf("Invalid token\n"); }

%%

int main() {
yylex();
return 0;
}
11 changes: 11 additions & 0 deletions labs/07/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
all:
yacc -d chatbot.y
lex chatbot.l
gcc y.tab.c lex.yy.c -o chatbot

clean:
rm -rf chatbot
rm -rf lex.yy.c
rm -rf y.tab.c
rm -rf y.tab.h
rm -rf y.tab.h.gch
23 changes: 23 additions & 0 deletions labs/07/chatbot.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%{
#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; }
\n { return 0; } /* End of input on newline */

. { return yytext[0]; }

%%

int yywrap() {
return 1;
}
31 changes: 31 additions & 0 deletions labs/07/chatbot.tab.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* A Bison parser, made by GNU Bison 2.7. */

/* Bison implementation for Yacc-like parsers in C

Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.

This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
31 changes: 31 additions & 0 deletions labs/07/chatbot.tab.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* A Bison parser, made by GNU Bison 2.7. */

/* Bison interface for Yacc-like parsers in C

Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.

This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
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

%%

chatbot : greeting
| farewell
| query
;

greeting : HELLO { printf("Chatbot: Holaaa? Que quieres\n"); }
;

farewell : GOODBYE { printf("Chatbot: Hasta nunca! Ten un buen día!\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);
}
;

%%

int main() {
printf("Chatbot: Hola! Dime lo que quieras, te escucho. Pero solo sé saludar jaja\n");
while (yyparse() == 0) {
// Loop until end of input
}
return 0;
}

void yyerror(const char *s) {
fprintf(stderr, "Chatbot: Que estas diciendo loco.\n");
}