examples: fix coding style

* examples/c/glr/c++-types.y: Use snake_case identifiers.
Prefer strdup to malloc+strcpy.
This commit is contained in:
Akim Demaille
2021-10-23 06:09:31 +02:00
parent 6e6950fbbc
commit 7558cbf373
2 changed files with 38 additions and 41 deletions

View File

@@ -244,7 +244,7 @@ init_table (void)
symrec * symrec *
putsym (char const *name, int sym_type) putsym (char const *name, int sym_type)
{ {
symrec *res = (symrec *) malloc (sizeof (symrec)); symrec *res = malloc (sizeof *res);
res->name = strdup (name); res->name = strdup (name);
res->type = sym_type; res->type = sym_type;
res->value.var = 0; // Set value to 0 even if fun. res->value.var = 0; // Set value to 0 even if fun.

View File

@@ -29,24 +29,24 @@
%code requires %code requires
{ {
union Node { union node {
struct { struct {
int isNterm; int is_nterm;
int parents; int parents;
} nodeInfo; } node_info;
struct { struct {
int isNterm; /* 1 */ int is_nterm; /* 1 */
int parents; int parents;
char const *form; char const *form;
union Node *children[3]; union node *children[3];
} nterm; } nterm;
struct { struct {
int isNterm; /* 0 */ int is_nterm; /* 0 */
int parents; int parents;
char *text; char *text;
} term; } term;
}; };
typedef union Node Node; typedef union node node_t;
} }
%define api.value.type union %define api.value.type union
@@ -61,12 +61,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
static Node *new_nterm (char const *, Node *, Node *, Node *); static node_t *new_nterm (char const *, node_t *, node_t *, node_t *);
static Node *new_term (char *); static node_t *new_term (char *);
static void free_node (Node *); static void free_node (node_t *);
static char *node_to_string (const Node *); static char *node_to_string (const node_t *);
static void node_print (FILE *, const Node *); static void node_print (FILE *, const node_t *);
static Node *stmt_merge (YYSTYPE x0, YYSTYPE x1); static node_t *stmt_merge (YYSTYPE x0, YYSTYPE x1);
static void yyerror (YYLTYPE const * const loc, const char *msg); static void yyerror (YYLTYPE const * const loc, const char *msg);
static yytoken_kind_t yylex (YYSTYPE *lval, YYLTYPE *lloc); static yytoken_kind_t yylex (YYSTYPE *lval, YYLTYPE *lloc);
@@ -83,9 +83,9 @@
%glr-parser %glr-parser
%type <Node *> stmt expr decl declarator TYPENAME ID %type <node_t *> stmt expr decl declarator TYPENAME ID
%destructor { free_node ($$); } <Node *> %destructor { free_node ($$); } <node_t *>
%printer { node_print (yyo, $$); } <Node *> %printer { node_print (yyo, $$); } <node_t *>
%% %%
@@ -186,12 +186,12 @@ yylex (YYSTYPE *lval, YYLTYPE *lloc)
if (isupper ((unsigned char) buffer[0])) if (isupper ((unsigned char) buffer[0]))
{ {
tok = TYPENAME; tok = TYPENAME;
lval->TYPENAME = new_term (strcpy (malloc (i), buffer)); lval->TYPENAME = new_term (strdup (buffer));
} }
else else
{ {
tok = ID; tok = ID;
lval->ID = new_term (strcpy (malloc (i), buffer)); lval->ID = new_term (strdup (buffer));
} }
} }
else else
@@ -206,45 +206,45 @@ yylex (YYSTYPE *lval, YYLTYPE *lloc)
} }
} }
static Node * static node_t *
new_nterm (char const *form, Node *child0, Node *child1, Node *child2) new_nterm (char const *form, node_t *child0, node_t *child1, node_t *child2)
{ {
Node *res = malloc (sizeof *res); node_t *res = malloc (sizeof *res);
res->nterm.isNterm = 1; res->nterm.is_nterm = 1;
res->nterm.parents = 0; res->nterm.parents = 0;
res->nterm.form = form; res->nterm.form = form;
res->nterm.children[0] = child0; res->nterm.children[0] = child0;
if (child0) if (child0)
child0->nodeInfo.parents += 1; child0->node_info.parents += 1;
res->nterm.children[1] = child1; res->nterm.children[1] = child1;
if (child1) if (child1)
child1->nodeInfo.parents += 1; child1->node_info.parents += 1;
res->nterm.children[2] = child2; res->nterm.children[2] = child2;
if (child2) if (child2)
child2->nodeInfo.parents += 1; child2->node_info.parents += 1;
return res; return res;
} }
static Node * static node_t *
new_term (char *text) new_term (char *text)
{ {
Node *res = malloc (sizeof *res); node_t *res = malloc (sizeof *res);
res->term.isNterm = 0; res->term.is_nterm = 0;
res->term.parents = 0; res->term.parents = 0;
res->term.text = text; res->term.text = text;
return res; return res;
} }
static void static void
free_node (Node *node) free_node (node_t *node)
{ {
if (!node) if (!node)
return; return;
node->nodeInfo.parents -= 1; node->node_info.parents -= 1;
/* Free only if 0 (last parent) or -1 (no parents). */ /* Free only if 0 (last parent) or -1 (no parents). */
if (node->nodeInfo.parents > 0) if (node->node_info.parents > 0)
return; return;
if (node->nodeInfo.isNterm == 1) if (node->node_info.is_nterm == 1)
{ {
free_node (node->nterm.children[0]); free_node (node->nterm.children[0]);
free_node (node->nterm.children[1]); free_node (node->nterm.children[1]);
@@ -256,15 +256,12 @@ free_node (Node *node)
} }
static char * static char *
node_to_string (const Node *node) node_to_string (const node_t *node)
{ {
char *res; char *res;
if (!node) if (!node)
{ res = strdup ("");
res = malloc (1); else if (node->node_info.is_nterm)
res[0] = 0;
}
else if (node->nodeInfo.isNterm == 1)
{ {
char *child0 = node_to_string (node->nterm.children[0]); char *child0 = node_to_string (node->nterm.children[0]);
char *child1 = node_to_string (node->nterm.children[1]); char *child1 = node_to_string (node->nterm.children[1]);
@@ -282,7 +279,7 @@ node_to_string (const Node *node)
} }
static void static void
node_print (FILE *out, const Node *n) node_print (FILE *out, const node_t *n)
{ {
char *str = node_to_string (n); char *str = node_to_string (n);
fputs (str, out); fputs (str, out);
@@ -290,7 +287,7 @@ node_print (FILE *out, const Node *n)
} }
static Node * static node_t *
stmt_merge (YYSTYPE x0, YYSTYPE x1) stmt_merge (YYSTYPE x0, YYSTYPE x1)
{ {
return new_nterm ("<OR>(%s, %s)", x0.stmt, x1.stmt, NULL); return new_nterm ("<OR>(%s, %s)", x0.stmt, x1.stmt, NULL);