added logic between yacc and table, not yet working needs debugging
This commit is contained in:
parent
cb8854f069
commit
4a5c84bb3e
4 changed files with 53 additions and 25 deletions
29
Makefile
Normal file
29
Makefile
Normal 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
23
table.c
|
@ -42,6 +42,7 @@ Symbol getStruct(char* name){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
error("No structure found");
|
error("No structure found");
|
||||||
|
return (createNewStructure("error", 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns the index with this name*/
|
/* Returns the index with this name*/
|
||||||
|
@ -52,6 +53,7 @@ int getIndex(char* name){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
error("No index found");
|
error("No index found");
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* removes all symbols associated with the current Depth*/
|
/* removes all symbols associated with the current Depth*/
|
||||||
|
@ -67,7 +69,6 @@ void clearOutOfScopeVariable(){
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
int delta = currentIndex - i ; // the number of elements we remove
|
|
||||||
int futureCurrentIndex = i;
|
int futureCurrentIndex = i;
|
||||||
|
|
||||||
while(i < currentIndex) {
|
while(i < currentIndex) {
|
||||||
|
@ -156,7 +157,7 @@ void displayTable(){
|
||||||
printf("\n");
|
printf("\n");
|
||||||
doubleLine();
|
doubleLine();
|
||||||
printf("Table of Symbols, depth = %d, length = %d, ESP = %d, EBP = %d\n", currentDepth, currentIndex, esp ,ebp);
|
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();
|
doubleLine();
|
||||||
for (int i = 0; i < currentIndex; ++i) {
|
for (int i = 0; i < currentIndex; ++i) {
|
||||||
Symbol a = symbolTable[i];
|
Symbol a = symbolTable[i];
|
||||||
|
@ -174,21 +175,3 @@ void line(){
|
||||||
void doubleLine(){
|
void doubleLine(){
|
||||||
printf("============================================================\n");
|
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();
|
|
||||||
}
|
|
8
table.h
8
table.h
|
@ -9,10 +9,10 @@
|
||||||
#define NAME_MAX_LENGTH 30
|
#define NAME_MAX_LENGTH 30
|
||||||
|
|
||||||
// a list of all type
|
// 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
|
// 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 {
|
typedef struct {
|
||||||
char name[NAME_MAX_LENGTH];
|
char name[NAME_MAX_LENGTH];
|
||||||
|
@ -22,8 +22,6 @@ typedef struct {
|
||||||
int depth;
|
int depth;
|
||||||
} Symbol;
|
} Symbol;
|
||||||
|
|
||||||
// TODO : move comments here
|
|
||||||
|
|
||||||
/*============================
|
/*============================
|
||||||
Array and Reallocation
|
Array and Reallocation
|
||||||
============================*/
|
============================*/
|
||||||
|
@ -62,7 +60,7 @@ Symbol createNewStructure(char* name, enumVarType type);
|
||||||
void toggleInit(char *name);
|
void toggleInit(char *name);
|
||||||
|
|
||||||
/*============================
|
/*============================
|
||||||
Element Acess
|
Element Access
|
||||||
============================*/
|
============================*/
|
||||||
|
|
||||||
/* Returns the index with this name*/
|
/* Returns the index with this name*/
|
||||||
|
|
18
testFile
Normal file
18
testFile
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue