From: Nick Bowler Date: Thu, 20 Jul 2023 01:08:03 +0000 (-0400) Subject: libcdecl: Combine array length parser rules. X-Git-Tag: v1.3~108 X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/commitdiff_plain/b126a7b2d76bffbe16dd439b8049898442191536 libcdecl: Combine array length parser rules. Some of the parser rules to handle incomplete array types versus arrays with explicit length (not VLAs) are duplicated between the regular and english parsers. As this is the same in both cases we can use a new symbol and drop some duplicate actions to simplify things a bit. --- diff --git a/src/parse.y b/src/parse.y index b57428d..918cbd0 100644 --- a/src/parse.y +++ b/src/parse.y @@ -239,6 +239,7 @@ static struct cdecl *insert_identifier(struct cdecl *decl, char *ident) %token T_VLA "variable-length" %type vla_ident +%type array_length %type varargs %type declspec_simple qualifier_simple %type typespec_simple typespec_tagged @@ -377,10 +378,7 @@ vla_ident: T_IDENT | T_ASTERISK { strcpy($$, ""); } -array: T_LBRACKET T_UINT T_RBRACKET { - if ($2 == 0) - FAIL(_("array length must be positive")); - +array: T_LBRACKET array_length T_RBRACKET { ALLOC_STRUCT($$, struct cdecl_declarator, .type = CDECL_DECL_ARRAY, .u.array.length = $2); @@ -388,9 +386,6 @@ array: T_LBRACKET T_UINT T_RBRACKET { ALLOC_STRUCT($$, struct cdecl_declarator, .type = CDECL_DECL_ARRAY, .u.array.vla = $2); -} | T_LBRACKET T_RBRACKET { - ALLOC_STRUCT($$, struct cdecl_declarator, - .type = CDECL_DECL_ARRAY); } parameter: declspecs declarator { @@ -563,17 +558,16 @@ english_array: T_VLA T_ARRAY english_vla T_OF { ALLOC_STRUCT($$, struct cdecl_declarator, .type = CDECL_DECL_ARRAY, .u.array.vla = $3); -} | T_ARRAY T_UINT T_OF { - if ($2 == 0) - FAIL(_("array length must be positive")); - +} | T_ARRAY array_length T_OF { ALLOC_STRUCT($$, struct cdecl_declarator, .type = CDECL_DECL_ARRAY, .u.array.length = $2); -} | T_ARRAY T_OF { - ALLOC_STRUCT($$, struct cdecl_declarator, - .type = CDECL_DECL_ARRAY, - .u.array.length = 0); +} + +array_length: { $$ = 0; } +array_length: T_UINT { + if (!($$ = $1)) + FAIL(_("array length must be positive")); } english_vla: T_IDENT | {