wrote up symbol table structure. Have to correct code and then add type checking grammar rules
This commit is contained in:
@ -75,7 +75,7 @@ fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number,tok, yytext);
|
|||||||
}
|
}
|
||||||
int run(FILE *alpha) {
|
int run(FILE *alpha) {
|
||||||
int token;
|
int token;
|
||||||
top = cur = CreateScope(NULL, 1, 1);
|
top = cur = init(CreateScope(NULL, 1, 1));
|
||||||
|
|
||||||
// If file is not found
|
// If file is not found
|
||||||
if (alpha == NULL) {
|
if (alpha == NULL) {
|
||||||
|
@ -44,8 +44,9 @@ typedef struct{
|
|||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
int numofdimensions;
|
int numofdimensions;
|
||||||
//the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define a multidimensional array of that size
|
//the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define >
|
||||||
int* arr;
|
int* sizesofdimensions;
|
||||||
|
TableNode* typeofarray;
|
||||||
}array_info;
|
}array_info;
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
@ -73,6 +74,53 @@ typedef union {
|
|||||||
FunTypeAdInfo* func_type_info;
|
FunTypeAdInfo* func_type_info;
|
||||||
}AdInfo;
|
}AdInfo;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
primitive_info* CreatePrimitiveInfo(size){
|
||||||
|
primitive_info* prim = (primitive_info*)malloc(sizeof(primitive_info));
|
||||||
|
prim.size=size;
|
||||||
|
return prim;
|
||||||
|
}
|
||||||
|
|
||||||
|
string_info* CreateStringInfo(int length, char* loc){
|
||||||
|
string_info* stringy = (string_info*)malloc(sizeof(string_info));
|
||||||
|
stringy.length=length;
|
||||||
|
char* location = loc;
|
||||||
|
return stringy;
|
||||||
|
}
|
||||||
|
|
||||||
|
array_info* CreateArrayInfo(int dim, int* sizes, TableNode* type){
|
||||||
|
array_info* arr = (array_info*)malloc(sizeof(array_info));
|
||||||
|
arr.numofdimensions=dim;
|
||||||
|
arr.typeofarray=type;
|
||||||
|
int* dimensionsizes = loc;
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
record_info* CreateRecordInfo(int length, TableNode* typesarray){
|
||||||
|
record_info* reccy = (record_info*)malloc(sizeof(record_info));
|
||||||
|
reccy.numofelements=length;
|
||||||
|
reccy->listoftypes = typesarray;
|
||||||
|
return reccy;
|
||||||
|
}
|
||||||
|
|
||||||
|
function_declaration_info* CreateFunctionDeclarationInfo(int line, bool asorregular){
|
||||||
|
function_declaration_info* fundec = (function_declaration_info*)malloc(sizeof(function_declaration_info));
|
||||||
|
fundec.startlinenumber=line;
|
||||||
|
fundec.regularoras = asorregular;
|
||||||
|
return fundec;
|
||||||
|
}
|
||||||
|
|
||||||
|
function_type_info* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){
|
||||||
|
function_type_info* funtype = (function_type_info*)malloc(sizeof(function_type_info));
|
||||||
|
funtype->parameter=parameter;
|
||||||
|
funtype->returntype = returntype;
|
||||||
|
return funtype;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
|
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
|
||||||
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
|
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
|
||||||
table->Line_Number = Line;
|
table->Line_Number = Line;
|
||||||
@ -105,28 +153,68 @@ SymbolTable* init(SymbolTable* start){
|
|||||||
printf("Cannot initialize a scope that is not the parent scope\n");
|
printf("Cannot initialize a scope that is not the parent scope\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
TableNode* table1 = (TableNode*)malloc(sizeof(SymbolTable));
|
TableNode* integ = (TableNode*)malloc(sizeof(SymbolTable));
|
||||||
table->Line_Number = Line;
|
TableNode* addr = (TableNode*)malloc(sizeof(SymbolTable));
|
||||||
table->Column_Number = Column;
|
TableNode* chara = (TableNode*)malloc(sizeof(SymbolTable));
|
||||||
table->Parent_Scope = ParentScope;
|
TableNode* stri = (TableNode*)malloc(sizeof(SymbolTable));
|
||||||
table->Children_Scope = NULL;
|
TableNode* boo = (TableNode*)malloc(sizeof(SymbolTable));
|
||||||
table->entries = NULL;
|
TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable));
|
||||||
|
start->entries = integ;
|
||||||
|
integ->next = addr;
|
||||||
|
addr->next = chara;
|
||||||
|
chara->next = stri;
|
||||||
|
stri->next = boo;
|
||||||
|
boo->next = arr;
|
||||||
|
integ->theName= "integer";
|
||||||
|
addr->theName= "address";
|
||||||
|
chara->theName= "character";
|
||||||
|
boo->theName= "Boolean";
|
||||||
|
stri->theName= "string";
|
||||||
|
arr->theName= "array"
|
||||||
|
|
||||||
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
|
//root TableNode that all are pointing to but not in table
|
||||||
|
TableNode* prime = (TableNode*)malloc(sizeof(SymbolTable));
|
||||||
|
prime->theName= "primitive";
|
||||||
|
prime->theType=NULL;
|
||||||
|
prime->additionalinfo = NULL;
|
||||||
|
prime->next = NULL;
|
||||||
|
|
||||||
|
TableNode* striprim = (TableNode*)malloc(sizeof(SymbolTable));
|
||||||
|
prime->theName= "1->character";
|
||||||
|
prime->theType=NULL;
|
||||||
|
prime->additionalinfo = NULL;
|
||||||
|
prime->next = NULL;
|
||||||
|
|
||||||
|
integ->theType=prime;
|
||||||
|
addr->theType=prime;
|
||||||
|
chara->theType=prime;
|
||||||
|
stri->theType=striprim;
|
||||||
|
boo->theType=prime;
|
||||||
|
arr->theType=prime;
|
||||||
|
|
||||||
|
start->Line_Number = 1;
|
||||||
|
start->Column_Number = 1;
|
||||||
|
start->Parent_Scope = NULL;
|
||||||
|
start->Children_Scope = NULL;
|
||||||
|
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, AdInfo* ad) {
|
||||||
|
|
||||||
if(table ==NULL){
|
if(table ==NULL){
|
||||||
printf("Null reference to table");
|
printf("Null reference to table");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
/*TableNode* topDef = (table_lookup(getAncestor(table),typeOf));
|
TableNode* topDef = (table_lookup(getAncestor(table),typeOf));
|
||||||
if(topDef == NULL){
|
if(topDef == NULL){
|
||||||
printf("This type is not defined at the top level\n");
|
printf("This type is not defined at the top level\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}*/
|
}
|
||||||
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
|
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
|
||||||
//newEntry->theType = topDef->theName;
|
newEntry->theType = topDef;
|
||||||
newEntry->theType=typeOf;
|
|
||||||
newEntry->theName = id;
|
newEntry->theName = id;
|
||||||
|
newEntry->additionalinfo = ad;
|
||||||
if (table->entries == NULL) {
|
if (table->entries == NULL) {
|
||||||
table->entries = newEntry;
|
table->entries = newEntry;
|
||||||
return newEntry;
|
return newEntry;
|
||||||
@ -137,7 +225,7 @@ if(topDef == NULL){
|
|||||||
return newEntry;
|
return newEntry;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
//we use false for type defs and true for functions for parameter of typeOf
|
//we use false for type defs and true for functions for parameter of typeOf
|
||||||
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
|
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
|
||||||
if(table ==NULL || table->Parent_Scope != NULL){
|
if(table ==NULL || table->Parent_Scope != NULL){
|
||||||
@ -171,7 +259,7 @@ if(table_lookup(table,id) != NULL){
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
TableNode* table_lookup(SymbolTable* table, char* x) {
|
TableNode* table_lookup(SymbolTable* table, char* x) {
|
||||||
TableNode* entrie = table->entries;
|
TableNode* entrie = table->entries;
|
||||||
for (; entrie != NULL; entrie = entrie->next) {
|
for (; entrie != NULL; entrie = entrie->next) {
|
||||||
@ -217,11 +305,11 @@ void print_symbol_table(SymbolTable* table, FILE* file_ptr) {
|
|||||||
for (; entrie != NULL; entrie = entrie->next) {
|
for (; entrie != NULL; entrie = entrie->next) {
|
||||||
if (parant_scope == 0) {
|
if (parant_scope == 0) {
|
||||||
fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n",
|
fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n",
|
||||||
entrie->theName, current_scope, entrie->theType,
|
entrie->theName, current_scope, entrie->theType->theName,
|
||||||
"Extra annotation");
|
"Extra annotation");
|
||||||
} else {
|
} else {
|
||||||
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName,
|
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName,
|
||||||
current_scope, parant_scope, entrie->theType, "Extra annotation");
|
current_scope, parant_scope, entrie->theType->theName, "Extra annotation");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (table->Children_Scope != NULL) {
|
if (table->Children_Scope != NULL) {
|
||||||
@ -254,7 +342,7 @@ SymbolTable* getFirstChild(ListOfTable* lt) { return lt->table; }
|
|||||||
ListOfTable* getRestOfChildren(ListOfTable* lt) { return lt->next; }
|
ListOfTable* getRestOfChildren(ListOfTable* lt) { return lt->next; }
|
||||||
TableNode* getFirstEntry(SymbolTable* st) { return st->entries; }
|
TableNode* getFirstEntry(SymbolTable* st) { return st->entries; }
|
||||||
TableNode* getNextEntry(TableNode* tn) { return tn->next; }
|
TableNode* getNextEntry(TableNode* tn) { return tn->next; }
|
||||||
char* getType(TableNode* tn) { return tn->theType; }
|
char* getType(TableNode* tn) { return tn->theType->theName; }
|
||||||
char* getName(TableNode* tn) { return tn->theName; }
|
char* getName(TableNode* tn) { return tn->theName; }
|
||||||
int getLine(SymbolTable* st) { return st->Line_Number; }
|
int getLine(SymbolTable* st) { return st->Line_Number; }
|
||||||
int getColumn(SymbolTable* st) { return st->Column_Number; }
|
int getColumn(SymbolTable* st) { return st->Column_Number; }
|
||||||
|
@ -15,6 +15,8 @@ typedef struct{
|
|||||||
typedef struct{
|
typedef struct{
|
||||||
int numofdimensions;
|
int numofdimensions;
|
||||||
//the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define > int* arr;
|
//the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define > int* arr;
|
||||||
|
int* sizesofdimensions;
|
||||||
|
TableNode* typeofarray;
|
||||||
}array_info;
|
}array_info;
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
|
Reference in New Issue
Block a user