aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam NAILI2018-04-22 15:32:40 +0200
committerAdam NAILI2018-04-22 15:32:40 +0200
commita431f189d33af68ea179e464c889761e65ed1f08 (patch)
tree2da926fa6eccd0d2b9459be841fd0168d474e607 /src
parent5d83670f6b0b142bcda19270d3c9b50f9370bee8 (diff)
downloadtpc-compiler-a431f189d33af68ea179e464c889761e65ed1f08.tar.gz
Basic implementation of the symbol table
Diffstat (limited to 'src')
-rw-r--r--src/symboltable.c25
-rw-r--r--src/symboltable.h3
-rw-r--r--src/tpc.lex7
-rw-r--r--src/tpc.y22
4 files changed, 40 insertions, 17 deletions
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[]) {
28 if (!strcmp(symbol_table.entries[count].name, name)) { 28 if (!strcmp(symbol_table.entries[count].name, name)) {
29 return; 29 return;
30 } 30 }
31 printf("No definition of the variable %s near line %d\n", name,
32 lineno);
33 } 31 }
32 printf("No definition of the variable %s near line %d\n", name,
33 lineno);
34}
35
36void display_table(){
37 int count;
38 for (count=0;count<symbol_table.size;count++) {
39 if(symbol_table.entries[count].type == INT)
40 printf("entier: %s, pos: %d \n", symbol_table.entries[count].name, symbol_table.entries[count].addr);
41 else
42 printf("caractere: %s, pos: %d \n", symbol_table.entries[count].name, symbol_table.entries[count].addr);
43 }
44 printf("\n");
34} 45}
35 46
47int get_type(const char name[]){
48 int count;
49
50 for (count = 0; count < symbol_table.size; count++) {
51 if (!strcmp(symbol_table.entries[count].name, name)) {
52 return symbol_table.entries[count].type;
53 }
54 }
55 return -1;
56} \ No newline at end of file
diff --git a/src/symboltable.h b/src/symboltable.h
index cc828fa..02e47a4 100644
--- a/src/symboltable.h
+++ b/src/symboltable.h
@@ -13,6 +13,7 @@
13typedef struct { 13typedef struct {
14 char name[MAXNAME]; 14 char name[MAXNAME];
15 int type; 15 int type;
16 int addr;
16} STentry; 17} STentry;
17 18
18 19
@@ -26,5 +27,5 @@ typedef struct {
26void addVar(const char name[], int type); 27void addVar(const char name[], int type);
27void lookup(const char name[]); 28void lookup(const char name[]);
28void display_table(); 29void display_table();
29 30int get_type(const char name[]);
30#endif \ No newline at end of file 31#endif \ No newline at end of file
diff --git a/src/tpc.lex b/src/tpc.lex
index 5137d39..7a0d6b6 100644
--- a/src/tpc.lex
+++ b/src/tpc.lex
@@ -5,7 +5,8 @@
5 */ 5 */
6 6
7#include "tpc.tab.h" 7#include "tpc.tab.h"
8 8#define INT 0
9#define CHAR 1
9int lineno = 1; 10int lineno = 1;
10%} 11%}
11 12
@@ -22,8 +23,8 @@ int lineno = 1;
22"+"|- { yylval.addsub=yytext[0]; return ADDSUB; } 23"+"|- { yylval.addsub=yytext[0]; return ADDSUB; }
23"<"|"<="|">"|">=" { strcpy(yylval.comp, yytext); return ORDER; } 24"<"|"<="|">"|">=" { strcpy(yylval.comp, yytext); return ORDER; }
24==|!= { strcpy(yylval.comp, yytext); return EQ; } 25==|!= { strcpy(yylval.comp, yytext); return EQ; }
25entier { strcpy(yylval.type, yytext); return TYPE; } 26entier { yylval.type = INT; return TYPE; }
26caractere { strcpy(yylval.type, yytext); return TYPE; } 27caractere { yylval.type = CHAR; return TYPE; }
27void { return VOID; } 28void { return VOID; }
28const { return CONST; } 29const { return CONST; }
29if { return IF; } 30if { 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 *);
17 char caractere; 17 char caractere;
18 int num; 18 int num;
19 char ident[64]; 19 char ident[64];
20 char type[32]; 20 int type;
21 char comp[3]; 21 char comp[3];
22 char addsub; 22 char addsub;
23 char divstar; 23 char divstar;
@@ -39,7 +39,7 @@ void yyerror(char *);
39 39
40%% 40%%
41Prog: 41Prog:
42 DeclConsts DeclVars DeclFoncts 42 DeclConsts DeclVars DeclFoncts {display_table();}
43 ; 43 ;
44DeclConsts: 44DeclConsts:
45 DeclConsts CONST ListConst ';' 45 DeclConsts CONST ListConst ';'
@@ -62,8 +62,8 @@ DeclVars:
62 | 62 |
63 ; 63 ;
64Declarateurs: 64Declarateurs:
65 Declarateurs ',' Declarateur 65 Declarateurs ',' Declarateur {addVar($<ident>3, $<type>0);}
66 | Declarateur 66 | Declarateur {addVar($<ident>1, $<type>0);}
67 ; 67 ;
68Declarateur: 68Declarateur:
69 IDENT 69 IDENT
@@ -85,8 +85,8 @@ Parametres:
85 | ListTypVar 85 | ListTypVar
86 ; 86 ;
87ListTypVar: 87ListTypVar:
88 ListTypVar ',' TYPE IDENT 88 ListTypVar ',' TYPE IDENT {addVar($<ident>4, $<type>3);}
89 | TYPE IDENT 89 | TYPE IDENT {addVar($<ident>2, $<type>1);}
90 ; 90 ;
91Corps: 91Corps:
92 '{' DeclConsts DeclVars SuiteInstr '}' 92 '{' DeclConsts DeclVars SuiteInstr '}'
@@ -99,8 +99,8 @@ Instr:
99 | ';' 99 | ';'
100 | RETURN Exp ';' 100 | RETURN Exp ';'
101 | RETURN ';' 101 | RETURN ';'
102 | READE '(' IDENT ')' ';' 102 | READE '(' IDENT ')' ';' {lookup($<ident>3);}
103 | READC '(' IDENT ')' ';' 103 | READC '(' IDENT ')' ';' {lookup($<ident>3);}
104 | PRINT '(' Exp ')' ';' 104 | PRINT '(' Exp ')' ';'
105 | IF '(' Exp ')' Instr 105 | IF '(' Exp ')' Instr
106 | IF '(' Exp ')' Instr ELSE Instr 106 | IF '(' Exp ')' Instr ELSE Instr
@@ -139,14 +139,14 @@ F:
139 ADDSUB F {$$ = $2;} //on fait remonter le type 139 ADDSUB F {$$ = $2;} //on fait remonter le type
140 | '!' F {$$ = $2;} 140 | '!' F {$$ = $2;}
141 | '(' Exp ')' {$$ = $2;} 141 | '(' Exp ')' {$$ = $2;}
142 | LValue //Il faut trouver le type de LValue 142 | LValue {$$ = $1;}
143 | NUM {$$ = INT;} // on stocke les types pour l'analyse sémantique 143 | NUM {$$ = INT;} // on stocke les types pour l'analyse sémantique
144 | CARACTERE {$$ = CHAR;} 144 | CARACTERE {$$ = CHAR;}
145 | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement 145 | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement
146 ; 146 ;
147LValue: 147LValue:
148 IDENT {lookup($<ident>1);} 148 IDENT {lookup($<ident>1);get_type($<ident>1);}
149 | IDENT '[' Exp ']' {lookup($<ident>1);} 149 | IDENT '[' Exp ']' {lookup($<ident>1);get_type($<ident>1);}
150 ; 150 ;
151Arguments: 151Arguments:
152 ListExp 152 ListExp