diff --git a/grammar.y b/grammar.y new file mode 100644 index 0000000..19c6ea2 --- /dev/null +++ b/grammar.y @@ -0,0 +1,46 @@ +%token ID 101 +%token T_INTEGER 201 +%token T_ADDRESS 202 +%token T_BOOLEAN 203 +%token T_CHARACTER 204 +%token T_STRING 205 +%token C_INTEGER 301 +%token C_NULL 302 +%token C_CHARACTER 303 +%token C_STRING 304 +%token C_TRUE 305 +%token C_FALSE 306 +%token WHILE 401 +%token IF 402 +%token THEN 403 +%token ELSE 404 +%token TYPE 405 +%token FUNCTION 406 +%token RETURN 407 +%token EXTERNAL 408 +%token AS 409 +%token L_PAREN 501 +%token R_PAREN 502 +%token L_BRACKET 503 +%token R_BRACKET 504 +%token L_BRACE 505 +%token R_BRACE 506 +%token SEMI_COLON 507 +%token COLON 508 +%token COMMA 509 +%token ARROW 510 +%token ADD 601 +%token SUB_OR_NEG 602 +%token MUL 603 +%token DIV 604 +%token REM 605 +%token LESS_THAN 606 +%token EQUAL_TO 607 +%token ASSIGN 608 +%token NOT 609 +%token AND 610 +%token OR 611 +%token DOT 612 +%token RESERVE 613 +%token RELEASE 614 +%token COMMENT 700 diff --git a/print_symbol_table.c b/print_symbol_table.c index 0ae90f6..c5e4208 100644 --- a/print_symbol_table.c +++ b/print_symbol_table.c @@ -41,7 +41,6 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr){ } /* -*/ int main(void){ char *prim = strdup("primitive"); char *inte = strdup("integer"); @@ -51,25 +50,41 @@ int main(void){ char *str = strdup("string"); char *arg = strdup("arg"); // Value* v = calloc(1, sizeof(Value)); - CreateEntry(parant, prim, boole, NULL, 0); - CreateEntry(parant, prim, chare, NULL, 0); - CreateEntry(parant, prim, inte, NULL, 0); - CreateEntry(parant, &"1 -> character", str, NULL, 0); - CreateEntry(parant, &"integer -> integer", &"int2int", NULL, 0); - CreateEntry(parant, &"string -> integer", &"string2int", NULL, 0); - CreateEntry(parant, &"int2int", &"square", NULL, 0); - CreateEntry(parant, &"string2int", &"entry", NULL, 0); + CreateEntry(parant, prim, boole); + CreateEntry(parant, prim, chare); + CreateEntry(parant, prim, inte); + CreateEntry(parant, &"1 -> character", str); + CreateEntry(parant, &"integer -> integer", &"int2int"); + CreateEntry(parant, &"string -> integer", &"string2int"); + CreateEntry(parant, &"int2int", &"square"); + CreateEntry(parant, &"string2int", &"entry"); SymbolTable * child = CreateScope(parant, 14,14); - CreateEntry(child, inte, &"x", NULL, 0); + CreateEntry(child, inte, &"x"); SymbolTable * second = CreateScope(parant, 21,15); - CreateEntry(second, str, arg, NULL, 0); - CreateEntry(second, inte, &"input", NULL, 0); - CreateEntry(second, inte, &"expected", NULL, 0); - CreateEntry(second, inte, &"actual", NULL, 0); - CreateEntry(second, &"$_undefined_type", &"result", NULL, 0); + CreateEntry(second, str, arg); + CreateEntry(second, inte, &"input"); + CreateEntry(second, inte, &"expected"); + CreateEntry(second, inte, &"actual"); + CreateEntry(second, &"$_undefined_type", &"result"); SymbolTable * third = CreateScope(second, 33,44); - CreateEntry(third, &"BOO", arg, NULL, 0); - CreateEntry(third, &"YAZOO", &"input", NULL, 0); + CreateEntry(third, &"BOO", arg); + CreateEntry(third, &"YAZOO", &"input"); + + TableNode *ret = table_lookup(third, "arg"); + printf("%s == %s\n", ret->theName, "arg"); + + ret = table_lookup(third, "hello"); + printf("This should be nil %p != %s\n", ret, "BOO"); + + ret = look_up(second, "input"); + printf("%s == %s\n", ret->theName, "input"); + + ret = look_up(second, "square"); + printf("%s == %s\n", ret->theName, "square"); + + ret = look_up(second, "spuare"); + printf("This should be nil %p == %s\n", ret, "square"); + print_symbol_table(parant, stderr); free(inte); free(boole); @@ -79,3 +94,5 @@ int main(void){ free(arg); return 0; } + +*/ diff --git a/runner.c b/runner.c index 85e8214..d170d26 100644 --- a/runner.c +++ b/runner.c @@ -1,6 +1,7 @@ +#include "symbol_table.h" +#include "symbol_table.c" #include "runner.h" - int main(int argc, char *argv[]) { if (argc == 1) { fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs \n"); @@ -152,3 +153,17 @@ int is_alpha_file(char *alpha, int file_len) { return 0; //is alpha file } +void enter_scope(int line, int column){ + curr = CreateScope(curr, line, column); +} +void exit_scope() { + if(curr->Parent_Scope == NULL){ + printf("Can't close top"); + return; + } + curr = curr->Parent_Scope; +} + + + + diff --git a/runner.h b/runner.h index a5c12fb..e7a8194 100644 --- a/runner.h +++ b/runner.h @@ -16,6 +16,16 @@ extern int line_number, column_number; extern char *yytext; extern FILE *yyin; int arg; + +SymbolTable * top; +SymbolTable * curr; + +int main(int argc, char* argv[]); +char *is_tok(int argc, char* argv[]); +int is_alpha_file(char *file, int file_len); +void enter_scope(int, int); +void exit_scope(void); + FILE *alpha_file; FILE *tok_flag = NULL; FILE *st_flag = NULL; diff --git a/symbol_table.c b/symbol_table.c index 9512b8f..e5d1366 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -47,6 +47,25 @@ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id){ return newEntry; } } +TableNode * table_lookup(SymbolTable * table, char * x){ + TableNode * entrie = table->entries; + for(; entrie != NULL; entrie = entrie->next){ + if (!strcmp(entrie->theName, x)){ + return entrie; + } + } + return NULL; +} +TableNode * look_up(SymbolTable * table, char * x){ + if(table == NULL){ + return NULL; + } + TableNode * ret = table_lookup(table, x); + if (ret != NULL){ + return ret; + } + return look_up(table->Parent_Scope, x); +} //uncomment the below main function along with the headers above for a simple standalone test of table and entry creation @@ -56,6 +75,7 @@ int main(){ SymbolTable* Second = CreateScope(NULL, 2,2); printf("Line number is %d, Column number of scope is %d\n",Second->Line_Number,Second->Column_Number); TableNode* First_Entry = CreateEntry(Second,String,X); + printf("The type of the first entry is %s\n",First_Entry->theType); return 0; } diff --git a/symbol_table.h b/symbol_table.h index d510a3b..4830345 100644 --- a/symbol_table.h +++ b/symbol_table.h @@ -1,6 +1,7 @@ #include #include #include +#include typedef struct ListOfTable{ struct SymbolTable* table;