// Declaration Action
declaration: type IDENTIFIER SEMICOLON {
printf("Declared %s of type %s\n", 2,1);
// Allocate memory for the new variable
if (strcmp(1, "int") == 0) {
int *var = malloc(sizeof(int));
// Save the pointer in the symbol table
SymbolTable_add(2, var);
} else if (strcmp(1, "float") == 0) {
float *var = malloc(sizeof(float));
// Save the pointer in the symbol table
SymbolTable_add(2, var);
} else if (strcmp(1, "bool") == 0) {
bool *var = malloc(sizeof(bool));
// Save the pointer in the symbol table
SymbolTable_add(2, var);
} else if (strcmp(1, "string") == 0) {
char *var = malloc(sizeof(char*));
// Save the pointer in the symbol table
SymbolTable_add(2, var);
}
} ;
Assistant
As an AI language model, I cannot evaluate the correctness of a grammar although I can try to check the basic syntax. The given grammar file seems to have the basic syntax and structure of a Bison parser, but it is missing some important aspects such as actions for non-terminals that have semantic meaning, and support for associativity and precedence of operators. Additionally, the actions for some non-terminals are incomplete or missing altogether, which would prevent the parser from generating correct output. Without complete actions, it is not possible to evaluate expressions or execute statements correctly.
User
Assistant
This updated grammar file has action code for non-terminals and support for associativity and precedence of operators. It also includes the helper functions and the necessary code for the symbol table. The actions for the declaration, loop, condition, and comparator non-terminals have been updated to call the corresponding helper functions. However, the expression non-terminal still needs its evaluation to be implemented. Lastly, there is an issue with the SymbolTable_lookup function where it does not return a value if the variable is not found.
// Loop Action
loop: FOR '(' type condition ')' statement_list {
printf("Loop: for (%s) {\n", 3);//ExecutetheloopbodyexecuteLoop(3);
}
| WHILE '(' type condition ')' statement_list {
printf("Loop: while (%s) {\n", 3);//ExecutetheloopbodyexecuteLoop(3);
} ;
// Condition Action
condition: expression comparator expression {
// Evaluate the expression
bool result = evaluateCondition(1,2, 3);
if (result) {
// Execute the statement list
executeStatements(statement_list);
} else {
printf("Condition: %s %s %s\n", 1->string, 2−>type,3->string);
}
};
// Expression Action
expression: IDENTIFIER {
printf("Expression: %s\n", 1−>string);//Lookupthevariableinthesymboltablevoid∗var=SymbolTablelookup(1->string);
// Return the result of the expression
return var;
}
| NUMBER {
printf("Expression: %d\n", 1−>number);//Returntheresultoft1->number;
}
| STRING {
printf("Expression: %s\n", 1−>string);//Returntheresultreturn1->string;
}
| expression '+' expression {
printf("Addition: %d + %d\n", 1,3);
// Add the two operands
return 1+3;
}
| expression '-' expression {
printf("Subtraction: %d - %d\n", 1,3);
// Subtract the second operand from the first
return 1−3;
}
| expression '*' expression {
printf("Multiplication: %d * %d\n", 1,3);
// Multiply the two operands
return 1∗3;
}
| expression '/' expression {
printf("Division: %d / %d\n", 1,3);
// Check for division by zero
if (3==0)f1 / $3;
}
// Comparator Action
comparator: OR {printf("Comparator: ||\n");}
| AND {printf("Comparator: &&\n");}
| NOT {printf("Comparator: !\n");}
| EQUALS {printf("Comparator: ==\n");}
| NOT_EQUALS {printf("Comparator: !=\n");}
| GREATER_THAN {printf("Comparator: >\n");}
// Declaration Action
declaration: type IDENTIFIER SEMICOLON {
printf("Declared %s of type %s\n", 2,1);
// Allocate memory for the new variable
if (strcmp(1, "int") == 0) {
int *var = malloc(sizeof(int));
// Save the pointer in the symbol table
SymbolTable_add(2, var);
} else if (strcmp(1, "float") == 0) {
float *var = malloc(sizeof(float));
// Save the pointer in the symbol table
SymbolTable_add(2, var);
} else if (strcmp(1, "bool") == 0) {
bool *var = malloc(sizeof(bool));
// Save the pointer in the symbol table
SymbolTable_add(2, var);
} else if (strcmp(1, "string") == 0) {
char *var = malloc(sizeof(char*));
// Save the pointer in the symbol table
SymbolTable_add(2, var);
}
} ;
// Loop Action
loop: FOR '(' type condition ')' statement_list {
printf("Loop: for (%s) {\n", 3);//ExecutetheloopbodyexecuteLoop(3);
}
| WHILE '(' type condition ')' statement_list {
printf("Loop: while (%s) {\n", 3);
} ;
// Condition Action
condition: expression comparator expression {
// Evaluate the expression
bool result = evaluateCondition(1,2, 3);
if (result) {
// Execute the statement list
executeStatements(statement_list);
} else {
printf("Condition: %s %s %s\n", 1->string, 2−>type,3->string);
}
};
// Expression Action
expression: IDENTIFIER {
printf("Expression: %s\n", 1−>string);//Look1->string);
// Return the result of the expression
return var;
}
| NUMBER {
printf("Expression: %d\n", 1->number;
}
| STRING {
printf("Expression: %s\n", 1->string;
}
| expression '+' expression {
printf("Addition: %d + %d\n", 3);
// Add the two operands
return 3;
}
| expression '-' expression {
printf("Subtraction: %d - %d\n", 3);
// Subtract the second operand from the first
return 3;
}
| expression '*' expression {
printf("Multiplication: %d * %d\n", 3);
// Multiply the two operands
return 3;
}
| expression '/' expression {
printf("Division: %d / %d\n", 3);
// Check for division by zero
if (1 / $3;
}
// Comparator Action
comparator: OR {printf("Comparator: ||\n");}
| AND {printf("Comparator: &&\n");}
| NOT {printf("Comparator: !\n");}
| EQUALS {printf("Comparator: ==\n");}
| NOT_EQUALS {printf("Comparator: !=\n");}
| GREATER_THAN {printf("Comparator: >\n");}
// Helper Functions
void executeStatements(StatementList *list) {
// Execute each statement in the list
for (int i=0; i < list->size; i++) {
executeStatement(list->statements[i]);
}
}
void executeStatement(Statement *statement) {
if (statement->type == DECLARATION) {
executeDeclaration(statement->declaration);
} else if (statement->type == LOOP) {
executeLoop(statement->loop);
} else if (statement->type == CONDITION) {
executeCondition(statement->condition);
}
}
void executeDeclaration(Declaration *declaration) {
// Allocate memory for the new variable
if (strcmp(declaration->type, "int") == 0) {
int *var = malloc(sizeof(int));
// Save the pointer in the symbol table
SymbolTable_add(declaration->identifier, var);
} else if (strcmp(declaration->type, "float") == 0) {
float *var = malloc(sizeof(float));
// Save the pointer in the symbol table
SymbolTable_add(declaration->identifier, var);
} else if (strcmp(declaration->type, "bool") == 0) {
bool *var = malloc(sizeof(bool));
// Save the pointer in the symbol table
SymbolTable_add(declaration->identifier, var);
} else if (strcmp(declaration->type, "string") == 0) {
char var = malloc(sizeof(char));
// Save the pointer in the symbol table
SymbolTable_add(declaration->identifier, var);
}
}
void executeLoop(Loop *loop) {
// Evaluate the condition
bool result = evaluateCondition(loop->condition);
while (result) {
// Execute the loop body
executeStatements(loop->statementList);
// Re-evaluate the condition
result = evaluateCondition
(loop->condition);
}
}
void executeCondition(Condition *condition) {
// Evaluate the expression
bool result = evaluateCondition(condition->expression1, condition->comparator, condition->expression2);
if (result) {
// Execute the statement list
executeStatements(condition->statementList);
}
}
bool evaluateCondition(Expression *expression1, Comparator *comparator, Expression *expression2) {
// Get the values of the expressions
void *val1 = evaluateExpression(expression1);
void *val2 = evaluateExpression(expression2);
// Compare the values
if (strcmp(comparator->type, "OR") == 0) {
return val1 || val2;
} else if (strcmp(comparator->type, "AND") == 0) {
return val1 && val2;
} else if (strcmp(comparator->type, "NOT") == 0) {
return !val1;
} else if (strcmp(
comparator->type, "EQUALS") == 0) {
return val1 == val2;
} else if (strcmp(comparator->type, "NOT_EQUALS") == 0) {
return val1 != val2;
} else if (strcmp(comparator->type, "GREATER_THAN") == 0) {
return val1 > val2;
} else if (strcmp(comparator->type, "LESS_THAN") == 0) {
return val1 < val2;
} else if (strcmp(comparator->type, "GREATER_THAN_EQUAL") == 0) {
return val1 >= val2;
} else if (strcmp(comparator->type, "LESS_THAN_EQUAL") == 0) {
return val1 <= val2;
}
return false;
}
void* evaluateExpression(Expression *expression) {
if (expression->type == IDENTIFIER) {
// Lookup the variable in the symbol table
return SymbolTable_lookup(expression->identifier);
} else if (expression->type == NUMBER) {
// Return the number
return expression->number;
} else if (expression->type == STRING) {
// Return the string
return expression->string;
}
return NULL;
}
// SymbolTable_add()
/void SymbolTable_add(char name, void* value) {
// Create a new entry in the symbol table
SymbolTableEntry entry = malloc(sizeof(SymbolTableEntry));
entry->name = name;
entry->value = value;
// Add the entry to the symbol table
symbolTable[numEntries++] = entry;
}/
void SymbolTable_add(char *key, void *value) {
// Resize the symbol table if necessary
if (symbol_table->size == symbol_table->capacity) {
symbol_table->capacity *= 2;
symbol_table->entries = realloc(symbol_table->entries, symbol_table->capacity * sizeof(SymbolTableEntry));
}
// Add the entry to the symbol table
SymbolTableEntry *entry = &(symbol_table->entries[symbol_table->size++]);
entry->key = key;
entry->value = value;
}
void *SymbolTable_lookup(char *key) {
// Lookup the entry in the symbol table
for (int i=0; i < symbol_table->size; i++) {
if (strcmp(symbol_table->entries[i].key, key) == 0) {
return symbol_table->entries[i].value;
}
// The variable is not defined
fprintf(stderr, "Error: variable '%s' is not defined\n", key);
exit(1);
}
}
/// SymbolTable_lookup()
void SymbolTable_lookup(char* name) {
// Search the symbol table for the variable
for (int i=0; i < numEntries; i++) {
if (strcmp(symbolTable[i]->name, name) == 0) {
return symbolTable[i]->value;
}
}
// If the variable is not found, return NULL
return NULL;
}*/
%%
int main(void) {
SymbolTable_init();
yyparse();
return 0;
}