%code top {
/*
* Parser for C declarations.
* Copyright © 2011 Nick Bowler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
}
%parse-param {yyscan_t scanner}
%parse-param {struct cdecl **out}
%lex-param {yyscan_t scanner}
%define api.pure
%error-verbose
%locations
%{
#include
#include
#include "scan.h"
#include "cdecl.h"
#define FAIL(msg) do { \
yyerror(&yylloc, NULL, NULL, msg); \
YYERROR; \
} while (0)
#define ALLOC(ptr, size) do { \
(ptr) = malloc(size); \
if (!(ptr)) \
FAIL("failed to allocate memory"); \
} while (0)
#define ALLOC_STRUCT(ptr, type, ...) do { \
ALLOC(ptr, sizeof (type)); \
*(ptr) = (type) { __VA_ARGS__ }; \
} while (0)
%}
%code requires {
#include
}
%code provides {
void yyerror(YYLTYPE *, struct cdecl **, void *, const char *);
int yyparse(void *scanner, struct cdecl **out);
}
%union {
uintmax_t uintval;
_Bool boolval;
char *strval;
struct cdecl_declspec *declspec;
struct cdecl_declarator *declarator;
struct cdecl *decl;
}
%{
static void free_decl(struct cdecl *);
static void free_declspec(struct cdecl_declspec *x)
{
struct cdecl_declspec *p;
while (x) {
p = x->next;
free(x->ident);
free(x);
x = p;
}
}
static void free_declarator(struct cdecl_declarator *x)
{
struct cdecl_declarator *p;
while (x) {
p = x->child;
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);
break;
case CDECL_DECL_ARRAY:
free(x->u.array.vla);
break;
case CDECL_DECL_FUNCTION:
free_decl(x->u.function.parameters);
break;
default:
assert(0);
}
free(x);
x = p;
}
}
static void free_decl(struct cdecl *x)
{
struct cdecl *p;
while (x) {
p = x->next;
/* The specifiers may be shared by an entire chain. */
if (!p || p->specifiers != x->specifiers)
free_declspec(x->specifiers);
free_declarator(x->declarators);
free(x);
x = p;
}
}
void cdecl_free(struct cdecl *decl)
{
free_decl(decl);
}
%}
%destructor { free($$); }
%destructor { free_declspec($$); }
%destructor { free_declarator($$); }
%destructor { free_decl($$); }
%token T_LEX_ERROR
%token T_IDENT "identifier"
%token T_UINT "integer constant"
%token T_SEMICOLON ";"
%token T_ASTERISK "*"
%token T_LPAREN "("
%token T_RPAREN ")"
%token T_LBRACKET "["
%token T_RBRACKET "]"
%token T_COMMA ","
%token T_ELLIPSIS "."
%token T_TYPEDEF "typedef"
%token T_EXTERN "extern"
%token T_STATIC "static"
%token T_AUTO "auto"
%token T_REGISTER "register"
%token T_INLINE "inline"
%token T_RESTRICT "restrict"
%token T_VOLATILE "volatile"
%token T_CONST "const"
%token T_VOID "void"
%token T_CHAR "char"
%token T_SHORT "short"
%token T_INT "int"
%token T_LONG "long"
%token T_FLOAT "float"
%token T_DOUBLE "double"
%token T_SIGNED "signed"
%token T_UNSIGNED "unsigned"
%token T_BOOL "_Bool"
%token T_COMPLEX "_Complex"
%token T_STRUCT "struct"
%token T_UNION "union"
%token T_ENUM "enum"
%type vla_ident
%type varargs
%type declspec_simple typespec_simple qualifier_simple
%type declspec_notype declspec_noid typespec_noid typespec
%type qualifier qualifiers
%type declspecs declspecs_noid
%type direct_declarator declarator pointer array parens postfix
%type direct_declarator_ish declarator_ish parameter_type_list
%type declaration declarators declarator_wrap
%type parameter parameters
%%
input: declaration {
*out = $1;
};
declaration: declspecs declarators T_SEMICOLON {
$$ = $2;
for (struct cdecl *i = $$; i; i = i->next)
i->specifiers = $1;
};
declspecs: declspec_notype declspecs {
$$ = $1;
$$->next = $2;
} | typespec declspecs_noid {
$$ = $1;
$$->next = $2;
}
declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
$$ = $1;
$$->next = $2;
}
qualifiers: { $$ = NULL; } | qualifiers qualifier {
$$ = $2;
$$->next = $1;
}
declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
$$ = $1;
$$->next = $3;
}
declarator_wrap: declarator {
ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
}
declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
| T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
| T_EXTERN { $$ = CDECL_STOR_EXTERN; }
| T_STATIC { $$ = CDECL_STOR_STATIC; }
| T_REGISTER { $$ = CDECL_STOR_REGISTER; }
| T_INLINE { $$ = CDECL_FUNC_INLINE; }
typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
| T_CHAR { $$ = CDECL_TYPE_CHAR; }
| T_SHORT { $$ = CDECL_TYPE_SHORT; }
| T_INT { $$ = CDECL_TYPE_INT; }
| T_LONG { $$ = CDECL_TYPE_LONG; }
| T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
| T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
| T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
| T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
| T_BOOL { $$ = CDECL_TYPE_BOOL; }
| T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
| T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
| T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
declspec_notype: qualifier | declspec_simple {
ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
}
typespec_noid: typespec_simple {
ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
}
qualifier: qualifier_simple {
ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
}
typespec: typespec_noid | T_STRUCT T_IDENT {
ALLOC_STRUCT($$, struct cdecl_declspec,
.type = CDECL_TYPE_STRUCT,
.ident = $2);
} | T_UNION T_IDENT {
ALLOC_STRUCT($$, struct cdecl_declspec,
.type = CDECL_TYPE_UNION,
.ident = $2);
} | T_ENUM T_IDENT {
ALLOC_STRUCT($$, struct cdecl_declspec,
.type = CDECL_TYPE_ENUM,
.ident = $2);
} | T_IDENT {
ALLOC_STRUCT($$, struct cdecl_declspec,
.type = CDECL_TYPE_IDENT,
.ident = $1);
}
declspec_noid: declspec_notype | typespec_noid
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");
ALLOC_STRUCT($$, struct cdecl_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);
}
parameter: declspecs declarator {
ALLOC_STRUCT($$, struct cdecl,
.specifiers = $1,
.declarators = $2);
}
parameters: parameter | parameters T_COMMA parameter {
$$ = $3;
$$->next = $1;
}
varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
parameter_type_list: parameters varargs {
struct cdecl *p, *c, *n;
/* Parameters were accumulated in reverse order. */
for (p = NULL, c = $1; c; p = c, c = n) {
n = c->next;
c->next = p;
}
ALLOC_STRUCT($$, struct cdecl_declarator,
.type = CDECL_DECL_FUNCTION,
.u.function.parameters = p,
.u.function.variadic = $2);
}
parens: T_LPAREN parameter_type_list T_RPAREN {
$$ = $2;
} | T_LPAREN declarator_ish T_RPAREN {
ALLOC_STRUCT($$, struct cdecl_declarator,
.type = CDECL_DECL_FUNCTION);
ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
.declarators = $2);
}
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 = $2,
.child = $3);
}
declarator: direct_declarator | pointer
declarator_ish: direct_declarator_ish | pointer
postfix: array | parens
direct_declarator_ish: {
ALLOC_STRUCT($$, struct cdecl_declarator,
.type = CDECL_DECL_NULL);
} | direct_declarator_ish postfix {
$$ = $2;
$$->child = $1;
}
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 postfix {
$$ = $2;
$$->child = $1;
}
%%
void
yyerror(YYLTYPE *loc, struct cdecl **out, yyscan_t scanner, const char *err)
{
if (strstr(err, "T_LEX_ERROR"))
return;
fprintf(stderr, "%s\n", err);
}