From 40f423a4b3b1e64e8424ab239cc14ecc5077640f Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Sun, 22 Apr 2018 14:49:42 +0200 Subject: Beginning of the symbol table implementation --- makefile | 10 +++++++--- res/ko_sample.tpc | 2 +- res/ok_sample.tpc | 4 ++-- res/test-table-symboles.tpc | 13 +++++++++++++ src/symboltable.c | 35 +++++++++++++++++++++++++++++++++++ src/symboltable.h | 30 ++++++++++++++++++++++++++++++ src/tpc.lex | 25 ++++++++++++++++--------- src/tpc.y | 44 +++++++++++++++++++++++++++++--------------- 8 files changed, 133 insertions(+), 30 deletions(-) create mode 100644 res/test-table-symboles.tpc create mode 100644 src/symboltable.c create mode 100644 src/symboltable.h diff --git a/makefile b/makefile index 30c573a..31a4316 100644 --- a/makefile +++ b/makefile @@ -10,6 +10,7 @@ DOC_DIR := doc LEX_SRC := tpc.lex YACC_SRC := tpc.y PDF_SRC := rapport.md +ST_SRC := symboltable # INTERMEDIATE LEX_GEN := tpc.yy @@ -34,8 +35,8 @@ all: clean $(OUT_DIR)/$(COMPIL_BIN) $(OUT_DIR)/$(LEX_GEN).c: $(SRC_DIR)/$(LEX_SRC) $(LEX) -o $@ $^ -$(OUT_DIR)/$(YACC_GEN).c $(OUT_DIR)/$(YACC_GEN).h: $(SRC_DIR)/$(YACC_SRC) - $(YACC) --output=$(OUT_DIR)/$(YACC_GEN).c --defines=$(OUT_DIR)/$(YACC_GEN).h -v $^ +$(OUT_DIR)/$(YACC_GEN).c $(OUT_DIR)/$(YACC_GEN).h: $(SRC_DIR)/$(YACC_SRC) $(SRC_DIR)/$(ST_SRC).h + $(YACC) --output=$(OUT_DIR)/$(YACC_GEN).c --defines=$(OUT_DIR)/$(YACC_GEN).h -v $< $(OUT_DIR)/$(LEX_GEN).o: $(OUT_DIR)/$(LEX_GEN).c $(OUT_DIR)/$(YACC_GEN).h $(CC) -o $@ -c $< $(IFLAGS) $(LFLAGS) $(CFLAGS) @@ -43,7 +44,10 @@ $(OUT_DIR)/$(LEX_GEN).o: $(OUT_DIR)/$(LEX_GEN).c $(OUT_DIR)/$(YACC_GEN).h $(OUT_DIR)/$(YACC_GEN).o: $(OUT_DIR)/$(YACC_GEN).c $(CC) -o $@ -c $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) -$(OUT_DIR)/$(COMPIL_BIN): $(OUT_DIR)/$(LEX_GEN).o $(OUT_DIR)/$(YACC_GEN).o +$(OUT_DIR)/$(ST_SRC).o: $(SRC_DIR)/$(ST_SRC).c + $(CC) -o $@ -c $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) + +$(OUT_DIR)/$(COMPIL_BIN): $(OUT_DIR)/$(LEX_GEN).o $(OUT_DIR)/$(YACC_GEN).o $(OUT_DIR)/$(ST_SRC).o $(CC) -o $@ $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) $(OUT_DIR)/$(REPORT_PDF): $(DOC_DIR)/$(PDF_SRC) diff --git a/res/ko_sample.tpc b/res/ko_sample.tpc index f3a4c7a..9dc344f 100644 --- a/res/ko_sample.tpc +++ b/res/ko_sample.tpc @@ -6,7 +6,7 @@ const special = 'b'; -char funky_func(int arg) { +caractere funky_func(entier arg) { return !arg; } diff --git a/res/ok_sample.tpc b/res/ok_sample.tpc index 7591dfd..7b56e7f 100644 --- a/res/ok_sample.tpc +++ b/res/ok_sample.tpc @@ -6,12 +6,12 @@ const special = 'b'; -char funky_func(int arg) { +caractere funky_func(entier arg) { return !arg; } void main(void) { - char ret; + caractere ret; ret = 1 + funky_func(0) * 2; while (ret) { diff --git a/res/test-table-symboles.tpc b/res/test-table-symboles.tpc new file mode 100644 index 0000000..3c12144 --- /dev/null +++ b/res/test-table-symboles.tpc @@ -0,0 +1,13 @@ +/* test-table-symboles.tpc */ + +/* Test file for simplified translator of a declaration of variables in C */ + entier r1,b,s,c,r2 ; + caractere letter, digit, punct; + +entier main(void) { + r1=12; + r2=24; + b=r1+r2; + punct='.'; + return b; +} diff --git a/src/symboltable.c b/src/symboltable.c new file mode 100644 index 0000000..852d1b8 --- /dev/null +++ b/src/symboltable.c @@ -0,0 +1,35 @@ +#include "symboltable.h" + +extern int lineno; /* from lexical analyser */ + +SymbolTable symbol_table = {{{{0},0}},MAXSYMBOLS,0}; + +void addVar(const char name[], int type) { + int count; + for (count = 0; count < symbol_table.size; count++) { + if (!strcmp(symbol_table.entries[count].name, name)) { + printf("semantic error, redefinition of variable %s near line %d\n", name, + lineno); + return; + } + } + if (++symbol_table.size > symbol_table.maxsize) { + printf("too many variables near line %d\n", lineno); + exit(1); + } + strcpy(symbol_table.entries[symbol_table.size - 1].name, name); + symbol_table.entries[symbol_table.size - 1].type = type; +} + +void lookup(const char name[]) { + int count; + + for (count = 0; count < symbol_table.size; count++) { + if (!strcmp(symbol_table.entries[count].name, name)) { + return; + } + printf("No definition of the variable %s near line %d\n", name, + lineno); + } +} + diff --git a/src/symboltable.h b/src/symboltable.h new file mode 100644 index 0000000..cc828fa --- /dev/null +++ b/src/symboltable.h @@ -0,0 +1,30 @@ +#ifndef __SYMBOLTABLE_H__ + +#include +#include +#include + +#define MAXNAME 32 +#define INT 0 +#define CHAR 1 +#define MAXSYMBOLS 256 + + +typedef struct { + char name[MAXNAME]; + int type; +} STentry; + + +typedef struct { + STentry entries[MAXSYMBOLS]; + int maxsize; + int size; +} SymbolTable; + + +void addVar(const char name[], int type); +void lookup(const char name[]); +void display_table(); + +#endif \ No newline at end of file diff --git a/src/tpc.lex b/src/tpc.lex index 8b824c0..5137d39 100644 --- a/src/tpc.lex +++ b/src/tpc.lex @@ -18,12 +18,12 @@ int lineno = 1; "/*" { BEGIN COMMENT; } "&&" { return AND; } "||" { return OR; } -"*"|"/"|% { return DIVSTAR; } -"+"|- { return ADDSUB; } -"<"|"<="|">"|">=" { return ORDER; } -==|!= { return EQ; } -int { return TYPE; } -char { return TYPE; } +"*"|"/"|% { yylval.divstar=yytext[0]; return DIVSTAR; } +"+"|- { yylval.addsub=yytext[0]; return ADDSUB; } +"<"|"<="|">"|">=" { strcpy(yylval.comp, yytext); return ORDER; } +==|!= { strcpy(yylval.comp, yytext); return EQ; } +entier { strcpy(yylval.type, yytext); return TYPE; } +caractere { strcpy(yylval.type, yytext); return TYPE; } void { return VOID; } const { return CONST; } if { return IF; } @@ -33,9 +33,16 @@ return { return RETURN; } print { return PRINT; } readc { return READC; } reade { return READE; } -[a-zA-Z_][a-zA-Z0-9_]* { return IDENT; } -[0-9]+ { return NUM; } -'\\?.' { return CARACTERE; } +[a-zA-Z_][a-zA-Z0-9_]* { strcpy(yylval.ident, yytext); return IDENT; } +[0-9]+ { sscanf(yytext, "%d", &(yylval.num)); return NUM; } +'\\?.' { if (strlen(yytext)==3) + yylval.caractere=yytext[1]; + else switch(yytext[2]) { + case 'n': yylval.caractere='\n'; break; + case 't': yylval.caractere='\t'; break; + case '\'': yylval.caractere='\''; break; + } + return CARACTERE; } . { return yytext[0]; } "*/" { BEGIN INITIAL; } \n { lineno++; } diff --git a/src/tpc.y b/src/tpc.y index a44a9aa..f1a51ff 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -5,6 +5,7 @@ */ #include +#include "symboltable.h" extern int lineno; int yylex(); @@ -12,12 +13,25 @@ void yyerror(char *); %} -%token CARACTERE NUM IDENT -%token ADDSUB DIVSTAR -%token ORDER EQ OR AND -%token IF WHILE RETURN -%token CONST VOID TYPE -%token PRINT READC READE +%union { + char caractere; + int num; + char ident[64]; + char type[32]; + char comp[3]; + char addsub; + char divstar; +} +%token CARACTERE +%token NUM +%token IDENT +%token ORDER EQ +%token ADDSUB +%token DIVSTAR +%token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE +%token TYPE + +%type Exp EB TB FB M E T F LValue %left ',' %precedence ')' @@ -122,17 +136,17 @@ T: | F ; F: - ADDSUB F - | '!' F - | '(' Exp ')' - | LValue - | NUM - | CARACTERE - | IDENT '(' Arguments ')' + ADDSUB F {$$ = $2;} //on fait remonter le type + | '!' F {$$ = $2;} + | '(' Exp ')' {$$ = $2;} + | LValue //Il faut trouver le type de LValue + | NUM {$$ = INT;} // on stocke les types pour l'analyse sémantique + | CARACTERE {$$ = CHAR;} + | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement ; LValue: - IDENT - | IDENT '[' Exp ']' + IDENT {lookup($1);} + | IDENT '[' Exp ']' {lookup($1);} ; Arguments: ListExp -- cgit v1.2.3