mirror of
https://github.com/RustyRaptor/compilers.git
synced 2026-12-03 15:15:17 -07:00
documented lab4
This commit is contained in:
1 parent
b5bba5b51d
commit
1df0a0f271
11 files changed
+98
-35
No files matched your search
+1
-1
@@ -23,4 +23,4 @@ memcheck:
|
||||
|
||||
# executes our code to test it's functionality
|
||||
test:
|
||||
./lab4
|
||||
./lab4 < testinput.txt
|
||||
@@ -0,0 +1,43 @@
|
||||
# CS 370 Compilers
|
||||
## Ziad Arafat - Feb 28 2021
|
||||
|
||||
### Overview
|
||||
In this project we are combining and extending the symbol table utilities we built and the calculator we built in order to create basic data types and more sophisticated syntax.
|
||||
|
||||
### To run code
|
||||
|
||||
- to build run:
|
||||
|
||||
```bash
|
||||
make
|
||||
```
|
||||
|
||||
- to test the code with the testinput.txt file run:
|
||||
|
||||
```bash
|
||||
make test
|
||||
```
|
||||
|
||||
- to clean the generated files
|
||||
|
||||
```bash
|
||||
make clean
|
||||
```
|
||||
|
||||
### Screenshots
|
||||
|
||||
1. Symbol table is full
|
||||
|
||||

|
||||
|
||||
2. The variable already exists
|
||||
|
||||

|
||||
|
||||
3. The variable doesn't exist
|
||||
|
||||

|
||||
|
||||
4. Left and right assignment usage test
|
||||
|
||||

