From a431f189d33af68ea179e464c889761e65ed1f08 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Sun, 22 Apr 2018 15:32:40 +0200 Subject: Basic implementation of the symbol table --- src/symboltable.c | 25 +++++++++++++++++++++++-- src/symboltable.h | 3 ++- src/tpc.lex | 7 ++++--- src/tpc.y | 22 +++++++++++----------- 4 files changed, 40 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/symboltable.c b/src/symboltable.c index 852d1b8..3f22b44 100644 --- a/src/symboltable.c +++ b/src/symboltable.c @@ -28,8 +28,29 @@ void lookup(const char name[]) { if (!strcmp(symbol_table.entries[count].name, name)) { return; } - printf("No definition of the variable %s near line %d\n", name, - lineno); } + printf("No definition of the variable %s near line %d\n", name, + lineno); +} + +void display_table(){ + int count; + for (count=0;count"|">=" { 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; } +entier { yylval.type = INT; return TYPE; } +caractere { yylval.type = CHAR; return TYPE; } void { return VOID; } const { return CONST; } if { return IF; } diff --git a/src/tpc.y b/src/tpc.y index f1a51ff..3b902be 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -17,7 +17,7 @@ void yyerror(char *); char caractere; int num; char ident[64]; - char type[32]; + int type; char comp[3]; char addsub; char divstar; @@ -39,7 +39,7 @@ void yyerror(char *); %% Prog: - DeclConsts DeclVars DeclFoncts + DeclConsts DeclVars DeclFoncts {display_table();} ; DeclConsts: DeclConsts CONST ListConst ';' @@ -62,8 +62,8 @@ DeclVars: | ; Declarateurs: - Declarateurs ',' Declarateur - | Declarateur + Declarateurs ',' Declarateur {addVar($3, $0);} + | Declarateur {addVar($1, $0);} ; Declarateur: IDENT @@ -85,8 +85,8 @@ Parametres: | ListTypVar ; ListTypVar: - ListTypVar ',' TYPE IDENT - | TYPE IDENT + ListTypVar ',' TYPE IDENT {addVar($4, $3);} + | TYPE IDENT {addVar($2, $1);} ; Corps: '{' DeclConsts DeclVars SuiteInstr '}' @@ -99,8 +99,8 @@ Instr: | ';' | RETURN Exp ';' | RETURN ';' - | READE '(' IDENT ')' ';' - | READC '(' IDENT ')' ';' + | READE '(' IDENT ')' ';' {lookup($3);} + | READC '(' IDENT ')' ';' {lookup($3);} | PRINT '(' Exp ')' ';' | IF '(' Exp ')' Instr | IF '(' Exp ')' Instr ELSE Instr @@ -139,14 +139,14 @@ F: ADDSUB F {$$ = $2;} //on fait remonter le type | '!' F {$$ = $2;} | '(' Exp ')' {$$ = $2;} - | LValue //Il faut trouver le type de LValue + | LValue {$$ = $1;} | NUM {$$ = INT;} // on stocke les types pour l'analyse sémantique | CARACTERE {$$ = CHAR;} | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement ; LValue: - IDENT {lookup($1);} - | IDENT '[' Exp ']' {lookup($1);} + IDENT {lookup($1);get_type($1);} + | IDENT '[' Exp ']' {lookup($1);get_type($1);} ; Arguments: ListExp -- cgit v1.2.3