There are several shift/reduce conflicts in this code, which can be resolved by adding precedence and associativity rules to the grammar.
One approach is to use precedence rules for the arithmetic operators (+, -, *, /) and logical operators (OR, AND). We can also use %left, %right, and %nonassoc directives to specify the associativity of operators.
For example, to specify that multiplication and division have higher precedence than addition and subtraction, we can add:
%left '+' '-'
%left '*' '/'
Similarly, to specify that AND has higher precedence than OR, we can add:
%left OR
%left AND
We can also use %nonassoc to specify that certain operators are non-associative. For example, to specify that equality operators are non-associative, we can add:
%nonassoc EQUALS NOT_EQUALS
Here's an updated grammar with precedence and associativity rules:
%{
#include <stdio.h>
#include <stdlib.h>
%}
%start program
%token IDENTIFIER
%token NUMBER
%token INTEGER
%token FLOAT
%token BOOLEAN
%token STRING
%token OR
%token AND
%token NOT
%token EQUALS
%token NOT_EQUALS
%token GREATER_THAN
%token LESS_THAN
%token GREATER_THAN_EQUAL
%token LESS_THAN_EQUAL
%token FOR
%token WHILE
%token IF
%token ELSE
%token SEMICOLON
%token type
%token VALUE
// Define yyval
%union {
int number;
char *string;
}
%type IDENTIFIER
%type NUMBER
%type STRING
%type declaration
%type loop
%type condition
%type expression
%type comparator
%type <statement_list> statement_list
// Precedence and Associativity Rules
%left '+' '-'
%left '*' '/'
%left OR
%left AND
%nonassoc EQUALS NOT_EQUALS GREATER_THAN LESS_THAN GREATER_THAN_EQUAL LESS_THAN_EQUAL
%%
// Program
program: statement_list | statement ;
statement: declaration | loop | condition | SEMICOLON ;
// Action Code
// 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 %prec '+'
| expression '-' expression %prec '-'
| expression ''
| expression '/' expression %prec '/'
;
// 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");}
| LESS_THAN {printf("Comparator: <\n");}
| GREATER_THAN_EQUAL {printf("Comparator: >=\n");}
| LESS_THAN_EQUAL {printf("Comparator: <=\n");}
;
// Statement List Action
statement_list: statement_list statement {printf("Statement List: \n");}
| statement {printf("Statement List: \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