The C Tricks I used when developing Peppa PEG

  • It’s FUN. I’ve written parsers for JSON, Mustache, Markdown and I think I can take the challenge now.
  • I haven’t had any opportunity to work on an Open Source project written in ANSI C.
  • Having a PEG parser in ANSI C can benefit whoever is developing a parser, as adding C bindings for other programming languages is not too difficult.

Segfault? GDB to Rescue!

Segmentation fault is an error I encountered quite often at the beginning. It means the program has attempted to access the memory it shouldn’t access, say, 0x0, or access some already free-ed memory. To avoid it, we should be careful, add NULL checkers before using a pointer, or make sure a free-ed memory never be called.

CMake: Make Friend

I have hated manually crafted Makefile from scratch and CMake has made it like a breeze.


add_custom_target( check COMMAND ${CMAKE_CTEST_COMMAND} …)
$ mkdir build
$ cmake ..
$ make check

Unity: A Testing Framework

I have used Unity as my testing framework for the project. Here is the reference.

Valgrind: Say Goodbye to Memory Leaks!

I found it particularly helpful Valgrind can help to detect memory leaks.

Callgrind: Identify Performance Bottleneck

To be more accurate, callgrind is just a subnet of Valgrind. It can output a summary of instruction executions. The most frequent appearing functions are notably important. If they’re time-consuming, the program will be very slow. Or, maybe we should think, are those frequently called functions really necessary, can they be pre-computed somewhere so the later calls can just use the cached values.

$ gcc -g  peppapeg.c examples/json.c && valgrind --tool=callgrind ./a.out
$ callgrind_annotate callgrind.out.63642


I now know Doxygen can be used to exact documentation from source code; CFFI can be used to create a C binding to programming languages, and more! I’ll spend more time on the project and share more tricks.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store