From 77559b13dc6200a83808e5a592a67463b0e89598 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Mon, 27 Mar 2017 16:47:29 +0100 Subject: Changing declarations --- c_compiler/src/c_parser.y | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'c_compiler/src/c_parser.y') diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 81293ba..feb5d0b 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -127,8 +127,8 @@ Parameter: DeclarationSpecifierList Declarator $$->setType(tmp_type); delete $1; } - | DeclarationSpecifierList { $$ = new Declaration(); delete $1; } - | DeclarationSpecifierList T_MULT { $$ = new Declaration(); delete $2; delete $1; } + | DeclarationSpecifierList { $$ = new IdentifierDeclaration(); delete $1; } + | DeclarationSpecifierList T_MULT { $$ = new IdentifierDeclaration(); delete $2; delete $1; } ; // Declaration @@ -202,8 +202,9 @@ Declarator: DirectDeclarator { $$ = $1; } Pointer: T_MULT { $$ = new Pointer(); delete $1; } - | T_MULT Pointer { $$ = $2; delete $1; } - | T_MULT TypeQualifierList Pointer { $$ = $3; delete $1; delete $2; } + | T_MULT Pointer { $$ = $2; delete $1; $$->type(new Pointer()); } + | T_MULT TypeQualifierList Pointer + { $$ = $3; delete $1; delete $2; $$->type(new Pointer()); } ; TypeQualifierList: @@ -217,23 +218,36 @@ TypeQualifier: ; DirectDeclarator: - T_IDENTIFIER { $$ = new Declaration(*$1); delete $1; } + T_IDENTIFIER { $$ = new IdentifierDeclaration(*$1); delete $1; } | T_LRB Declarator T_RRB { $$ = $2; } | DirectDeclarator T_LSB ConditionalExpression T_RSB { - $$ = new ArrayDeclaration($1->getId(), $1->getInitializer(), $3->constantFold()); + $$ = new ArrayDeclaration($1, $1->getInitializer(), $3->constantFold()); TypePtr tmp_ptr = std::make_shared($3->constantFold()); - $$->setType(tmp_ptr); + if($$->getType() == nullptr) + $$->setType(tmp_ptr); + else + $$->getType()->type(tmp_ptr); + } + + | DirectDeclarator T_LSB T_RSB + { + $$ = new ArrayDeclaration($1, $1->getInitializer()); + TypePtr tmp_ptr = std::make_shared(0); + if($$->getType() == nullptr) + $$->setType(tmp_ptr); + else + $$->getType()->type(tmp_ptr); } - | DirectDeclarator T_LSB T_RSB { $$ = $1; } + | DirectDeclarator T_LRB T_RRB { $$ = $1; $$->setExternDeclaration(true); } | DirectDeclarator T_LRB ParameterTypeList T_RRB { $1->linkDeclaration($3); $$ = $1; $$->setExternDeclaration(true); } | DirectDeclarator T_LRB IdentifierList T_RRB { $$ = $1; $$->setExternDeclaration(true); } ; -IdentifierList: T_IDENTIFIER { $$ = new Declaration(); } - | IdentifierList T_CMA T_IDENTIFIER { $$ = new Declaration(); } +IdentifierList: T_IDENTIFIER { $$ = new IdentifierDeclaration(); } + | IdentifierList T_CMA T_IDENTIFIER { $$ = new IdentifierDeclaration(); } Initializer: AssignmentExpression { $$ = $1; } | T_LCB InitializerList T_RCB { $$ = $2; } -- cgit