start implementing symbol table
This commit is contained in:
parent
7d65980c4f
commit
d250a8f837
6 changed files with 134 additions and 53 deletions
9
.gitignore
vendored
9
.gitignore
vendored
|
@ -1,5 +1,6 @@
|
||||||
as.tab.c
|
/as.tab.c
|
||||||
as.tab.h
|
/as.tab.h
|
||||||
compilateur
|
/compilateur
|
||||||
lex.yy.c
|
/lex.yy.c
|
||||||
|
|
||||||
|
/as.output
|
||||||
|
|
10
Makefile
10
Makefile
|
@ -1,9 +1,11 @@
|
||||||
LEX = flex
|
LEX = flex
|
||||||
YACC = bison -d
|
YACC = bison -d -v
|
||||||
CC = gcc
|
CC = gcc
|
||||||
|
|
||||||
compilateur: as.tab.c lex.yy.c
|
all: compilateur
|
||||||
$(CC) -o compilateur as.tab.c lex.yy.c
|
|
||||||
|
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
|
as.tab.c: as.y
|
||||||
$(YACC) as.y
|
$(YACC) as.y
|
||||||
|
@ -16,4 +18,4 @@ lex.yy.c: al.lex
|
||||||
build: compilateur
|
build: compilateur
|
||||||
|
|
||||||
clean:
|
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
|
||||||
|
|
133
as.y
133
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 ;
|
Params : | Param SuiteParams { printf("Params\n"); } ;
|
||||||
Param : tINT tID ;
|
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 ;
|
Printf : tPRINTF tPO tID tPF tPV { printf("Printf\n"); } ;
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
// If : tIF tPO Cond tPF Body ;
|
// If : tIF tPO Cond tPF Body ;
|
||||||
//
|
//
|
||||||
|
@ -98,24 +100,75 @@ Printf : tPRINTF tPO tID tPF tPV
|
||||||
|
|
||||||
// Return : tRET E 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 <string.h>
|
||||||
|
|
||||||
|
#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) {
|
void yyerror(char const* err) {
|
||||||
printf("%s\n", err);
|
printf("%s\n", err);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
5
symbol_table.c
Normal file
5
symbol_table.c
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#include "symbol_table.h"
|
||||||
|
|
||||||
|
void add_symbol(SymbolTable table, enum Type type, char *name) {
|
||||||
|
|
||||||
|
}
|
25
symbol_table.h
Normal file
25
symbol_table.h
Normal file
|
@ -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 */
|
5
test.c
5
test.c
|
@ -2,9 +2,4 @@ int main(){
|
||||||
int x, y, z;
|
int x, y, z;
|
||||||
const PL5_op, b, c;
|
const PL5_op, b, c;
|
||||||
printf(x);
|
printf(x);
|
||||||
if (x == x && y != 5 + 6 || 1 + 2 == 2 + 1 && !(x == x)) {
|
|
||||||
printf(x);
|
|
||||||
} else {
|
|
||||||
x = 3 + 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue