added logic between yacc and table, not yet working needs debugging

This commit is contained in:
Raphaël LACROIX 2023-04-12 23:47:13 +02:00
parent cb8854f069
commit 4a5c84bb3e
4 changed files with 53 additions and 25 deletions

29
Makefile Normal file
View file

@ -0,0 +1,29 @@
GRM=yacc.y
LEX=lex.l
BIN=out
CC=gcc
CFLAGS=-Wall -g
OBJ=y.tab.o lex.yy.o table.o
all: $(BIN)
@touch testFile # to prevent an error in case of deletion
./out < testFile
%.o: %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
y.tab.c: $(GRM)
bison --yacc -d $<
lex.yy.c: $(LEX)
flex $<
$(BIN): $(OBJ)
$(CC) $(CFLAGS) $(CPPFLAGS) $^ -o $@
clean:
rm $(OBJ) y.tab.c y.tab.h lex.yy.c

23
table.c
View file

@ -42,6 +42,7 @@ Symbol getStruct(char* name){
}
}
error("No structure found");
return (createNewStructure("error", 0));
}
/* Returns the index with this name*/
@ -52,6 +53,7 @@ int getIndex(char* name){
}
}
error("No index found");
return (0);
}
/* removes all symbols associated with the current Depth*/
@ -67,7 +69,6 @@ void clearOutOfScopeVariable(){
i++;
}
int delta = currentIndex - i ; // the number of elements we remove
int futureCurrentIndex = i;
while(i < currentIndex) {
@ -156,7 +157,7 @@ void displayTable(){
printf("\n");
doubleLine();
printf("Table of Symbols, depth = %d, length = %d, ESP = %d, EBP = %d\n", currentDepth, currentIndex, esp ,ebp);
printf("Name | init?, varType, offset, depth\n", currentDepth, currentIndex);
printf("Name | init?, varType, offset, depth\n");
doubleLine();
for (int i = 0; i < currentIndex; ++i) {
Symbol a = symbolTable[i];
@ -173,22 +174,4 @@ void line(){
}
void doubleLine(){
printf("============================================================\n");
}
int main(){
initSymbolTable();
addElement("variable1", INT);
displayTable();
increaseDepth();
displayTable();
toggleInit("variable1");
displayTable();
addElement("variable2", INT);
displayTable();
addElement("variable3", FLOAT);
displayTable();
decreaseDepth();
displayTable();
addElement("variable4", INT);
displayTable();
}

View file

@ -9,10 +9,10 @@
#define NAME_MAX_LENGTH 30
// a list of all type
typedef enum enumVarType {INT, FLOAT} enumVarType; // TODO : update
typedef enum enumVarType {INT, FLOAT} enumVarType;
// a list of all type's sizes
int memorySizes[2] ={1,1}; // TODO : update
extern int memorySizes[2] = {1,1}; // TODO : PROBLEM DOES'NT COMPILE
typedef struct {
char name[NAME_MAX_LENGTH];
@ -22,8 +22,6 @@ typedef struct {
int depth;
} Symbol;
// TODO : move comments here
/*============================
Array and Reallocation
============================*/
@ -62,7 +60,7 @@ Symbol createNewStructure(char* name, enumVarType type);
void toggleInit(char *name);
/*============================
Element Acess
Element Access
============================*/
/* Returns the index with this name*/

18
testFile Normal file
View file

@ -0,0 +1,18 @@
int compute(int a, int d) {
b = a;
while (c > 0) {
b = b + a * 4;
}
return b;
}
void main(void) {
int a;
if (a == 3) {
print(a);
} else {
int b = compute(a, 2 * a);
print(b);
}
}