]> git.draconx.ca Git - cdecl99.git/blobdiff - src/parse.y
Make an explicit null declarator type.
[cdecl99.git] / src / parse.y
index 7b73eae26d9a053066b2850fbc03513b18d77a4f..7d4c3dc0c889f88469c4905f9f8cf36fa685b33d 100644 (file)
@@ -22,6 +22,8 @@
 %locations
 
 %{
+#include <assert.h>
+
 #include "scan.h"
 #include "cdecl.h"
 
@@ -77,16 +79,22 @@ static void free_declarator(struct cdecl_declarator *x)
        while (x) {
                p = x->next;
                switch (x->type) {
+               case CDECL_DECL_NULL:
+                       break;
                case CDECL_DECL_IDENT:
                        free(x->u.ident);
                        break;
                case CDECL_DECL_POINTER:
                        free_declspec(x->u.pointer.qualifiers);
-                       free_declarator(x->u.pointer.declarator);
+                       break;
+               case CDECL_DECL_ARRAY:
+                       free(x->u.array.vla);
                        break;
                default:
-                       abort();
+                       assert(0);
                }
+
+               free_declarator(x->child);
                free(x);
                x = p;
        }
@@ -114,6 +122,8 @@ void cdecl_free(struct cdecl *decl)
 %token T_LEX_ERROR
 
 %token <strval> T_IDENT "identifier"
+%token <uintval> T_UINT "integer constant"
+
 %token T_SEMICOLON ";"
 %token T_ASTERISK  "*"
 %token T_LPAREN    "("
@@ -150,11 +160,12 @@ void cdecl_free(struct cdecl *decl)
 %token T_UNION    "union"
 %token T_ENUM     "enum"
 
+%type <strval>     vla_ident
 %type <uintval>    declspec_simple typespec_simple qualifier_simple
 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
-%type <declspec>   qualifier qualifiers pointer
+%type <declspec>   qualifier qualifiers
 %type <declspec>   declspecs declspecs_noid
-%type <declarator> declarator declarators
+%type <declarator> direct_declarator declarator declarators pointer array
 %type <decl>       declaration
 
 %%
@@ -247,17 +258,51 @@ typespec: typespec_noid | T_STRUCT T_IDENT {
 
 declspec_noid: declspec_notype | typespec_noid
 
-pointer: T_ASTERISK qualifiers { $$ = $2; }
+vla_ident: T_IDENT | T_ASTERISK {
+       ALLOC($$, sizeof "");
+       strcpy($$, "");
+}
+
+array: T_LBRACKET T_UINT T_RBRACKET {
+       if ($2 == 0)
+               FAIL("array length must be positive");
 
-declarator: T_IDENT {
        ALLOC_STRUCT($$, struct cdecl_declarator,
-               .type = CDECL_DECL_IDENT,
-               .u.ident = $1);
-} | pointer declarator {
+               .type = CDECL_DECL_ARRAY,
+               .u.array.length = $2);
+} | T_LBRACKET vla_ident 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);
+}
+
+pointer: T_ASTERISK qualifiers direct_declarator {
+       ALLOC_STRUCT($$, struct cdecl_declarator,
+               .type = CDECL_DECL_POINTER,
+               .u.pointer.qualifiers = $2,
+               .child = $3);
+} | T_ASTERISK qualifiers pointer {
        ALLOC_STRUCT($$, struct cdecl_declarator,
                .type = CDECL_DECL_POINTER,
-               .u.pointer.qualifiers = $1,
-               .u.pointer.declarator = $2);
+               .u.pointer.qualifiers = $2,
+               .child = $3);
+}
+
+declarator: direct_declarator | pointer
+
+direct_declarator: {
+       ALLOC_STRUCT($$, struct cdecl_declarator,
+               .type = CDECL_DECL_NULL);
+} | T_IDENT {
+       ALLOC_STRUCT($$, struct cdecl_declarator,
+               .type = CDECL_DECL_IDENT,
+               .u.ident = $1);
+} | direct_declarator array {
+       $$ = $2;
+       $$->child = $1;
 } | T_LPAREN declarator T_RPAREN {
        $$ = $2;
 };