working on tabel

This commit is contained in:
Partho Bhattacharya
2025-03-28 14:49:17 -04:00
parent 37dedf1818
commit cca01eb0b5
2 changed files with 19 additions and 34 deletions

View File

@ -1,3 +1,4 @@
/* Syntax Analyzer with Bison (3.8.2) */ /* Syntax Analyzer with Bison (3.8.2) */
/* The Translators - Spring 2025 */ /* The Translators - Spring 2025 */
@ -245,37 +246,10 @@ simple_statement:
; ;
assignable: assignable:
ID {TableNode *node = table_lookup(cur, $<words>1); ID {$$ = $1; }
printf("%s\n", $1); | assignable ablock {$$ = getName(getReturn(look_up(cur, $1)));}
if (node == NULL) { | assignable rec_op ID {if(undefined != (tn = table_lookup(getRecList(look_up(cur, $1)), $3)))
printf("could not find %s in current scope\n", $<words>1); {$$ = getType(tn);}}
node = table_lookup(getAncestor(cur), $<words>1);
if (node != NULL && getAdInfoType(node) == TYPE_FUNCTION_DECLARATION) {
if (getAsKeyword(node)) {
node = table_lookup(getAncestor(cur), getType(node));
$<integ>$ = getRecLength(getParameter(node));
} else {
$<integ>$ = 1;
}
}
else {
printf("assignable not defined as correct type at line %d, column %d\n", @1.first_line, @1.first_column);
}
} else if (getAdInfoType(node) == TYPE_ARRAY) {
$<integ>$ = getNumArrDim(node);
} else {
$<words>$ = getName(node);
}
}
| assignable ablock {if ($<integ>1 != $<integ>2) {
printf("invalid number of expressions in ablock at line %d, column %d\n", @1.first_line, @1.first_column);
}
$$ = getName(getReturn(look_up(cur, $1)));
}
| assignable rec_op ID {TableNode * tn; printf("this is assignable.id %s\n", getType(look_up(cur, $1)));
if(NULL != (tn = table_lookup(getRecList(look_up(cur, $1)), $3)))
{$$ = "test";}
}
; ;
rec_op : rec_op :

View File

@ -44,7 +44,8 @@ typedef enum {
// size // size
TYPE_PRIMITIVE = 6, TYPE_PRIMITIVE = 6,
//likely NULL //likely NULL
TYPE_ALL_ELSE = 7 TYPE_ALL_ELSE = 7,
TYPE_UNDEFINED = 8
} types; } types;
@ -214,7 +215,13 @@ int getRecSize(SymbolTable* tn){
} }
// below function takes a bool to see if parameter should be decomposed or not // below function takes a bool to see if parameter should be decomposed or not
// note that functions only take one input and have one output assignable:
ID {$$ = $1; }
| assignable ablock {$$ = getName(getReturn(look_up(cur, $1)));}
| assignable rec_op ID {TableNode * tn; if(NULL != (tn = table_lookup(getRecList(look_up(cur, $1)), $3)))
{$$ = getType(tn);}
}
;// note that functions only take one input and have one output
// using "as" the input record can be decomposed to give the illusion of // using "as" the input record can be decomposed to give the illusion of
// multiple inputs Below function also has the line number where the function is // multiple inputs Below function also has the line number where the function is
// first defined // first defined
@ -488,6 +495,9 @@ int getAdInfoType(TableNode* tn){
if(strcmp(getType(tn),getName(arrayprim))==0){ if(strcmp(getType(tn),getName(arrayprim))==0){
return TYPE_ARRAY; return TYPE_ARRAY;
} }
if(strcmp(getType(tn),getName(undefined))==0){
return TYPE_UNDEFINED;
}
else{ else{
return TYPE_FUNCTION_DECLARATION; return TYPE_FUNCTION_DECLARATION;
} }
@ -637,6 +647,7 @@ TableNode *look_up(SymbolTable *table, char *x) {
void print_symbol_table(SymbolTable *table, FILE *file_ptr) { void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
return;
if (table->Parent_Scope == NULL) { if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME",
"SCOPE", "PARENT", "TYPE", "Extra annotation"); "SCOPE", "PARENT", "TYPE", "Extra annotation");