+static Node *
+new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
+{
+ Node *node = malloc (sizeof (Node));
+ node->nterm.type = 1;
+ node->nterm.parents = 0;
+ node->nterm.form = form;
+ node->nterm.children[0] = child0;
+ if (child0)
+ child0->node_info.parents += 1;
+ node->nterm.children[1] = child1;
+ if (child1)
+ child1->node_info.parents += 1;
+ node->nterm.children[2] = child2;
+ if (child2)
+ child2->node_info.parents += 1;
+ return node;
+}
+
+static Node *
+new_term (char *text)
+{
+ Node *node = malloc (sizeof (Node));
+ node->term.type = 0;
+ node->term.parents = 0;
+ node->term.text = text;
+ return node;
+}
+
+static void
+free_node (Node *node)
+{
+ if (!node)
+ return;
+ node->node_info.parents -= 1;
+ /* Free only if 0 (last parent) or -1 (no parents). */
+ if (node->node_info.parents > 0)
+ return;
+ if (node->node_info.type == 1)
+ {
+ free_node (node->nterm.children[0]);
+ free_node (node->nterm.children[1]);
+ free_node (node->nterm.children[2]);
+ }
+ else
+ free (node->term.text);
+ free (node);
+}