diff --git a/labs/01/relation_analyser.py b/labs/01/relation_analyser.py index 6fd8b91..48e1a3f 100644 --- a/labs/01/relation_analyser.py +++ b/labs/01/relation_analyser.py @@ -1,36 +1,48 @@ -import graphviz # https://graphviz.readthedocs.io/en/stable/index.html +#Tuve problemas instalando graphviz por lo que decidi generar un archivo de texto para que lo pueda copiar y pegar en Graphviz online def analyze(val): - """ - Here goes your code to do the analysis - 1. Reflexive: aRa for all a in X, - 2. Symmetric: aRb implies bRa for all a,b in X - 3. Transitive: aRb and bRc imply aRc for all a,b,c in X, - """ - Reflexive = False - Symmetric = False - Transitive = False - - return Reflexive,Symmetric,Transitive - -def plot(): - """ - Here goes your code to do the plot of the set - """ - g = graphviz.Digraph('G', filename='hello.gv') - g.edge('Hello', 'World') - g.view() + + Reflexivo = all((a, a) in val for a, _ in val) + Simetrico = all((b, a) in val for a, b in val) + Transitivo = all((a, c) in val for a, b1 in val for b2, c in val if b1 == b2) + + return Reflexivo, Simetrico, Transitivo + +def text_graph(val): + with open('graph.txt', 'w') as file: + file.write('Digraph {\n') + + for pair in val: + file.write(f'\t{pair[0]} -> {pair[1]} ;\n') + + file.write('}\n') def main(): - print("Hello World analyzing input!") - val = input("Enter your set: ") - print(val) - Reflexive,Symmetric,Transitive = analyze(val) - print(f"\ - 1. Reflexive: {Reflexive} \ - 2. Symmetric: {Symmetric} \ - 3. Transitive: {Transitive}") - plot() + print("Set:") + val_str = input("Ingresa tu set en el formato { (a,b), (c,d), (e,f) ... }: ") + val = eval(val_str) # Convierte string a una tupla + + Reflexivo, Simetrico, Transitivo = analyze(val) + + if Reflexivo: + print("R es reflexivo.") + else: + print("R no es reflexivo.") + + if Simetrico: + print("R es simetrico.") + else: + print("R no es simetrico.") + + if Transitivo: + print("R es transitivo.") + else: + print("R no es transitivo.") + + if Reflexivo and Simetrico and Transitivo: + print("R tiene una relación equivalente") + + text_graph(val) if __name__ == "__main__": main() diff --git a/labs/03/grammar.l b/labs/03/grammar.l new file mode 100644 index 0000000..a06174e --- /dev/null +++ b/labs/03/grammar.l @@ -0,0 +1,20 @@ +%{ +#include "y.tab.h" +%} + +%% + +"a" { return ARTICLE; } +"the" { return ARTICLE; } +"boy"|"girl"|"flower" { return NOUN; } +"touches"|"likes"|"sees" { return VERB; } +"with" { return PREP; } +\n { return EOL; } +. { /* ignore anything else */ } + +%% + +int yywrap() { + return 1; +} + diff --git a/labs/03/grammar.y b/labs/03/grammar.y new file mode 100644 index 0000000..9148005 --- /dev/null +++ b/labs/03/grammar.y @@ -0,0 +1,67 @@ +%{ +#include + +int yylex(); +void yyerror(const char *s); + +#define PASS 1 +#define FAIL 0 + +extern FILE* yyin; + +%} + +%token ARTICLE NOUN VERB PREP EOL + +%% + +sentence_list: sentence EOL { if ($1 == PASS) printf("PASS\n"); else printf("FAIL\n"); } + | sentence_list sentence EOL { if ($2 == PASS) printf("PASS\n"); else printf("FAIL\n"); } + ; + +sentence: noun_phrase verb_phrase { $$ = $1 && $2; } + ; + +noun_phrase: cmplx_noun { $$ = $1; } + | cmplx_noun prep_phrase { $$ = $1 && $2; } + ; + +verb_phrase: cmplx_verb { $$ = $1; } + | cmplx_verb prep_phrase { $$ = $1 && $2; } + ; + +prep_phrase: PREP cmplx_noun { $$ = $2; } + ; + +cmplx_noun: ARTICLE NOUN { $$ = PASS; } + ; + +cmplx_verb: VERB { $$ = PASS; } + | VERB noun_phrase { $$ = $2; } + ; + +%% + +void yyerror(const char *s) { + fprintf(stderr, "%s\n", s); +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + return 1; + } + + FILE *input_file = fopen(argv[1], "r"); + if (!input_file) { + perror("Error opening file"); + return 1; + } + + yyin = input_file; + yyparse(); + fclose(input_file); + + return 0; +} +