Closed nohwnd closed 9 years ago
Hi nohwd,
You're basically right in your assessment of the error. The function lval_add
is getting the variable x
when x
is NULL
. This is causing the segfault. But x
shouldn't be NULL
when it is passed to lval_add
because of the following lines of code:
if (strcmp(t->tag, ">") == 0) { x = lval_sexpr(); }
if (strstr(t->tag, "sexpr")) { x = lval_sexpr(); }
So it looks like these two if
statements are both failing, which means that the tag
field is not the string ">"
(strcmp(t->tag, ">") == 0
) and that "sexpr"
is not a substring of the tag
field (strstr(t->tag, "sexpr")
).
Why might this be?
Well if you print the AST using mpc_ast_print()
you can probably find out. I've not checked but my guess is that you're s-expressions are not tagged with the string "sexpr"
, but are instead tagged with the string "sexpression"
, because this is the name of your rule in the mpc
grammar. If you change the rule name to sexpr
, or change the substring check to sexpression
, I think it should work.
I hope that helps,
Dan
Thanks for the prompt answer. The actual error was that I was using strstr
in both if
instead of strcmp
in the first if
. Took me good couple of hours before asking for help and now it is solved in few minutes. :)
Glad you managed to fix it!
Hello I am following the code in the book and in this chapter I am getting
Segmentation fault: 11
, I read on the internet that is caused by uninitialized access to data. I went through the code in the book few times and I can't spot the error. Could you check my code please? This is the output fromLLDB
:The problem is likely in
lval_read
where NULL is assigned to x pointer. If I change that line tolval* x = lval_sexpr();
the code works correctly.This is my full code: