Skip to content

Commit 45ac248

Browse files
committed
created lab 7
1 parent b18b035 commit 45ac248

File tree

14 files changed

+4038
-0
lines changed

14 files changed

+4038
-0
lines changed

7LAB/.clang-format

Lines changed: 550 additions & 0 deletions
Large diffs are not rendered by default.

7LAB/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# FLAGS = -O0 -Wall -Wextra -pedantic
2+
3+
# the gcc options so I don't have to copy paste them everywhere
4+
# any time I make changes to them.
5+
FLAGS = -O0 -Wextra -pedantic
6+
7+
# Entry point target
8+
all: lab6
9+
10+
# Compile all the THINGS!
11+
lab6: lab6.l lab6.y ast.c ast.h
12+
lex lab6.l
13+
yacc -d --verbose --debug lab6.y
14+
gcc $(FLAGS) y.tab.c lex.yy.c ast.c -o lab6 -lm
15+
16+
# remove the binary and other generated stuff
17+
clean:
18+
rm lab6 lex.yy.c y.tab.c y.tab.h
19+
20+
# runs valgrind to look for any memory mistakes
21+
memcheck:
22+
valgrind ./lab6 -s
23+
24+
# executes our code to test it's functionality
25+
test:
26+
./lab6 < testinput.txt

7LAB/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# CS370 Compilers Lab 5
2+
## Ziad Arafat - Mar 8 2021
3+
4+
In this lab we take our previous lab code and implement the proper syntax rules from the DECAF programming language.
5+
6+
## Screenshots
7+
#### Good Test
8+
![alt](GoodTest.png)
9+
10+
#### Error Test
11+
12+
![alt](ErrorTest.png)
13+
14+
## Changelog
15+
16+
#### LEX
17+
- added the DECAF tokens
18+
- Added some debug prints to the tokens
19+
- Added square brackets to the lex directive
20+
- Added '<' token to the directive
21+
- Added modulus '%'
22+
- Added directive for HEX numbers
23+
- Created function to convert hex numbers to their int value
24+
- Disabled the newline return token
25+
26+
#### YACC
27+
- added all the rules from DECAF and reformatted them to work with Bison */
28+
- for each of the rules with a list of tokens I created a recursive rule that allows for one or more of the token.
29+
- Replaced any instance of '=' with T_ASSIGN

