aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam NAILI2018-04-22 14:49:42 +0200
committerAdam NAILI2018-04-22 14:49:42 +0200
commit40f423a4b3b1e64e8424ab239cc14ecc5077640f (patch)
treec7da7c2eed97d62cc8ab2e003293935cda5a93dc /src
parentc0802bc17f856546b95a5b51252f7a35d9e1ab10 (diff)
downloadtpc-compiler-40f423a4b3b1e64e8424ab239cc14ecc5077640f.tar.gz
Beginning of the symbol table implementation
Diffstat (limited to 'src')
-rw-r--r--src/symboltable.c35
-rw-r--r--src/symboltable.h30
-rw-r--r--src/tpc.lex25
-rw-r--r--src/tpc.y44
4 files changed, 110 insertions, 24 deletions
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 @@
1#include "symboltable.h"
2
3extern int lineno; /* from lexical analyser */
4
5SymbolTable symbol_table = {{{{0},0}},MAXSYMBOLS,0};
6
7void addVar(const char name[], int type) {
8 int count;
9 for (count = 0; count < symbol_table.size; count++) {
10 if (!strcmp(symbol_table.entries[count].name, name)) {
11 printf("semantic error, redefinition of variable %s near line %d\n", name,
12 lineno);
13 return;
14 }
15 }
16 if (++symbol_table.size > symbol_table.maxsize) {
17 printf("too many variables near line %d\n", lineno);
18 exit(1);
19 }
20 strcpy(symbol_table.entries[symbol_table.size - 1].name, name);
21 symbol_table.entries[symbol_table.size - 1].type = type;
22}
23
24void lookup(const char name[]) {
25 int count;
26
27 for (count = 0; count < symbol_table.size; count++) {
28 if (!strcmp(symbol_table.entries[count].name, name)) {
29 return;
30 }
31 printf("No definition of the variable %s near line %d\n", name,
32 lineno);
33 }
34}
35
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 @@
1#ifndef __SYMBOLTABLE_H__
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#define MAXNAME 32
8#define INT 0
9#define CHAR 1
10#define MAXSYMBOLS 256
11
12
13typedef struct {
14 char name[MAXNAME];
15 int type;
16} STentry;
17
18
19typedef struct {
20 STentry entries[MAXSYMBOLS];
21 int maxsize;
22 int size;
23} SymbolTable;
24
25
26void addVar(const char name[], int type);
27void lookup(const char name[]);
28void display_table();
29
30#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;
18"/*" { BEGIN COMMENT; } 18"/*" { BEGIN COMMENT; }
19"&&" { return AND; } 19"&&" { return AND; }
20"||" { return OR; } 20"||" { return OR; }
21"*"|"/"|% { return DIVSTAR; } 21"*"|"/"|% { yylval.divstar=yytext[0]; return DIVSTAR; }
22"+"|- { return ADDSUB; } 22"+"|- { yylval.addsub=yytext[0]; return ADDSUB; }
23"<"|"<="|">"|">=" { return ORDER; } 23"<"|"<="|">"|">=" { strcpy(yylval.comp, yytext); return ORDER; }
24==|!= { return EQ; } 24==|!= { strcpy(yylval.comp, yytext); return EQ; }
25int { return TYPE; } 25entier { strcpy(yylval.type, yytext); return TYPE; }
26char { return TYPE; } 26caractere { strcpy(yylval.type, yytext); return TYPE; }
27void { return VOID; } 27void { return VOID; }
28const { return CONST; } 28const { return CONST; }
29if { return IF; } 29if { return IF; }
@@ -33,9 +33,16 @@ return { return RETURN; }
33print { return PRINT; } 33print { return PRINT; }
34readc { return READC; } 34readc { return READC; }
35reade { return READE; } 35reade { return READE; }
36[a-zA-Z_][a-zA-Z0-9_]* { return IDENT; } 36[a-zA-Z_][a-zA-Z0-9_]* { strcpy(yylval.ident, yytext); return IDENT; }
37[0-9]+ { return NUM; } 37[0-9]+ { sscanf(yytext, "%d", &(yylval.num)); return NUM; }
38'\\?.' { return CARACTERE; } 38'\\?.' { if (strlen(yytext)==3)
39 yylval.caractere=yytext[1];
40 else switch(yytext[2]) {
41 case 'n': yylval.caractere='\n'; break;
42 case 't': yylval.caractere='\t'; break;
43 case '\'': yylval.caractere='\''; break;
44 }
45 return CARACTERE; }
39. { return yytext[0]; } 46. { return yytext[0]; }
40<COMMENT>"*/" { BEGIN INITIAL; } 47<COMMENT>"*/" { BEGIN INITIAL; }
41<COMMENT>\n { lineno++; } 48<COMMENT>\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 @@
5 */ 5 */
6 6
7#include <stdio.h> 7#include <stdio.h>
8#include "symboltable.h"
8 9
9extern int lineno; 10extern int lineno;
10int yylex(); 11int yylex();
@@ -12,12 +13,25 @@ void yyerror(char *);
12 13
13%} 14%}
14 15
15%token CARACTERE NUM IDENT 16%union {
16%token ADDSUB DIVSTAR 17 char caractere;
17%token ORDER EQ OR AND 18 int num;
18%token IF WHILE RETURN 19 char ident[64];
19%token CONST VOID TYPE 20 char type[32];
20%token PRINT READC READE 21 char comp[3];
22 char addsub;
23 char divstar;
24}
25%token <caractere> CARACTERE
26%token <num> NUM
27%token <ident> IDENT
28%token <comp> ORDER EQ
29%token <addsub> ADDSUB
30%token <divstar> DIVSTAR
31%token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE
32%token <type> TYPE
33
34%type <num> Exp EB TB FB M E T F LValue
21 35
22%left ',' 36%left ','
23%precedence ')' 37%precedence ')'
@@ -122,17 +136,17 @@ T:
122 | F 136 | F
123 ; 137 ;
124F: 138F:
125 ADDSUB F 139 ADDSUB F {$$ = $2;} //on fait remonter le type
126 | '!' F 140 | '!' F {$$ = $2;}
127 | '(' Exp ')' 141 | '(' Exp ')' {$$ = $2;}
128 | LValue 142 | LValue //Il faut trouver le type de LValue
129 | NUM 143 | NUM {$$ = INT;} // on stocke les types pour l'analyse sémantique
130 | CARACTERE 144 | CARACTERE {$$ = CHAR;}
131 | IDENT '(' Arguments ')' 145 | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement
132 ; 146 ;
133LValue: 147LValue:
134 IDENT 148 IDENT {lookup($<ident>1);}
135 | IDENT '[' Exp ']' 149 | IDENT '[' Exp ']' {lookup($<ident>1);}
136 ; 150 ;
137Arguments: 151Arguments:
138 ListExp 152 ListExp