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
70 changes: 41 additions & 29 deletions labs/01/relation_analyser.py
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 20 additions & 0 deletions labs/03/grammar.l
Original file line number Diff line number Diff line change
@@ -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;
}

67 changes: 67 additions & 0 deletions labs/03/grammar.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
%{
#include <stdio.h>

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 <input_file>\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;
}