7LAB/ast.c

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
/* Abstract syntax tree code
2+
3+
This code is used to define an AST node,
4+
routine for printing out the AST
5+
defining an enumerated type so we can figure out what we need to
6+
do with this. The ENUM is basically going to be every non-terminal
7+
and terminal in our language.
8+
9+
Shaun Cooper February 2020
10+
11+
*/
12+
13+
#include <stdio.h>
14+
#include <malloc.h>
15+
#include "ast.h"
16+
17+
/* uses malloc to create an ASTnode and passes back the heap address of the newley created node */
18+
ASTnode *ASTCreateNode(enum AST_Tree_Element_Type mytype)
19+
{
20+
ASTnode *p;
21+
if (mydebug)
22+
fprintf(stderr, "Creating AST Node \n");
23+
p = (ASTnode *)malloc(sizeof(ASTnode)); // get head data
24+
p->type = mytype; // set up the Element type
25+
p->S1 = NULL;
26+
p->S2 = NULL; // det default values
27+
p->value = 0;
28+
return (p);
29+
}
30+
31+
/* Helper function to print tabbing */
32+
33+
void PT(int howmany)
34+
{
35+
for (int i = 0; i < howmany; i++) {
36+
printf(" ");
37+
}
38+
}
39+
40+
void AST_Print_Type(enum AST_Decaf_Types t)
41+
{
42+
switch (t) {
43+
case A_Decaf_INT:
44+
printf(" INT ");
45+
break;
46+
case A_Decaf_BOOL:
47+
printf(" BOOLEAN ");
48+
break;
49+
case A_Decaf_STRING:
50+
printf(" STRING ");
51+
break;
52+
case A_Decaf_VOID:
53+
printf(" VOID ");
54+
break;
55+
default:
56+
fprintf(stderr, "Unknown AST DECAF TYPE !!!\n");
57+
} // of switch
58+
59+
} // of AST_Print_Type
60+
61+
/* Print out the abstract syntax tree */
62+
void ASTprint(int level, ASTnode *p)
63+
{
64+
int i;
65+
if (p == NULL)
66+
return;
67+
else {
68+
PT(level); /* indent */
69+
switch (p->type) {
70+
case A_VAR_RVALUE:
71+
printf("Variable %s \n", p->name);
72+
if (p->S1 != NULL) {
73+
PT(level + 2);
74+
printf("[\n");
75+
ASTprint(level+2, p->S1);
76+
PT(level + 2);
77+
printf("]\n");
78+
}
79+
break;
80+
case A_PROGRAM:
81+
// printf("PROGRAM \n");
82+
ASTprint(level + 1, p->S1);
83+
ASTprint(level + 1, p->S2);
84+
break;
85+
case A_EXTERN:
86+
printf("EXTERN FUNC %s\n", p->name);
87+
// printf("\n");
88+
ASTprint(level + 1, p->S1);
89+
// printf("\n");
90+
91+
printf("END EXTERN with Type:\n ");
92+
AST_Print_Type(p->A_Declared_Type);
93+
break;
94+
95+
case A_PACKAGE:
96+
printf("Package : %s\n", p->name);
97+
PT(level + 2);
98+
// printf("{\n");
99+
ASTprint(level + 1, p->S1);
100+
ASTprint(level + 1, p->S2);
101+
PT(level + 2);
102+
printf("\n");
103+
break;
104+
case A_ExternType:
105+
printf("EXTERN Type ");
106+
AST_Print_Type(p->A_Declared_Type);
107+
printf("\n");
108+
109+
break;
110+
case A_VARDEC:
111+
printf("Variable ");
112+
printf(" %s", p->name);
113+
114+
if (p->S1 != NULL) {
115+
printf("[");
116+
printf("%d", p->S1->value);
117+
printf("] ");
118+
}
119+
printf(" with type ");
120+
AST_Print_Type(p->A_Declared_Type);
121+
printf(" ");
122+
if (p->S2 != NULL) {
123+
printf("= %d", p->S2->value);
124+
}
125+
printf("\n");
126+
// ASTprint(level, p->S1);
127+
break;
128+
case A_METHODDEC:
129+
printf("METHOD FUNCTION '%s' with type ", p->name);
130+
AST_Print_Type(p->A_Declared_Type);
131+
/* print out the parameter list */
132+
if (p->S1 == NULL) {
133+
printf("\n");
134+
PT(level + 2);
135+
printf(" (NONE) ");
136+
} else {
137+
printf("\n");
138+
PT(level + 2);
139+
printf("(\n");
140+
ASTprint(level + 2, p->S1);
141+
PT(level + 2);
142+
printf(")");
143+
}
144+
printf("\n");
145+
ASTprint(level + 2, p->S2); // print out the block
146+
break;
147+
148+
case A_METHODID:
149+
printf("Method Variable");
150+
printf(" %s", p->name);
151+
AST_Print_Type(p->A_Declared_Type);
152+
printf("\n");
153+
154+
break;
155+
case A_PARAM:
156+
printf("PARAMETER ");
157+
if (p->operator== A_Decaf_INT)
158+
printf(" INT ");
159+
if (p->operator== A_Decaf_BOOL)
160+
printf(" VOID ");
161+
if (p->operator== A_Decaf_VOID)
162+
printf(" BOOLEAN ");
163+
printf("%s ", p->name);
164+
if (p->value == -1)
165+
printf("[]");
166+
ASTprint(level + 2, p->S1);
167+
printf("\n");
168+
break;
169+
case A_EXPR:
170+
printf("EXPR ");
171+
switch (p->operator) {
172+
case A_PLUS:
173+
printf(" + ");
174+
break;
175+
case A_MINUS:
176+
printf(" - ");
177+
break;
178+
case A_DIVIDE:
179+
printf(" / ");
180+
break;
181+
case A_EQ:
182+
printf(" == ");
183+
break;
184+
case A_LEQ:
185+
printf(" <= ");
186+
break;
187+
case A_GEQ:
188+
printf(" >= ");
189+
break;
190+
case A_GT:
191+
printf(" > ");
192+
break;
193+
case A_LT:
194+
printf(" < ");
195+
break;
196+
case A_LEFTSHIFT:
197+
printf(" << ");
198+
break;
199+
case A_RIGHTSHIFT:
200+
printf(" >> ");
201+
break;
202+
case A_MOD:
203+
printf(" %% ");
204+
break;
205+
case A_AND:
206+
printf(" && ");
207+
break;
208+
case A_OR:
209+
printf(" || ");
210+
break;
211+
case A_NEQ:
212+
printf(" != ");
213+
break;
214+
case A_TIMES:
215+
printf(" * ");
216+
break;
217+
case A_NOT:
218+
printf(" !");
219+
break;
220+
case A_UMINUS:
221+
// print words so we know it's not just minus
222+
printf(" Unary Minus");
223+
break;
224+
default:
225+
printf("Unknown EXPR Operator");
226+
}
227+
printf("\n");
228+
ASTprint(level + 1, p->S1);
229+
if (p->operator!= A_NOT && p->operator != A_UMINUS)
230+
ASTprint(level + 1, p->S2);
231+
break;
232+
case A_BLOCK:
233+
printf("BLOCK STATEMENT \n");
234+
ASTprint(level + 1, p->S1);
235+
ASTprint(level + 1, p->S2);
236+
break;
237+
case A_WHILESTMT:
238+
printf("WHILE STATEMENT \n");
239+
ASTprint(level + 1, p->S1);
240+
ASTprint(level + 2, p->S2);
241+
break;
242+
243+
case A_CONSTANT_INT:
244+
printf("CONSTANT INTEGER %d\n", p->value);
245+
break;
246+
case A_CONSTANT_STRING:
247+
printf("CONSTANT STRING %s\n", p->name);
248+
break;
249+
250+
case A_CONSTANT_BOOL:
251+
printf("CONSTANT BOOLEAN ");
252+
if (p->value == 1) {
253+
printf("true\n");
254+
} else {
255+
printf("false\n");
256+
}
257+
break;
258+
case A_BREAK:
259+
printf("BREAK STATEMENT \n");
260+
break;
261+
case A_RETURN:
262+
printf("RETURN STATEMENT \n");
263+
ASTprint(level + 1, p->S1);
264+
break;
265+
case A_ASSIGN:
266+
printf("ASSIGNMENT STATEMENT\n");
267+
ASTprint(level + 1, p->S1);
268+
ASTprint(level + 1, p->S2);
269+
break;
270+
case A_IF:
271+
printf("IF STATEMENT \n");
272+
ASTprint(level + 1, p->S1);
273+
ASTprint(level + 1, p->S2);
274+
break;
275+
case A_IFBODY:
276+
// printf("IF BODY \n");
277+
ASTprint(level + 1, p->S1);
278+
ASTprint(level + 1, p->S2);
279+
break;
280+
281+
case A_ELSE:
282+
printf("ELSE \n");
283+
ASTprint(level + 1, p->S1);
284+
break;
285+
case A_METHODCALL:
286+
printf(" METHOD CALL name: %s\n", p->name);
287+
PT(level);
288+
printf("(\n");
289+
printf("METHOD ARG \n");
290+
ASTprint(level + 1, p->S1);
291+
PT(level);
292+
printf(")\n");
293+
break;
294+
295+
default:
296+
printf("unknown type in ASTprint\n");
297+
}
298+
ASTprint(level, p->next);
299+
}
300+
}
301+
302+
/* dummy main program so I can compile for syntax error independently
303+
*/
304+
// void main()
305+
// {
306+
// }
307+
/* */

0 commit comments

Comments
 (0)