diff --git a/.gitignore b/.gitignore index 90594c3..d035dbc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -as.tab.c -as.tab.h -compilateur -lex.yy.c +/as.tab.c +/as.tab.h +/compilateur +/lex.yy.c +/as.output diff --git a/Makefile b/Makefile index 1b1b18d..0e4e519 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,11 @@ LEX = flex -YACC = bison -d +YACC = bison -d -v CC = gcc -compilateur: as.tab.c lex.yy.c - $(CC) -o compilateur as.tab.c lex.yy.c +all: compilateur + +compilateur: as.tab.c lex.yy.c symbol_table.c + $(CC) -o compilateur as.tab.c lex.yy.c symbol_table.c as.tab.c: as.y $(YACC) as.y @@ -16,4 +18,4 @@ lex.yy.c: al.lex build: compilateur clean: - rm as.tab.c as.tab.h lex.yy.c compilateur + rm as.tab.c as.tab.h lex.yy.c compilateur as.output diff --git a/as.y b/as.y index a87d8b8..aee84ce 100644 --- a/as.y +++ b/as.y @@ -47,46 +47,48 @@ %% -S : Main ; +S : Main { printf("S\n"); } ; -Main : tINT tMAIN tPO Params tPF MainBody ; +Main : tINT tMAIN tPO Params tPF MainBody { printf("Main\n"); } ; -Params : | Param SuiteParams ; -Param : tINT tID ; +Params : | Param SuiteParams { printf("Params\n"); } ; +Param : tINT tID { printf("Param\n"); } ; -SuiteParams : tVIRG Param SuiteParams ; +SuiteParams : tVIRG Param SuiteParams { printf("SuiteParam\n"); } ; -MainBody : tAO Declarations Instructions tAF ; +MainBody : tAO Declarations Instructions tAF { printf("MainBody\n"); } ; -Body : tAO Instructions tAF ; +Body : tAO Instructions tAF { printf("Body\n"); } ; -Instructions : Instruction Instructions | ; +Instructions : Instruction Instructions | { printf("Instructions\n"); } ; -Instruction : Aff | Printf | If ; +Instruction : Aff | Printf { printf("Instruction\n"); } ; -Aff : tID tEQ E tPV ; +Aff : tID tEQ E tPV { printf("Aff\n"); } ; -E : tNB | tID | E tADD E | E tMUL E | E tSUB E | E tDIV E | tPO E tPF | tSUB E ; +E : tNB | tID | E tADD E | E tMUL E | E tSUB E | E tDIV E | tPO E tPF | tSUB E { printf("E\n"); } ; -Declarations : | Declaration Declarations ; +Declarations : | Declaration Declarations { printf("Declarations\n"); } ; -Declaration : DeclarationInt | DeclarationConst ; +Declaration : DeclarationInt | DeclarationConst { printf("Declaration\n"); } ; -DeclarationInt : tINT DeclarationBody tPV ; +DeclarationInt : tINT DeclarationBody tPV { printf("DeclarationInt\n"); } ; -DeclarationConst : tCONST DeclarationBody tPV ; +DeclarationConst : tCONST DeclarationBody tPV { printf("DeclarationConst\n"); } ; -DeclarationBody : tID tVIRG DeclarationBody | tID ; +DeclarationBody : tID tVIRG DeclarationBody | tID { printf("DeclarationBody\n"); } ; -If : IfSimple | IfElse ; +// While : tWHILE tPO Cond tPF Body { printf("While\n"); } ; +// +// If : IfSimple | IfElse { printf("If\n"); } ; +// +// IfSimple : tIF tPO Cond tPF Body { printf("IfSimple\n"); } ; +// +// IfElse : IfSimple tELSE Body { printf("IfElse\n"); } ; +// +// Cond : tNOT Cond | Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | E tINFEQ E | E tSUP E | E tSUPEQ E | E tNOTEQ E | tNOT tPO Cond tPF { printf("Cond\n"); } ; -IfSimple : tIF tPO Cond tPF Body ; - -IfElse : IfSimple tELSE Body ; - -Cond : Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | E tINFEQ E | E tSUP E | E tSUPEQ E | E tNOTEQ E | tNOT Cond | tNOT tPO Cond tPF ; - -Printf : tPRINTF tPO tID tPF tPV +Printf : tPRINTF tPO tID tPF tPV { printf("Printf\n"); } ; // If : tIF tPO Cond tPF Body ; // @@ -98,24 +100,75 @@ Printf : tPRINTF tPO tID tPF tPV // Return : tRET E tPV ; - -/* S -> E ; S - * S -> - */ -// S : E tPV -// { printf("RES: %d\n", $1); } -// S -// | { printf("END\n"); } -// ; -// -// E : E tADD E { $$ = $1 + $3; } -// | E tSUB E { $$ = $1 - $3; } -// | tOB E tCB { $$ = $2; } -// | tNB { $$ = $1; } -// ; - %% +#include + +#define LABEL_SIZE 10 + +// ADD @ + @ -> @ +// SUB @ - @ -> @ +// AFC nb -> @ +// CPY @ -> @ + +// Exemple +// y = 1 + 2; +// AFC 1 32 +// AFC 2 33 +// ADD 32 33 32 +// CPY 32 @y + +/* +table des symboles (tS) : +sous forme de pile + -> où sera la variable lors de l'exec + | +| nom | type | @ | init | +|-------|--------|-----|--------| +| a | | 0 | | +| b | | 1 | | +| c | | 2 | | +| | | | | +| . | | . | | -> entiers sur 1 octet +| . | | . | | +| . | | . | | +| | | | | +| y | | 26 | | + +Donc on remplace @y par 26 dans CPY 32 @y + +/!\ + +E + E + E +| | | +-> -> -> vt variable temporaire (autre expression) +-> -> -> variable -> ADD @E1 @E2 vt +-> -> -> nombre + +Donc 9 possibilités (3 par E) + +On suppose que E est tout le temps une vt, donc on génère tout le temps le même code + +*/ + +int id_counter = 0; + +typedef struct node { + char label[LABEL_SIZE]; + int left_child; + int right_child; + int id; +} node; + +int create_node(char* l, int n1, int n2) { + node n; + strcpy(l, n.label); + n.left_child = n1; + n.right_child = n2; + n.id = id_counter; + id_counter++; +} + void yyerror(char const* err) { printf("%s\n", err); exit(1); diff --git a/symbol_table.c b/symbol_table.c new file mode 100644 index 0000000..73aafe5 --- /dev/null +++ b/symbol_table.c @@ -0,0 +1,5 @@ +#include "symbol_table.h" + +void add_symbol(SymbolTable table, enum Type type, char *name) { + +} \ No newline at end of file diff --git a/symbol_table.h b/symbol_table.h new file mode 100644 index 0000000..3287251 --- /dev/null +++ b/symbol_table.h @@ -0,0 +1,25 @@ +#ifndef SYMBOL_TABLE_H_INCLUDED +#define SYMBOL_TABLE_H_INCLUDED + +#define SYMBOL_TABLE_SIZE 100 + +enum Type { + TYPE_CONST, + TYPE_INT +}; + +struct SymbolItem { + enum Type type; + char* name; + int address; + int init; +}; + +typedef struct SymbolTable { + struct SymbolIte table[SYMBOL_TABLE_SIZE]; + int index; +} SymbolTable; + +void add_symbol(SymbolTable table, enum Type type, char* name); + +#endif /* !SYMBOL_TABLE_H_INCLUDED */ diff --git a/test.c b/test.c index 0d5161d..082dddd 100644 --- a/test.c +++ b/test.c @@ -2,9 +2,4 @@ int main(){ int x, y, z; const PL5_op, b, c; printf(x); - if (x == x && y != 5 + 6 || 1 + 2 == 2 + 1 && !(x == x)) { - printf(x); - } else { - x = 3 + 2; - } }