From be36106cdcda8d4abb4a6771c73f79c18e076459 Mon Sep 17 00:00:00 2001 From: David Glez Date: Tue, 29 Apr 2025 21:31:14 -0600 Subject: [PATCH] A01633817-homework-04 --- labs/04/A01633817-homework-04/Makefile | 10 +++ .../A01633817-homework-04/code_generator.py | 78 +++++++++++++++++++ labs/04/A01633817-homework-04/example.ac | 16 ++++ labs/04/A01633817-homework-04/lex_analaizer.l | 28 +++++++ 4 files changed, 132 insertions(+) create mode 100644 labs/04/A01633817-homework-04/Makefile create mode 100644 labs/04/A01633817-homework-04/code_generator.py create mode 100644 labs/04/A01633817-homework-04/example.ac create mode 100644 labs/04/A01633817-homework-04/lex_analaizer.l diff --git a/labs/04/A01633817-homework-04/Makefile b/labs/04/A01633817-homework-04/Makefile new file mode 100644 index 00000000..4ab18d89 --- /dev/null +++ b/labs/04/A01633817-homework-04/Makefile @@ -0,0 +1,10 @@ +all: + flex lex_analaizer.l + gcc lex.yy.c -o lex_analaizer -lfl + +run: all + ./lex_analaizer example.ac + +clean: + rm -f lex.yy.c lex_analaizer + diff --git a/labs/04/A01633817-homework-04/code_generator.py b/labs/04/A01633817-homework-04/code_generator.py new file mode 100644 index 00000000..4ae97e16 --- /dev/null +++ b/labs/04/A01633817-homework-04/code_generator.py @@ -0,0 +1,78 @@ +import string +import random +import argparse + +def id_generator(size=10, chars=string.ascii_uppercase + string.digits): + return ''.join(random.choice(chars) for _ in range(size)) + +opreators = ["+","-","*","/"] + +def get_comment_line(): + comment_line = "//%s" % (id_generator()) + return comment_line + +def get_float_line(): + float_line = "f %s" % (random.choice(string.ascii_lowercase)) + return float_line + +def get_integer_line(): + integer_line = "i %s"% (random.choice(string.ascii_lowercase)) + return integer_line + +def get_asigment_line(): + asigment_line = "%s = %s" %(random.choice(string.ascii_lowercase),\ + random.randint(0,100)) + return asigment_line + +def get_asigment_line_2(): + asigment_line_2 = "%s = %s %s %s" % \ + (random.choice(string.ascii_lowercase),\ + random.choice(string.ascii_lowercase),\ + random.choice(opreators),\ + random.randint(0,100)) + return asigment_line_2 + +def get_print_line(): + print_line = "p %s" % (random.choice(string.ascii_lowercase)) + return print_line + + +parser = argparse.ArgumentParser(description='Generate random AC code') +parser.add_argument('--stress', dest='stress', action='store_true',\ + help='generate HUGE code to stress the lab') +args = parser.parse_args() + +if args.stress: + f= open("random_code.ac","w+") + for x in range(0, 100000): + comment_line = get_comment_line() + float_line = get_float_line() + integer_line = get_integer_line() + asigment_line = get_asigment_line() + asigment_line_2 = get_asigment_line_2() + print_line = get_print_line() + + f.write(comment_line + "\n") + f.write(float_line + "\n") + f.write(integer_line + "\n") + f.write(asigment_line+ "\n") + f.write(asigment_line_2 + "\n") + f.write(print_line + "\n") + + f.close() + +else: + comment_line = get_comment_line() + float_line = get_float_line() + integer_line = get_integer_line() + asigment_line = get_asigment_line() + asigment_line_2 = get_asigment_line_2() + print_line = get_print_line() + + print(comment_line) + print(float_line) + print(integer_line) + print(asigment_line) + print(asigment_line_2) + print(print_line) + diff --git a/labs/04/A01633817-homework-04/example.ac b/labs/04/A01633817-homework-04/example.ac new file mode 100644 index 00000000..1d181b73 --- /dev/null +++ b/labs/04/A01633817-homework-04/example.ac @@ -0,0 +1,16 @@ +// basic code + +//float b +f b + +// integer a +i a + +// a = 5 +a = 5 + +// b = a + 3.2 +b = a + 3.2 + +//print 8.5 +p b diff --git a/labs/04/A01633817-homework-04/lex_analaizer.l b/labs/04/A01633817-homework-04/lex_analaizer.l new file mode 100644 index 00000000..231b3ca0 --- /dev/null +++ b/labs/04/A01633817-homework-04/lex_analaizer.l @@ -0,0 +1,28 @@ +%{ +#include +%} + +%% +"//".* { printf("COMMENT\n"); } +f[ \t]+[a-z] { printf("floatdcl id\n"); } +i[ \t]+[a-z] { printf("intdcl id\n"); } +[a-z][ \t]*=[ \t]*[0-9]+ { printf("id assign inum\n"); } +[a-z][ \t]*=[ \t]*[a-z][ \t]*\+[ \t]*[0-9]+\.[0-9]+ { printf("id assign id plus fnum\n"); } +p[ \t]+[a-z] { printf("print id\n"); } +[ \t\n]+ { /* ignora whitespace */ } +. { /* ignora todo lo demas */ } +%% + +int main(int argc, char **argv) { + if (argc > 1) { + FILE *file = fopen(argv[1], "r"); + if (!file) { + perror("File opening failed"); + return 1; + } + yyin = file; + } + yylex(); + return 0; +} +