diff options
author | Adam NAILI | 2018-04-22 14:49:42 +0200 |
---|---|---|
committer | Adam NAILI | 2018-04-22 14:49:42 +0200 |
commit | 40f423a4b3b1e64e8424ab239cc14ecc5077640f (patch) | |
tree | c7da7c2eed97d62cc8ab2e003293935cda5a93dc | |
parent | c0802bc17f856546b95a5b51252f7a35d9e1ab10 (diff) | |
download | tpc-compiler-40f423a4b3b1e64e8424ab239cc14ecc5077640f.tar.gz |
Beginning of the symbol table implementation
-rw-r--r-- | makefile | 10 | ||||
-rw-r--r-- | res/ko_sample.tpc | 2 | ||||
-rw-r--r-- | res/ok_sample.tpc | 4 | ||||
-rw-r--r-- | res/test-table-symboles.tpc | 13 | ||||
-rw-r--r-- | src/symboltable.c | 35 | ||||
-rw-r--r-- | src/symboltable.h | 30 | ||||
-rw-r--r-- | src/tpc.lex | 25 | ||||
-rw-r--r-- | src/tpc.y | 44 |
8 files changed, 133 insertions, 30 deletions
@@ -10,6 +10,7 @@ DOC_DIR := doc | |||
10 | LEX_SRC := tpc.lex | 10 | LEX_SRC := tpc.lex |
11 | YACC_SRC := tpc.y | 11 | YACC_SRC := tpc.y |
12 | PDF_SRC := rapport.md | 12 | PDF_SRC := rapport.md |
13 | ST_SRC := symboltable | ||
13 | 14 | ||
14 | # INTERMEDIATE | 15 | # INTERMEDIATE |
15 | LEX_GEN := tpc.yy | 16 | LEX_GEN := tpc.yy |
@@ -34,8 +35,8 @@ all: clean $(OUT_DIR)/$(COMPIL_BIN) | |||
34 | $(OUT_DIR)/$(LEX_GEN).c: $(SRC_DIR)/$(LEX_SRC) | 35 | $(OUT_DIR)/$(LEX_GEN).c: $(SRC_DIR)/$(LEX_SRC) |
35 | $(LEX) -o $@ $^ | 36 | $(LEX) -o $@ $^ |
36 | 37 | ||
37 | $(OUT_DIR)/$(YACC_GEN).c $(OUT_DIR)/$(YACC_GEN).h: $(SRC_DIR)/$(YACC_SRC) | 38 | $(OUT_DIR)/$(YACC_GEN).c $(OUT_DIR)/$(YACC_GEN).h: $(SRC_DIR)/$(YACC_SRC) $(SRC_DIR)/$(ST_SRC).h |
38 | $(YACC) --output=$(OUT_DIR)/$(YACC_GEN).c --defines=$(OUT_DIR)/$(YACC_GEN).h -v $^ | 39 | $(YACC) --output=$(OUT_DIR)/$(YACC_GEN).c --defines=$(OUT_DIR)/$(YACC_GEN).h -v $< |
39 | 40 | ||
40 | $(OUT_DIR)/$(LEX_GEN).o: $(OUT_DIR)/$(LEX_GEN).c $(OUT_DIR)/$(YACC_GEN).h | 41 | $(OUT_DIR)/$(LEX_GEN).o: $(OUT_DIR)/$(LEX_GEN).c $(OUT_DIR)/$(YACC_GEN).h |
41 | $(CC) -o $@ -c $< $(IFLAGS) $(LFLAGS) $(CFLAGS) | 42 | $(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 | |||
43 | $(OUT_DIR)/$(YACC_GEN).o: $(OUT_DIR)/$(YACC_GEN).c | 44 | $(OUT_DIR)/$(YACC_GEN).o: $(OUT_DIR)/$(YACC_GEN).c |
44 | $(CC) -o $@ -c $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) | 45 | $(CC) -o $@ -c $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) |
45 | 46 | ||
46 | $(OUT_DIR)/$(COMPIL_BIN): $(OUT_DIR)/$(LEX_GEN).o $(OUT_DIR)/$(YACC_GEN).o | 47 | $(OUT_DIR)/$(ST_SRC).o: $(SRC_DIR)/$(ST_SRC).c |
48 | $(CC) -o $@ -c $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) | ||
49 | |||
50 | $(OUT_DIR)/$(COMPIL_BIN): $(OUT_DIR)/$(LEX_GEN).o $(OUT_DIR)/$(YACC_GEN).o $(OUT_DIR)/$(ST_SRC).o | ||
47 | $(CC) -o $@ $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) | 51 | $(CC) -o $@ $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) |
48 | 52 | ||
49 | $(OUT_DIR)/$(REPORT_PDF): $(DOC_DIR)/$(PDF_SRC) | 53 | $(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 @@ | |||
6 | 6 | ||
7 | const special = 'b'; | 7 | const special = 'b'; |
8 | 8 | ||
9 | char funky_func(int arg) { | 9 | caractere funky_func(entier arg) { |
10 | return !arg; | 10 | return !arg; |
11 | } | 11 | } |
12 | 12 | ||
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 @@ | |||
6 | 6 | ||
7 | const special = 'b'; | 7 | const special = 'b'; |
8 | 8 | ||
9 | char funky_func(int arg) { | 9 | caractere funky_func(entier arg) { |
10 | return !arg; | 10 | return !arg; |
11 | } | 11 | } |
12 | 12 | ||
13 | void main(void) { | 13 | void main(void) { |
14 | char ret; | 14 | caractere ret; |
15 | ret = 1 + funky_func(0) * 2; | 15 | ret = 1 + funky_func(0) * 2; |
16 | 16 | ||
17 | while (ret) { | 17 | 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 @@ | |||
1 | /* test-table-symboles.tpc */ | ||
2 | |||
3 | /* Test file for simplified translator of a declaration of variables in C */ | ||
4 | entier r1,b,s,c,r2 ; | ||
5 | caractere letter, digit, punct; | ||
6 | |||
7 | entier main(void) { | ||
8 | r1=12; | ||
9 | r2=24; | ||
10 | b=r1+r2; | ||
11 | punct='.'; | ||
12 | return b; | ||
13 | } | ||
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 | |||
3 | extern int lineno; /* from lexical analyser */ | ||
4 | |||
5 | SymbolTable symbol_table = {{{{0},0}},MAXSYMBOLS,0}; | ||
6 | |||
7 | void 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 | |||
24 | void 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 | |||
13 | typedef struct { | ||
14 | char name[MAXNAME]; | ||
15 | int type; | ||
16 | } STentry; | ||
17 | |||
18 | |||
19 | typedef struct { | ||
20 | STentry entries[MAXSYMBOLS]; | ||
21 | int maxsize; | ||
22 | int size; | ||
23 | } SymbolTable; | ||
24 | |||
25 | |||
26 | void addVar(const char name[], int type); | ||
27 | void lookup(const char name[]); | ||
28 | void 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; } |
25 | int { return TYPE; } | 25 | entier { strcpy(yylval.type, yytext); return TYPE; } |
26 | char { return TYPE; } | 26 | caractere { strcpy(yylval.type, yytext); return TYPE; } |
27 | void { return VOID; } | 27 | void { return VOID; } |
28 | const { return CONST; } | 28 | const { return CONST; } |
29 | if { return IF; } | 29 | if { return IF; } |
@@ -33,9 +33,16 @@ return { return RETURN; } | |||
33 | print { return PRINT; } | 33 | print { return PRINT; } |
34 | readc { return READC; } | 34 | readc { return READC; } |
35 | reade { return READE; } | 35 | reade { 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++; } |
@@ -5,6 +5,7 @@ | |||
5 | */ | 5 | */ |
6 | 6 | ||
7 | #include <stdio.h> | 7 | #include <stdio.h> |
8 | #include "symboltable.h" | ||
8 | 9 | ||
9 | extern int lineno; | 10 | extern int lineno; |
10 | int yylex(); | 11 | int 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: | |||