aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam NAILI2018-06-06 03:06:34 +0200
committerAdam NAILI2018-06-06 03:06:34 +0200
commit38771494bada2b71a5e3a320938e6a259c1e208c (patch)
treec8c14d57d981a67802d75f966267e0fd64563605
parent7ab0b07d8224280330320238c45889c460cd0bda (diff)
downloadtpc-compiler-38771494bada2b71a5e3a320938e6a259c1e208c.tar.gz
Fixing chain declarator and array conflict
-rw-r--r--res/test_add.tpc5
-rw-r--r--res/test_global.tpc11
-rw-r--r--res/test_mul.tpc7
-rw-r--r--src/tpc.y31
4 files changed, 33 insertions, 21 deletions
diff --git a/res/test_add.tpc b/res/test_add.tpc
index 2f6b9dd..848130c 100644
--- a/res/test_add.tpc
+++ b/res/test_add.tpc
@@ -1,12 +1,11 @@
1/* test-table-symboles.tpc */
2
3/* Test file for simplified translator of a declaration of variables in C */
4entier a; 1entier a;
5 2
6entier main(void) { 3entier main(void) {
7 entier a,b,res; 4 entier a,b,res;
8 a = 2; 5 a = 2;
9 b = 3; 6 b = 3;
7 res = a + b;
8 print(res);
10 res = a - b; 9 res = a - b;
11 print(res); 10 print(res);
12} 11}
diff --git a/res/test_global.tpc b/res/test_global.tpc
index 731fffb..8fda248 100644
--- a/res/test_global.tpc
+++ b/res/test_global.tpc
@@ -1,15 +1,14 @@
1/* test-table-symboles.tpc */
2
3/* Test file for simplified translator of a declaration of variables in C */
4entier r1,b,s,c,r2 ; 1entier r1,b,s,c,r2 ;
5caractere letter, digit, punct; 2caractere letter, digit, punct;
3entier i;
4
6 5
7void calcul(void) { 6void calcul(void) {
8 entier i;
9 i = 3;
10 r1=12; 7 r1=12;
11 r2=13; 8 r2=13;
12 b = r1 + r2 + i; 9 s = 3;
10
11 b = r1 + r2 + s;
13 print(b); 12 print(b);
14} 13}
15 14
diff --git a/res/test_mul.tpc b/res/test_mul.tpc
index f3b6705..62a8080 100644
--- a/res/test_mul.tpc
+++ b/res/test_mul.tpc
@@ -1,12 +1,13 @@
1/* test-table-symboles.tpc */
2
3/* Test file for simplified translator of a declaration of variables in C */
4entier a; 1entier a;
5 2
6entier main(void) { 3entier main(void) {
7 entier a,b,res; 4 entier a,b,res;
8 a = 2; 5 a = 2;
9 b = 3; 6 b = 3;
7 res = a * b;
8 print(res);
9 res = a / b;
10 print(res);
10 res = a % b; 11 res = a % b;
11 print(res); 12 print(res);
12} 13}
diff --git a/src/tpc.y b/src/tpc.y
index 50bfa5d..59fa9d1 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -26,8 +26,9 @@ static int num_if = 0;
26static int num_while = 0; 26static int num_while = 0;
27static int nb_param[255]; 27static int nb_param[255];
28static int num_scope = -1; 28static int num_scope = -1;
29static int offset = 0;
30static int is_array = 0;
29static char fname[64]; 31static char fname[64];
30static Type type;
31%} 32%}
32 33
33%union { 34%union {
@@ -49,7 +50,7 @@ static Type type;
49%token <type> TYPE 50%token <type> TYPE
50 51
51%type <num> Exp EB TB FB M E T F Parametres 52%type <num> Exp EB TB FB M E T F Parametres
52%type <ident> LValue 53%type <ident> LValue Declarateur
53 54
54%left ',' 55%left ','
55%precedence ')' 56%precedence ')'
@@ -76,16 +77,28 @@ NombreSigne:
76| ADDSUB NUM { $<num>$ = $<addsub>1 == '-' ? - $<num>2 : $<num>2; } 77| ADDSUB NUM { $<num>$ = $<addsub>1 == '-' ? - $<num>2 : $<num>2; }
77; 78;
78DeclVars: 79DeclVars:
79 DeclVars TYPE Declarateurs ';' {type = $<type>2;} 80 DeclVars TYPE Declarateurs ';'
80| 81|
81; 82;
82Declarateurs: 83Declarateurs:
83 Declarateurs ',' Declarateur 84 Declarateurs ',' Declarateur {
84| Declarateur 85 if(!is_array){
86 gen_declaration($<ident>3, $<type>0, scope);
87 }else{
88 gen_tab_declaration($<ident>3, scope, offset);
89 }
90 }
91| Declarateur {
92 if(!is_array){
93 gen_declaration($<ident>1, $<type>0, scope);
94 }else{
95 gen_tab_declaration($<ident>1, scope, offset);
96 }
97 }
85; 98;
86Declarateur: 99Declarateur:
87 IDENT { gen_declaration($<ident>1, type, scope);} 100 IDENT {strcpy($$,$1);is_array = 0;}
88| IDENT '[' NUM ']' { gen_tab_declaration($<ident>1, scope, $<num>3);} 101| IDENT '[' NUM ']' {offset = $<num>3;strcpy($$,$1);is_array=1;}
89; 102;
90DeclFoncts: 103DeclFoncts:
91 DeclFoncts DeclFonct 104 DeclFoncts DeclFonct
@@ -182,8 +195,8 @@ F:
182| IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); } 195| IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); }
183; 196;
184LValue: 197LValue:
185 IDENT { gen_check($<ident>1, scope); } 198 IDENT { gen_check($<ident>1, scope); strcpy($$, $1);}
186| IDENT '[' Exp ']' { gen_check($<ident>1, scope); } 199| IDENT '[' Exp ']' { gen_check($<ident>1, scope); strcpy($$, $1);}
187; 200;
188Arguments: 201Arguments:
189 ListExp 202 ListExp