Skip to content

Commit 82fc1e2

Browse files
author
Rodrigo Chavez
committed
A01635547-homework-04
Signed-off-by: Rodrigo Chavez <rodrigo.chavez@oracle.com>
1 parent 88a21fc commit 82fc1e2

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

labs/04/Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
LDFLAGS = -L/opt/homebrew/opt/flex/lib
2+
CPPFLAGS = -I/opt/homebrew/opt/flex/include
3+
4+
# generate
5+
example: code_generator.py
6+
python3 code_generator.py > example.ac
7+
8+
# compile
9+
gen_lex:
10+
lex language_ac.l
11+
gcc $(CPPFLAGS) $(LDFLAGS) lex.yy.c -o lexical_scan -lfl
12+
13+
test: example.ac
14+
./lexical_scan < example.ac
15+
# run lex, then compile lex.yy.c then run lexical_scan
16+
#
17+
stress:
18+
lex language_ac.l
19+
gcc $(CPPFLAGS) $(LDFLAGS) lex.yy.c -o lexical_scan -lfl
20+
python3 code_generator.py --stress
21+
./lexical_scan < random_code.ac
22+
run:
23+
lex language_ac.l
24+
gcc $(CPPFLAGS) $(LDFLAGS) lex.yy.c -o lexical_scan -lfl
25+
python3 code_generator.py > example.ac
26+
./lexical_scan < example.ac
27+
28+
# Limpiar archivos generados
29+
clean:
30+
rm -f lex.yy.c lexical_scan example.ac

labs/04/code_generator.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import string
2+
import random
3+
import argparse
4+
5+
def id_generator(size=10, chars=string.ascii_uppercase + string.digits):
6+
return ''.join(random.choice(chars) for _ in range(size))
7+
8+
opreators = ["+","-","*","/"]
9+
10+
def get_comment_line():
11+
comment_line = "//%s" % (id_generator())
12+
return comment_line
13+
14+
def get_float_line():
15+
float_line = "f %s" % (random.choice(string.ascii_lowercase))
16+
return float_line
17+
18+
def get_integer_line():
19+
integer_line = "i %s"% (random.choice(string.ascii_lowercase))
20+
return integer_line
21+
22+
def get_asigment_line():
23+
asigment_line = "%s = %s" %(random.choice(string.ascii_lowercase),\
24+
random.randint(0,100))
25+
return asigment_line
26+
27+
def get_asigment_line_2():
28+
asigment_line_2 = "%s = %s %s %s" % \
29+
(random.choice(string.ascii_lowercase),\
30+
random.choice(string.ascii_lowercase),\
31+
random.choice(opreators),\
32+
random.randint(0,100))
33+
return asigment_line_2
34+
35+
def get_print_line():
36+
print_line = "p %s" % (random.choice(string.ascii_lowercase))
37+
return print_line
38+
39+
40+
parser = argparse.ArgumentParser(description='Generate random AC code')
41+
parser.add_argument('--stress', dest='stress', action='store_true',\
42+
help='generate HUGE code to stress the lab')
43+
args = parser.parse_args()
44+
45+
if args.stress:
46+
f= open("random_code.ac","w+")
47+
for x in range(0, 100000):
48+
comment_line = get_comment_line()
49+
float_line = get_float_line()
50+
integer_line = get_integer_line()
51+
asigment_line = get_asigment_line()
52+
asigment_line_2 = get_asigment_line_2()
53+
print_line = get_print_line()
54+
55+
f.write(comment_line + "\n")
56+
f.write(float_line + "\n")
57+
f.write(integer_line + "\n")
58+
f.write(asigment_line+ "\n")
59+
f.write(asigment_line_2 + "\n")
60+
f.write(print_line + "\n")
61+
62+
f.close()
63+
64+
else:
65+
comment_line = get_comment_line()
66+
float_line = get_float_line()
67+
integer_line = get_integer_line()
68+
asigment_line = get_asigment_line()
69+
asigment_line_2 = get_asigment_line_2()
70+
print_line = get_print_line()
71+
72+
print(comment_line)
73+
print(float_line)
74+
print(integer_line)
75+
print(asigment_line)
76+
print(asigment_line_2)
77+
print(print_line)
78+

labs/04/example.ac

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//OFKZ5W996B
2+
f q
3+
i q
4+
r = 61
5+
o4 = f - 98
6+
p k

labs/04/language_ac.l

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
%{
2+
#include <stdio.h>
3+
%}
4+
5+
%option noyywrap
6+
7+
letter [a-eg-hj-oq-zA-EG-HJ-OQ-Z]
8+
digit [0-9]
9+
number {digit}+
10+
11+
%%
12+
\/{2}.* { printf("COMMENT "); }
13+
f { printf("floatdcl "); }
14+
i { printf("intdcl "); }
15+
= { printf("assign "); }
16+
\+ { printf("plus "); }
17+
\- { printf("minus "); }
18+
\* { printf("multiplication "); }
19+
\/ { printf("division "); }
20+
p { printf("print "); }
21+
({letter}{digit}*)+ { printf("id "); }
22+
(0|([1-9]{digit}*))\.(({digit}*[1-9])|0) { printf("fnum "); }
23+
{number} { printf("inum "); }
24+
[ \t]+ /* ignore whitespace */
25+
%%
26+
27+
int main() {
28+
yylex();
29+
return 0;
30+
}

labs/04/lexical_scan

51.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)