diff --git a/LAB4/Makefile b/LAB4/Makefile index 942a997..6f47e2d 100644 --- a/LAB4/Makefile +++ b/LAB4/Makefile @@ -23,4 +23,4 @@ memcheck: # executes our code to test it's functionality test: - ./lab4 \ No newline at end of file + ./lab4 < testinput.txt \ No newline at end of file diff --git a/LAB4/README.md b/LAB4/README.md new file mode 100644 index 0000000..3aa27a5 --- /dev/null +++ b/LAB4/README.md @@ -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 + +![symfull](tablefull.png) + +2. The variable already exists + +![alt](varexist.png) + +3. The variable doesn't exist + +![alt](notexist.png) + +4. Left and right assignment usage test + +![alt](leftright.png) \ No newline at end of file diff --git a/LAB4/bitch b/LAB4/bitch new file mode 100644 index 0000000..0de4c57 --- /dev/null +++ b/LAB4/bitch @@ -0,0 +1,7 @@ +int fuck; +int hoe; +fuck = 5 +hoe = 6 +fuck + hoe +hoe + 8 +9 + 9 diff --git a/LAB4/lab4docalc.l b/LAB4/lab4docalc.l index 7ba1403..80d62e5 100644 --- a/LAB4/lab4docalc.l +++ b/LAB4/lab4docalc.l @@ -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); diff --git a/LAB4/lab4docalc.y b/LAB4/lab4docalc.y index 5840463..5713ed8 100644 --- a/LAB4/lab4docalc.y +++ b/LAB4/lab4docalc.y @@ -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 expr %token INTEGER %token 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");} ; diff --git a/LAB4/leftright.png b/LAB4/leftright.png new file mode 100644 index 0000000..254d8e4 Binary files /dev/null and b/LAB4/leftright.png differ diff --git a/LAB4/notexist.png b/LAB4/notexist.png new file mode 100644 index 0000000..eff529e Binary files /dev/null and b/LAB4/notexist.png differ diff --git a/LAB4/symtable.c b/LAB4/symtable.c index be99723..7bbf209 100644 --- a/LAB4/symtable.c +++ b/LAB4/symtable.c @@ -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) diff --git a/LAB4/tablefull.png b/LAB4/tablefull.png new file mode 100644 index 0000000..9faa50d Binary files /dev/null and b/LAB4/tablefull.png differ diff --git a/LAB4/testinput.txt b/LAB4/testinput.txt new file mode 100644 index 0000000..0d20042 --- /dev/null +++ b/LAB4/testinput.txt @@ -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 + diff --git a/LAB4/varexist.png b/LAB4/varexist.png new file mode 100644 index 0000000..2f16857 Binary files /dev/null and b/LAB4/varexist.png differ