From 38771494bada2b71a5e3a320938e6a259c1e208c Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Wed, 6 Jun 2018 03:06:34 +0200 Subject: Fixing chain declarator and array conflict --- res/test_add.tpc | 5 ++--- res/test_global.tpc | 11 +++++------ res/test_mul.tpc | 7 ++++--- src/tpc.y | 31 ++++++++++++++++++++++--------- 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 @@ -/* test-table-symboles.tpc */ - -/* Test file for simplified translator of a declaration of variables in C */ entier a; entier main(void) { entier a,b,res; a = 2; b = 3; + res = a + b; + print(res); res = a - b; print(res); } 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 @@ -/* test-table-symboles.tpc */ - -/* Test file for simplified translator of a declaration of variables in C */ entier r1,b,s,c,r2 ; caractere letter, digit, punct; +entier i; + void calcul(void) { - entier i; - i = 3; r1=12; r2=13; - b = r1 + r2 + i; + s = 3; + + b = r1 + r2 + s; print(b); } 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 @@ -/* test-table-symboles.tpc */ - -/* Test file for simplified translator of a declaration of variables in C */ entier a; entier main(void) { entier a,b,res; a = 2; b = 3; + res = a * b; + print(res); + res = a / b; + print(res); res = a % b; print(res); } 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; static int num_while = 0; static int nb_param[255]; static int num_scope = -1; +static int offset = 0; +static int is_array = 0; static char fname[64]; -static Type type; %} %union { @@ -49,7 +50,7 @@ static Type type; %token TYPE %type Exp EB TB FB M E T F Parametres -%type LValue +%type LValue Declarateur %left ',' %precedence ')' @@ -76,16 +77,28 @@ NombreSigne: | ADDSUB NUM { $$ = $1 == '-' ? - $2 : $2; } ; DeclVars: - DeclVars TYPE Declarateurs ';' {type = $2;} + DeclVars TYPE Declarateurs ';' | ; Declarateurs: - Declarateurs ',' Declarateur -| Declarateur + Declarateurs ',' Declarateur { + if(!is_array){ + gen_declaration($3, $0, scope); + }else{ + gen_tab_declaration($3, scope, offset); + } + } +| Declarateur { + if(!is_array){ + gen_declaration($1, $0, scope); + }else{ + gen_tab_declaration($1, scope, offset); + } + } ; Declarateur: - IDENT { gen_declaration($1, type, scope);} -| IDENT '[' NUM ']' { gen_tab_declaration($1, scope, $3);} + IDENT {strcpy($$,$1);is_array = 0;} +| IDENT '[' NUM ']' {offset = $3;strcpy($$,$1);is_array=1;} ; DeclFoncts: DeclFoncts DeclFonct @@ -182,8 +195,8 @@ F: | IDENT '(' Arguments ')' { $$ = gen_function_call($1,$3); } ; LValue: - IDENT { gen_check($1, scope); } -| IDENT '[' Exp ']' { gen_check($1, scope); } + IDENT { gen_check($1, scope); strcpy($$, $1);} +| IDENT '[' Exp ']' { gen_check($1, scope); strcpy($$, $1);} ; Arguments: ListExp -- cgit v1.2.3