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 --- src/tpc.y | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'src/tpc.y') 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