diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/symboltable.c | 85 | ||||
-rw-r--r-- | src/symboltable.h | 10 | ||||
-rw-r--r-- | src/tpc.y | 64 |
3 files changed, 117 insertions, 42 deletions
diff --git a/src/symboltable.c b/src/symboltable.c index 3f22b44..cdbd442 100644 --- a/src/symboltable.c +++ b/src/symboltable.c | |||
@@ -2,55 +2,92 @@ | |||
2 | 2 | ||
3 | extern int lineno; /* from lexical analyser */ | 3 | extern int lineno; /* from lexical analyser */ |
4 | 4 | ||
5 | SymbolTable symbol_table = {{{{0},0}},MAXSYMBOLS,0}; | 5 | SymbolTable glo_symbol_table = {{{{0},0}},MAXSYMBOLS,0}; |
6 | SymbolTable loc_symbol_table = {{{{0},0}},MAXSYMBOLS,0}; | ||
6 | 7 | ||
7 | void addVar(const char name[], int type) { | 8 | void glo_addVar(const char name[], int type) { |
8 | int count; | 9 | int count; |
9 | for (count = 0; count < symbol_table.size; count++) { | 10 | for (count = 0; count < glo_symbol_table.size; count++) { |
10 | if (!strcmp(symbol_table.entries[count].name, name)) { | 11 | if (!strcmp(glo_symbol_table.entries[count].name, name)) { |
11 | printf("semantic error, redefinition of variable %s near line %d\n", name, | 12 | fprintf(stderr,"semantic error, redefinition of variable %s near line %d\n", name, |
12 | lineno); | 13 | lineno); |
13 | return; | 14 | return; |
14 | } | 15 | } |
15 | } | 16 | } |
16 | if (++symbol_table.size > symbol_table.maxsize) { | 17 | if (++glo_symbol_table.size > glo_symbol_table.maxsize) { |
17 | printf("too many variables near line %d\n", lineno); | 18 | fprintf(stderr,"too many variables near line %d\n", lineno); |
18 | exit(1); | 19 | exit(1); |
19 | } | 20 | } |
20 | strcpy(symbol_table.entries[symbol_table.size - 1].name, name); | 21 | strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); |
21 | symbol_table.entries[symbol_table.size - 1].type = type; | 22 | glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; |
23 | glo_symbol_table.entries[glo_symbol_table.size - 1].addr = (glo_symbol_table.size - 1)*8; | ||
22 | } | 24 | } |
23 | 25 | ||
24 | void lookup(const char name[]) { | 26 | //Verifies that the variable exists and returns the type |
27 | int glo_lookup(const char name[]) { | ||
25 | int count; | 28 | int count; |
26 | 29 | ||
27 | for (count = 0; count < symbol_table.size; count++) { | 30 | for (count = 0; count < glo_symbol_table.size; count++) { |
28 | if (!strcmp(symbol_table.entries[count].name, name)) { | 31 | if (!strcmp(glo_symbol_table.entries[count].name, name)) { |
29 | return; | 32 | return glo_symbol_table.entries[count].type; |
30 | } | 33 | } |
31 | } | 34 | } |
32 | printf("No definition of the variable %s near line %d\n", name, | 35 | fprintf(stderr,"No definition of the variable %s near line %d\n", name, |
33 | lineno); | 36 | lineno); |
37 | return -1; | ||
34 | } | 38 | } |
35 | 39 | ||
36 | void display_table(){ | 40 | |
41 | void glo_display_table(){ | ||
37 | int count; | 42 | int count; |
38 | for (count=0;count<symbol_table.size;count++) { | 43 | for (count=0;count<glo_symbol_table.size;count++) { |
39 | if(symbol_table.entries[count].type == INT) | 44 | if(glo_symbol_table.entries[count].type == INT) |
40 | printf("entier: %s, pos: %d \n", symbol_table.entries[count].name, symbol_table.entries[count].addr); | 45 | printf("entier: %s, pos: %d \n", glo_symbol_table.entries[count].name, glo_symbol_table.entries[count].addr); |
41 | else | 46 | else |
42 | printf("caractere: %s, pos: %d \n", symbol_table.entries[count].name, symbol_table.entries[count].addr); | 47 | printf("caractere: %s, pos: %d \n", glo_symbol_table.entries[count].name, glo_symbol_table.entries[count].addr); |
43 | } | 48 | } |
44 | printf("\n"); | 49 | printf("\n"); |
45 | } | 50 | } |
46 | 51 | ||
47 | int get_type(const char name[]){ | ||
48 | int count; | ||
49 | 52 | ||
50 | for (count = 0; count < symbol_table.size; count++) { | 53 | void loc_addVar(const char name[], int type) { |
51 | if (!strcmp(symbol_table.entries[count].name, name)) { | 54 | int count; |
52 | return symbol_table.entries[count].type; | 55 | for (count = 0; count < loc_symbol_table.size; count++) { |
56 | if (!strcmp(loc_symbol_table.entries[count].name, name)) { | ||
57 | fprintf(stderr,"semantic error, redefinition of variable %s near line %d\n", name, | ||
58 | lineno); | ||
59 | return; | ||
53 | } | 60 | } |
54 | } | 61 | } |
62 | if (++glo_symbol_table.size > glo_symbol_table.maxsize) { | ||
63 | fprintf(stderr,"too many variables near line %d\n", lineno); | ||
64 | exit(1); | ||
65 | } | ||
66 | strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); | ||
67 | glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; | ||
68 | glo_symbol_table.entries[glo_symbol_table.size - 1].addr = (glo_symbol_table.size - 1)*8; | ||
69 | } | ||
70 | |||
71 | |||
72 | int loc_lookup(const char name[]) { | ||
73 | int count; | ||
74 | |||
75 | for (count = 0; count < loc_symbol_table.size; count++) { | ||
76 | if (!strcmp(loc_symbol_table.entries[count].name, name)) { | ||
77 | return loc_symbol_table.entries[count].type; | ||
78 | } | ||
79 | } | ||
80 | fprintf(stderr,"No definition of the variable %s near line %d\n", name, | ||
81 | lineno); | ||
55 | return -1; | 82 | return -1; |
83 | } | ||
84 | void loc_display_table(){ | ||
85 | int count; | ||
86 | for (count=0;count<loc_symbol_table.size;count++) { | ||
87 | if(loc_symbol_table.entries[count].type == INT) | ||
88 | printf("entier: %s, pos: %d \n", loc_symbol_table.entries[count].name, loc_symbol_table.entries[count].addr); | ||
89 | else | ||
90 | printf("caractere: %s, pos: %d \n", loc_symbol_table.entries[count].name, loc_symbol_table.entries[count].addr); | ||
91 | } | ||
92 | printf("\n"); | ||
56 | } \ No newline at end of file | 93 | } \ No newline at end of file |
diff --git a/src/symboltable.h b/src/symboltable.h index 02e47a4..bd7a373 100644 --- a/src/symboltable.h +++ b/src/symboltable.h | |||
@@ -24,8 +24,10 @@ typedef struct { | |||
24 | } SymbolTable; | 24 | } SymbolTable; |
25 | 25 | ||
26 | 26 | ||
27 | void addVar(const char name[], int type); | 27 | void glo_addVar(const char name[], int type); |
28 | void lookup(const char name[]); | 28 | int glo_lookup(const char name[]); |
29 | void display_table(); | 29 | void glo_display_table(); |
30 | int get_type(const char name[]); | 30 | void loc_addVar(const char name[], int type); |
31 | int loc_lookup(const char name[]); | ||
32 | void loc_display_table(); | ||
31 | #endif \ No newline at end of file | 33 | #endif \ No newline at end of file |
@@ -11,6 +11,9 @@ extern int lineno; | |||
11 | int yylex(); | 11 | int yylex(); |
12 | void yyerror(char *); | 12 | void yyerror(char *); |
13 | 13 | ||
14 | #define GLOBAL 0 | ||
15 | #define LOCAL 1 | ||
16 | static int status = GLOBAL; | ||
14 | %} | 17 | %} |
15 | 18 | ||
16 | %union { | 19 | %union { |
@@ -31,15 +34,48 @@ void yyerror(char *); | |||
31 | %token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE | 34 | %token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE |
32 | %token <type> TYPE | 35 | %token <type> TYPE |
33 | 36 | ||
34 | %type <num> Exp EB TB FB M E T F LValue | 37 | %type <num> Exp EB TB FB M E T F |
38 | %type <ident> LValue | ||
35 | 39 | ||
36 | %left ',' | 40 | %left ',' |
37 | %precedence ')' | 41 | %precedence ')' |
38 | %precedence ELSE | 42 | %precedence ELSE |
39 | 43 | ||
40 | %% | 44 | %% |
41 | Prog: | 45 | Prog:{printf("section .data\n"); |
42 | DeclConsts DeclVars DeclFoncts {display_table();} | 46 | printf("format_entier db \"%%d \n\", 10,0"); |
47 | printf("section .bss\nsection .text\nglobal _start\n"); | ||
48 | printf("print:\n"); | ||
49 | printf("push rbp\n"); | ||
50 | printf("mov rbp, rsp\n"); | ||
51 | |||
52 | printf("push rax\n"); | ||
53 | printf("push rcx\n"); | ||
54 | printf("push rdx\n"); | ||
55 | printf("push rdi\n"); | ||
56 | printf("push rsi\n"); | ||
57 | printf("push r8\n"); | ||
58 | |||
59 | printf("mov r8, rdx \n"); | ||
60 | printf("mov rcx, rcx\n"); | ||
61 | printf("mov rdx, rbx\n"); | ||
62 | printf(" mov rdi, format_entier\n"); | ||
63 | printf(" mov rax, 0\n"); | ||
64 | printf(" call printf WRT ..plt\n"); | ||
65 | |||
66 | printf("pop r8\n"); | ||
67 | printf("pop rsi\n"); | ||
68 | printf("pop rdi\n"); | ||
69 | printf("pop rdx\n"); | ||
70 | printf("pop rcx\n"); | ||
71 | printf("pop rax\n"); | ||
72 | |||
73 | printf("pop rbp\n"); | ||
74 | printf("ret\n"); | ||
75 | } | ||
76 | DeclConsts DeclVars DeclFoncts { | ||
77 | |||
78 | glo_display_table();} | ||
43 | ; | 79 | ; |
44 | DeclConsts: | 80 | DeclConsts: |
45 | DeclConsts CONST ListConst ';' | 81 | DeclConsts CONST ListConst ';' |
@@ -62,8 +98,8 @@ DeclVars: | |||
62 | | | 98 | | |
63 | ; | 99 | ; |
64 | Declarateurs: | 100 | Declarateurs: |
65 | Declarateurs ',' Declarateur {addVar($<ident>3, $<type>0);} | 101 | Declarateurs ',' Declarateur {glo_addVar($<ident>3, $<type>0);} |
66 | | Declarateur {addVar($<ident>1, $<type>0);} | 102 | | Declarateur {glo_addVar($<ident>1, $<type>0);} |
67 | ; | 103 | ; |
68 | Declarateur: | 104 | Declarateur: |
69 | IDENT | 105 | IDENT |
@@ -74,7 +110,7 @@ DeclFoncts: | |||
74 | | DeclFonct | 110 | | DeclFonct |
75 | ; | 111 | ; |
76 | DeclFonct: | 112 | DeclFonct: |
77 | EnTeteFonct Corps | 113 | EnTeteFonct {status = LOCAL;} Corps {status = GLOBAL;} |
78 | ; | 114 | ; |
79 | EnTeteFonct: | 115 | EnTeteFonct: |
80 | TYPE IDENT '(' Parametres ')' | 116 | TYPE IDENT '(' Parametres ')' |
@@ -85,8 +121,8 @@ Parametres: | |||
85 | | ListTypVar | 121 | | ListTypVar |
86 | ; | 122 | ; |
87 | ListTypVar: | 123 | ListTypVar: |
88 | ListTypVar ',' TYPE IDENT {addVar($<ident>4, $<type>3);} | 124 | ListTypVar ',' TYPE IDENT {glo_addVar($<ident>4, $<type>3);} |
89 | | TYPE IDENT {addVar($<ident>2, $<type>1);} | 125 | | TYPE IDENT {glo_addVar($<ident>2, $<type>1);} |
90 | ; | 126 | ; |
91 | Corps: | 127 | Corps: |
92 | '{' DeclConsts DeclVars SuiteInstr '}' | 128 | '{' DeclConsts DeclVars SuiteInstr '}' |
@@ -99,8 +135,8 @@ Instr: | |||
99 | | ';' | 135 | | ';' |
100 | | RETURN Exp ';' | 136 | | RETURN Exp ';' |
101 | | RETURN ';' | 137 | | RETURN ';' |
102 | | READE '(' IDENT ')' ';' {lookup($<ident>3);} | 138 |