verifying grammar

This commit is contained in:
Scarlett
2025-04-01 17:02:24 -04:00
parent fac92f62f7
commit 5bd68ddb85
5 changed files with 52 additions and 43 deletions

View File

@ -267,16 +267,30 @@ simple_statement:
| RETURN expression
;
assignable:
ID {$$ = getType(look_up(cur,$1));}
| assignable ablock {$$ = getName(getReturn(look_up(cur, $1)));} //add array case here
| assignable rec_op ID {if(undefined != table_lookup(getRecList(look_up(cur, $1)), $3)){
{$$ = getName(table_lookup(getRecList(look_up(cur, $1)), $3));}};}
;
rec_op :
DOT
/////////// VERIFIED UP UNTIL THIS POINT
// assignable needs more code- specifically for arrays, records and ablock checks
ablock:
L_PAREN argument_list {$<integ>$ = $<integ>2;} R_PAREN
;
argument_list:
expression COMMA argument_list {$<integ>$ = $<integ>3 + 1; printdebug("[ARGUMENT_LIST] argument list is %d", $<integ>$);}
| expression {$<integ>$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $<integ>$);}
;
// will ALWAYS be a TYPE
expression:
constant {printdebug("constant expression");} {$$ = $<words>1;}
@ -341,8 +355,8 @@ expression:
| expression EQUAL_TO expression {printdebug("equals check expression");
if(strcmp($1,$3)==0){$$=strdup("Boolean");}
else if((strcmp($1,"array")==0||strcmp($1,"record")==0||
strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");}
//else if((strcmp($1,"array")==0||strcmp($1,"record")==0||
// strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");}
else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s",
@2.first_line,@2.first_column,$1,$3);$$=strdup("undefined");}}
@ -353,17 +367,16 @@ expression:
| memOp assignable {$$ = strdup("address");}
;
ablock:
L_PAREN argument_list {$<integ>$ = $<integ>2;} R_PAREN
// prolly right, check back with me later
// add array case
// include type check for ablock in arrays - ablock is always the int of the elements in array/rec
assignable:
ID {$$ = getType(look_up(cur,$1)); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);}
| assignable ablock {$$ = getName(getReturn(table_lookup(getAncestor(cur), $1))); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1);} // add array case
| assignable rec_op ID {if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3)){
{$$ = getName(table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3));}}; printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", $$, $1);}
;
argument_list:
expression COMMA argument_list {$<integ>$ = $<integ>3 + 1;}
| expression {$<integ>$ = 1;}
;
memOp:
RESERVE {printdebug("reserve expression");}
| RELEASE {printdebug("release expression");}
@ -371,12 +384,12 @@ memOp:
constant:
C_STRING {$$ = $<words>1;}
| C_INTEGER {$$ = "integer";}
| C_NULL {$$ = $<words>1;}
| C_CHARACTER {$$ = $<words>1;}
| C_TRUE {$$ = $<words>1;}
| C_FALSE {$$ = $<words>1;}
C_STRING {$$ = $<words>1; printdebug("string of C_STRING in constant is %s",$<words>1);}
| C_INTEGER {$$ = "integer"; printdebug("string of C_INTEGER in constant is integer");}
| C_NULL {$$ = $<words>1; printdebug("string of C_NULL in constant is %s",$<words>1);}
| C_CHARACTER {$$ = $<words>1; printdebug("string of C_CHARACTER in constant is %s",$<words>1);}
| C_TRUE {$$ = $<words>1; printdebug("string of C_TRUE in constant is %s",$<words>1);}
| C_FALSE {$$ = $<words>1; printdebug("string of C_FALSE in constant is %s",$<words>1);}
;
types:

View File

@ -223,13 +223,4 @@ int is_alpha_file(char *alpha, int file_len) {
return -1; // not alpha file
}
return 0; // is alpha file
}
void enter_scope(int line, int column) { cur = CreateScope(cur, line, column); }
void exit_scope() {
if (cur->Parent_Scope == NULL) {
printf("Can't close top");
return;
}
cur = cur->Parent_Scope;
}

View File

@ -40,8 +40,6 @@ SymbolTable *cur;
// 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;

View File

@ -512,7 +512,7 @@ SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column) {
SymbolTable *init(SymbolTable *start) {
if (start->Parent_Scope != NULL) {
printdebug(
"Cannot initialize a scope that is not the parent scope");
"%s[FATAL] Cannot initialize a scope that is not the parent scope", COLOR_RED);
return NULL;
}
integ = (TableNode *)calloc(1, sizeof(TableNode));
@ -979,6 +979,9 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
printdebug("%s[WARNING] passed in a non-top level scope to "
"print_symbol_table",
COLOR_ORANGE);
printdebug("%sParent of's: line %d, column %d",
COLOR_ORANGE, table->Parent_Scope->Line_Number, table->Parent_Scope->Column_Number);
return;
}
if (table->Parent_Scope == NULL) {
@ -1100,13 +1103,13 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
fprintf(file_ptr,
"%-17s: %06d : :%-21s: %-28s\n",
entrie->theName, current_scope,
getType(entrie), "Function");
getType(entrie), "User Defined");
} else {
fprintf(file_ptr,
"%-17s: %06d : %06d : %-21s: %-28s\n",
entrie->theName, current_scope,
parant_scope, getType(entrie),
"Function");
"User Defined");
}
}
if (getAdInfoType(entrie) == TYPE_UNDEFINED) {
@ -1145,6 +1148,12 @@ SymbolTable *getAncestor(SymbolTable *table) {
}
if (table->Parent_Scope == NULL) {
// if table has no parent, return itself
printdebug("already at top scope!");
if (table == cur) {
printdebug("passed in the current scope");
} else {
printdebug("passed in a different scope");
}
return table;
} else {
// call function recursively to grab ancestor

View File

@ -7,9 +7,9 @@ You should #include this file at the start of your alpha file.
Some useful types are defined below.
*)
type string: 1 -> character
type BooleanXBoolean: [Boolean: x, y]
type characterXcharacter: [character: x, y]
type integerXinteger: [integer: x, y]
type BooleanXBoolean: [Boolean: x; Boolean: y]
type characterXcharacter: [character: x; character: y]
type integerXinteger: [integer: x; integer: y]
type Boolean2Boolean: Boolean -> Boolean
type integer2integer: integer -> integer
@ -25,6 +25,4 @@ type address2integer: address -> integer
external function printInteger: integer2integer
external function printCharacter: character2integer
external function printBoolean: Boolean2integer
external function reserve: integer2address
external function release: address2integer
function entry: string2integer