I’m working on a compiler in c (flex/bison) and I have this segfault that I just can’t figure out, I’ve tried gdb and know where it is caused but everything I’ve tried doesn’t seem to resolve it.
4+3;
lex: création token NUMBER 4
lex: création token +
lex: création token NUMBER 3
lex: création token ;
lex: fin de lecture
Parsing:: c'est bien une expression
ast de l'expression:
[ :: [ [ :4.00: ] :+: [ :3.00: ] ] ]
Program received signal SIGSEGV, Segmentation fault.
0x0000555555558315 in print_prog (p=0x4008000000000000) at AST.c:158
158 if(NULL != p->command){
(gdb) backtrace
#0 0x0000555555558315 in print_prog (p=0x4008000000000000) at AST.c:158
#1 0x0000555555558348 in print_prog (p=0x555555561c00) at AST.c:161
#2 0x00005555555552c1 in main () at main.c:19
I’ll provide some of my file for context
so these are parts of my ast.h and ast.c files
ast.h
struct _expr_tree {
char rule; /* "name" of the rule/operation operation */
float number; /* int for value */
struct _expr_tree* left; /* NULL if unary node or leaf*/
struct _expr_tree* right; /* used for unary node but NULL if leaf */
bool BOOLEAN;
char *VAR;
};
typedef struct _expr_tree* AST_expr;
struct _command_tree {
char rule; /* "name" of the rule/operation operation */
int taille;
AST_expr expr1;
struct _command_tree* then_comm;
struct _command_tree* else_comm; /* used for command with at least one sub-expression */
};
typedef struct _command_tree* AST_comm;
struct _program_tree {
char rule;
int taille;
AST_comm command;
struct _program_tree* next;
};
typedef struct _program_tree* AST_prog;
ast.c
AST_comm new_command(AST_expr expression){
AST_comm t = malloc(sizeof(struct _command_tree));
if (t!=NULL){ /* malloc ok */
t->expr1 = expression;
} else printf("ERR : MALLOC ");
return t;
}
AST_prog new_prog(AST_comm c, AST_prog next){
AST_prog p = malloc(sizeof(struct _program_tree));
if(p!=NULL){
p->command = c;
p->next = next;
p->rule = '';
p->taille = 0;
} else printf("ERR : malloc");
return p;
}
parser
programme:
| commande programme
{ *rez = new_prog($1, $2); }
;
commande: expression ';'
{ $$ = new_command($1); }
| I '(' expression ')' commande ';' S commande ';'
{ $$ = new_if_else_expr($3, $5, $8); }
;
I tried casting the AST_prog in new_prog function but it didn’t work, I also wrote NULL first in every if statement but it still didn’t solve much. I’m this point I’m kinda lost.
Any help is welcomed and thank you all for reading !
Amazigh Alloun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.