orangeduck / BuildYourOwnLisp

Learn C and build your own programming language in under 1000 lines of code!
http://www.buildyourownlisp.com/
Other
2.84k stars 392 forks source link

Segfault being caused by mpc_parse_run #154

Closed icebarf closed 3 years ago

icebarf commented 3 years ago

Program (Lisp) crashing when calling load function. On Chapter 13. Here is the GDB Error output and my implementation of the lisp. I could not figure out why the error was happening. It would be great if you could tell me if its an issue with the parser or my code.

image main.zip

icebarf commented 3 years ago

After researching for a while I found out that it is an issue with the variable Byolisp or Lispy that is being passed into mpc_parse_contents. The value of Byolisp/Lispy in my version is 0x0 or NULL. I haven't been able to figure out the solution yet. Any help would be appreciated.

orangeduck commented 3 years ago

Hi,

The name you give to mpc_new needs to match the name you use in the grammer. My guess is that these do not match and so it is not assigning the rule anything.

Dan

icebarf commented 3 years ago

Oh yes, Thanks Dan. Actually after carefully comparing your version and mine, I was re declaring the grammar in main function after declaring them in the global scope as well. Thus the Lispy variable became 0x0 (NULL) and caused a segfault.

Thanks for writing this book, I really appreciate your work and have gained good amount of knowledge about C. And lastly thanks for replying even years after writing of the book.

orangeduck commented 3 years ago

Glad you enjoyed it good luck with the rest of the book!