I build an AST with bison with a pointer to a Node structure:
typedef struct Node {
enum node_type type;
Stmt *left;
struct Node *right;
} Node;
Then I print it in bison in this manner:
printf("%s\n",root->right->left->value_s.declaration->value_d.string);
printf("%s\n",root->right->left->value_s.declaration->id);
printf("%s\n",root->right->right->right->left->value_s.stdout->string);
it shows well, but I was trying to print it with an external function and then I get the segmentation fault. This is the starter bison production:
program: stmts {
printf("---- PRINTING AST ----\n");
$$ = malloc(sizeof(Node));
$$->type = NODE_TYPE_NODE;
$$->left = NULL;
$$->right = $1;
Traversal($$);
}
;
This is the code of the external function:
void Traversal(struct Node *root) {
if(root->left == NULL && root->right == NULL)
printf("AST is empty.");
else {
switch (root->left->type) {
case NODE_TYPE_STMT_D:
TravDeclaration(root->left->value_s.declaration);
break;
/* other cases */
default:
printf("Error, check node_type.");
break;
}
if(root->right->type != NODE_TYPE_END)
Traversal(root->right);
}
}
I think that there is a possible error in the use of the pointer when I call the external function. Please tell me if I missed something in the question, this is my first time here.