|
||||
@@ -0,0 +1,7 @@
|
||||
int fuck;
|
||||
int hoe;
|
||||
fuck = 5
|
||||
hoe = 6
|
||||
fuck + hoe
|
||||
hoe + 8
|
||||
9 + 9
|
||||
+1
-1
@@ -19,12 +19,12 @@ int yylex();
|
||||
- added an entry for parenthesis to the tokens directive
|
||||
- added int directive to return T_INT token
|
||||
- changed the directive for VARIABLE to accept multichar variables
|
||||
- added semicolon as a token
|
||||
*/
|
||||
%%
|
||||
"int" {
|
||||
return(T_INT);
|
||||
}
|
||||
|
||||
[a-zA-Z][a-zA-Z0-9]* {
|
||||
if (mydebug) fprintf(stderr,"Letter found\n");
|
||||
yylval.string=strdup(yytext);
|
||||
|
||||
+31
-33
@@ -4,9 +4,9 @@
|
||||
#include "symtable.h"
|
||||
int yylex();
|
||||
|
||||
int regSize = 99999;
|
||||
int regSize = 5; // created variable to hold size of the regs array
|
||||
|
||||
int regs[9999];
|
||||
int regs[5]; // changed size to be big enough
|
||||
int base, debugsw;
|
||||
|
||||
void yyerror (s) /* Called by yyparse on error */
|
||||
@@ -17,10 +17,11 @@ void yyerror (s) /* Called by yyparse on error */
|
||||
|
||||
|
||||
%}
|
||||
/* defines the start symbol, what values come back from LEX and how the operators are associated */
|
||||
|
||||
%start P
|
||||
/* defines the start symbol, what values come back from LEX and how the operators are associated changed to P */
|
||||
/* changed to P */
|
||||
%start P
|
||||
|
||||
/* Defines possible types */
|
||||
%union
|
||||
{
|
||||
int value;
|
||||
@@ -28,10 +29,17 @@ void yyerror (s) /* Called by yyparse on error */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
set expr to have type value
|
||||
INTEGER is type value
|
||||
VARIABLE is type string
|
||||
*/
|
||||
%type <value> expr
|
||||
|
||||
%token <value> INTEGER
|
||||
%token <string> VARIABLE
|
||||
|
||||
%token T_INT
|
||||
|
||||
%left '|'
|
||||
@@ -53,15 +61,21 @@ void yyerror (s) /* Called by yyparse on error */
|
||||
|
||||
%% /* end specs, begin rules */
|
||||
|
||||
/* add the context free grammar rules */
|
||||
|
||||
/* start */
|
||||
P : DECLS list
|
||||
;
|
||||
|
||||
/* define DECLS as declarations or empty */
|
||||
DECLS : DECLS DECL
|
||||
| /* empty */
|
||||
;
|
||||
|
||||
/* define declaration as int var; newline */
|
||||
DECL : T_INT VARIABLE ';' '\n'
|
||||
{
|
||||
/* check if the variable exists then insert or barf */
|
||||
if (Search($2) == 1 ) {
|
||||
printf("The variable exists already");
|
||||
} else if (size >= regSize) {
|
||||
@@ -71,28 +85,6 @@ DECL : T_INT VARIABLE ';' '\n'
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// check if VARIABLE $2 is already defined
|
||||
// if not check to make sure our symtab has enuf room
|
||||
// you should make a global variable that defines the size
|
||||
// of our memory
|
||||
};
|
||||
|
||||
DECL : T_INT VARIABLE '=' expr ';' '\n'
|
||||
{
|
||||
if (Search($2) == 1 ) {
|
||||
printf("The variable exists already");
|
||||
} else if (size >= regSize) {
|
||||
printf("The table is full");
|
||||
} else {
|
||||
Insert($2, size);
|
||||
regs[FetchAddress($2)] = $4;
|
||||
}
|
||||
// check if VARIABLE $2 is already defined
|
||||
// if not check to make sure our symtab has enuf room
|
||||
// you should make a global variable that defines the size
|
||||
// of our memory
|
||||
};
|
||||
|
||||
list : /* empty */
|
||||
@@ -104,7 +96,9 @@ list : /* empty */
|
||||
stat : expr
|
||||
{ fprintf(stderr,"The answer is %d\n", $1); }
|
||||
| VARIABLE '=' expr {
|
||||
// check if it exists first or barf
|
||||
if (Search($1) == 1) {
|
||||
// now we get the correct address from the table
|
||||
regs[FetchAddress($1)] = $3;
|
||||
} else {
|
||||
printf("Undefined variable");
|
||||
@@ -131,13 +125,17 @@ expr : '(' expr ')'
|
||||
{ $$ = -$2; }
|
||||
| VARIABLE
|
||||
{
|
||||
if (Search($1) == 1) {
|
||||
$$ = regs[FetchAddress($1)];
|
||||
} else {
|
||||
printf("Undefined variable");
|
||||
}
|
||||
// check if it exists before trying to access
|
||||
if (Search($1) == 0) {
|
||||
printf("Undefined variable %s\n", $1);
|
||||
$$ = 0; // give it a default value
|
||||
|
||||
} else {
|
||||
|
||||
// get the address to find value
|
||||
$$ = regs[FetchAddress($1)];
|
||||
}
|
||||
|
||||
/* fprintf(stderr,"Found a variable value = %d\n",$$); */
|
||||
}
|
||||
| INTEGER {$$=$1; fprintf(stderr,"found an integer \n");}
|
||||
;
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 95 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 71 KiB |
@@ -233,6 +233,9 @@ void Delete(char *sym)
|
||||
/*
|
||||
FetchAddress
|
||||
|
||||
input: char variable
|
||||
output: integer address
|
||||
|
||||
Modified version of Search. Returns the address integer instead of flag.
|
||||
*/
|
||||
int FetchAddress(char *s)
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 47 KiB |
@@ -0,0 +1,12 @@
|
||||
int var1;
|
||||
int var2;
|
||||
int var3;
|
||||
var1 = 5
|
||||
var2 = 6
|
||||
var3 = 7
|
||||
var1 + var2
|
||||
var2 + var3
|
||||
var1 + 5
|
||||
7 + var2
|
||||
7 + 7
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
Reference in new issue
Block a user