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) */
/* The Translators - Spring 2025 */
@ -245,37 +246,10 @@ simple_statement:
;
assignable:
ID {TableNode *node = table_lookup(cur, $<words>1);
printf("%s\n", $1);
if (node == NULL) {
printf("could not find %s in current scope\n", $<words>1);
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";}
}
ID {$$ = $1; }
| assignable ablock {$$ = getName(getReturn(look_up(cur, $1)));}
| assignable rec_op ID {if(undefined != (tn = table_lookup(getRecList(look_up(cur, $1)), $3)))
{$$ = getType(tn);}}
;
rec_op :

View File

@ -44,7 +44,8 @@ typedef enum {
// size
TYPE_PRIMITIVE = 6,
//likely NULL
TYPE_ALL_ELSE = 7
TYPE_ALL_ELSE = 7,
TYPE_UNDEFINED = 8
} types;
@ -214,7 +215,13 @@ int getRecSize(SymbolTable* tn){
}
// 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
// multiple inputs Below function also has the line number where the function is
// first defined
@ -488,7 +495,10 @@ int getAdInfoType(TableNode* tn){
if(strcmp(getType(tn),getName(arrayprim))==0){
return TYPE_ARRAY;
}
else{
if(strcmp(getType(tn),getName(undefined))==0){
return TYPE_UNDEFINED;
}
else{
return TYPE_FUNCTION_DECLARATION;
}
}
@ -637,6 +647,7 @@ TableNode *look_up(SymbolTable *table, char *x) {
void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
return;
if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME",
"SCOPE", "PARENT", "TYPE", "Extra annotation");