diff options
author | Adam NAILI | 2018-04-22 15:32:40 +0200 |
---|---|---|
committer | Adam NAILI | 2018-04-22 15:32:40 +0200 |
commit | a431f189d33af68ea179e464c889761e65ed1f08 (patch) | |
tree | 2da926fa6eccd0d2b9459be841fd0168d474e607 /src | |
parent | 5d83670f6b0b142bcda19270d3c9b50f9370bee8 (diff) | |
download | tpc-compiler-a431f189d33af68ea179e464c889761e65ed1f08.tar.gz |
Basic implementation of the symbol table
Diffstat (limited to 'src')
-rw-r--r-- | src/symboltable.c | 25 | ||||
-rw-r--r-- | src/symboltable.h | 3 | ||||
-rw-r--r-- | src/tpc.lex | 7 | ||||
-rw-r--r-- | src/tpc.y | 22 |
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 | |||
36 | void 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 | ||
47 | int 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 @@ | |||
13 | typedef struct { | 13 | typedef 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 { | |||
26 | void addVar(const char name[], int type); | 27 | void addVar(const char name[], int type); |
27 | void lookup(const char name[]); | 28 | void lookup(const char name[]); |
28 | void display_table(); | 29 | void display_table(); |
29 | 30 | int 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 | ||
9 | int lineno = 1; | 10 | int 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; } |
25 | entier { strcpy(yylval.type, yytext); return TYPE; } | 26 | entier { yylval.type = INT; return TYPE; } |
26 | caractere { strcpy(yylval.type, yytext); return TYPE; } | 27 | caractere { yylval.type = CHAR; return TYPE; } |
27 | void { return VOID; } | 28 | void { return VOID; } |
28 | const { return CONST; } | 29 | const { return CONST; } |
29 | if { return IF; } | 30 | if { return IF; } |
@@ -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 | %% |
41 | Prog: | 41 | Prog: |
42 | DeclConsts DeclVars DeclFoncts | 42 | DeclConsts DeclVars DeclFoncts {display_table();} |
43 | ; | 43 | ; |
44 | DeclConsts: | 44 | DeclConsts: |
45 | DeclConsts CONST ListConst ';' | 45 | DeclConsts CONST ListConst ';' |
@@ -62,8 +62,8 @@ DeclVars: | |||
62 | | | 62 | | |
63 | ; | 63 | ; |
64 | Declarateurs: | 64 | Declarateurs: |
65 | Declarateurs ',' Declarateur | 65 | Declarateurs ',' Declarateur {addVar($<ident>3, $<type>0);} |
66 | | Declarateur | 66 | | Declarateur {addVar($<ident>1, $<type>0);} |
67 | ; | 67 | ; |
68 | Declarateur: | 68 | Declarateur: |
69 | IDENT | 69 | IDENT |
@@ -85,8 +85,8 @@ Parametres: | |||
85 | | ListTypVar | 85 | | ListTypVar |
86 | ; | 86 | ; |
87 | ListTypVar: | 87 | ListTypVar: |
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 | ; |
91 | Corps: | 91 | Corps: |
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 | ; |
147 | LValue: | 147 | LValue: |
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 | ; |
151 | Arguments: | 151 | Arguments: |
152 | ListExp | 152 | ListExp |