diff --git a/src/grammar.y b/src/grammar.y index 8a287d2..204898b 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -1,3 +1,4 @@ + /* Syntax Analyzer with Bison (3.8.2) */ /* The Translators - Spring 2025 */ @@ -203,17 +204,24 @@ definition: } if(type_of_param_type == TYPE_UNDEFINED){ CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); + // throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); } else { if(type_of_param_type == TYPE_FUNCTION_TYPE){ CreateEntry(cur, TYPE_FUNCTION_DECLARATION, parameter,NULL, getAdInfo(parameter)); + // throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); + } if(type_of_param_type == TYPE_ARRAY_TYPE){ CreateEntry(cur, TYPE_ARRAY, parameter,NULL, getAdInfo(parameter)); - } - if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ - CreateEntry(cur, TYPE_PRIMITIVE, parameter,NULL, getAdInfo(parameter)); - } + //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); + } + if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ + CreateEntry(cur, TYPE_PRIMITIVE, parameter,NULL, getAdInfo(parameter))==undefined; + // throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");} + + } + } } else { printdebug("record found"); for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ @@ -234,18 +242,29 @@ definition: if(type_of_param_type == TYPE_UNDEFINED){ printdebug("undefined type of parameter inside record"); CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); + //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); + } else { if(type_of_param_type == TYPE_FUNCTION_DECLARATION){ printdebug("function declaration of parameter inside record"); CreateEntry(cur, TYPE_FUNCTION_DECLARATION, getTypeEntry(entry),NULL, getAdInfo(entry)); + //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); + } if(type_of_param_type == TYPE_ARRAY){ printdebug("array type of parameter inside record"); CreateEntry(cur, TYPE_ARRAY, getTypeEntry(entry),NULL, getAdInfo(entry)); - } + //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); + } if(type_of_param_type == TYPE_PRIMITIVE){ printdebug("primitive type of parameter inside record"); CreateEntry(cur, TYPE_PRIMITIVE, getTypeEntry(entry),NULL, getAdInfo(entry)); + //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");} + } + if(type_of_param_type == TYPE_RECORD){ + printdebug("record type of parameter inside record"); + CreateEntry(cur, TYPE_RECORD, getTypeEntry(entry),NULL, getAdInfo(entry)); + //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");} } /*printdebug("creating entry of type %s for function", getType(entry)); CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ @@ -455,22 +474,30 @@ declaration: else if(d == TYPE_FUNCTION_TYPE) { printdebug("invalid (function) type passed in declaration list in dblock", @2.first_line, @2.first_column); d = TYPE_FUNCTION_DECLARATION; - CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + if(CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)) == undefined){ + throw_error(ERROR_TYPE, "Duplicate defination of function in declaration list"); + } } else if(d == TYPE_ARRAY_TYPE){ printdebug("array variable at line %d and column %d", @2.first_line, @2.first_column); d = TYPE_ARRAY; - CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + if(CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)) == undefined){ + throw_error(ERROR_TYPE, "Duplicate defination of array in declaration list"); + } } else if(d == TYPE_RECORD_TYPE){ printdebug("record variable at line %d and column %d", @2.first_line, @2.first_column); d = TYPE_RECORD; - CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + if(CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1))== undefined){ + throw_error(ERROR_TYPE, "Duplicate defination of record in declaration list"); + } } else if(d == TYPE_PRIMITIVE_TYPE){ printdebug("primitive variable at line %d and column %d", @2.first_line, @2.first_column); d = TYPE_PRIMITIVE; - CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + if(CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)) == undefined){ + throw_error(ERROR_TYPE, "Duplicate defination of primitive in declaration list"); + } }else { throw_error(ERROR_TYPE, "%s is being defined with an undefined type", $3); CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); diff --git a/src/symbol_table.c b/src/symbol_table.c index ab2cd88..a7c05be 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -956,6 +956,11 @@ TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, return NULL; } */ + if((id != NULL) && table_lookup(cur,id)!=undefined){ + printdebug("This name is already defined in the current scope"); + //throw_error(ERROR_TYPE, "Already defined."); + return undefined; + } if (typeOf == NULL) { printdebug("Passing an NULL Type Node to Create Entry"); return undefined; @@ -982,9 +987,14 @@ TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, printdebug("[CreateEntry] Adding %s to the symbol table", id); return newEntry; } else { - TableNode *oldEntry = table->entries; - table->entries = newEntry; - newEntry->next = oldEntry; + TableNode*oldEntry = table->entries; + while (oldEntry->next != NULL) { + oldEntry = oldEntry->next; + } + oldEntry->next = newEntry; + newEntry->next = NULL; + //table->entries = newEntry; + //newEntry->next = oldEntry; printdebug("[CreateEntry] Adding %s to the symbol table", id); return newEntry; } diff --git a/tests/carl/NoErrors/functionValue.alpha b/tests/carl/NoErrors/functionValue.alpha index 8ad0e6c..c1134ce 100644 --- a/tests/carl/NoErrors/functionValue.alpha +++ b/tests/carl/NoErrors/functionValue.alpha @@ -28,6 +28,7 @@ function c: string2int_2_integer function d: iic2b d(x,y,z) := { + [string: s;integer: s] return (x < y & z < 'm'); }