# Parsing and generating C code

**URL:** <https://discourse.julialang.org/t/parsing-and-generating-c-code/15959>\
**Category:** General Usage\
**Created:** [October 6, 2018, 8:15am UTC](https://discourse.julialang.org/t/parsing-and-generating-c-code/15959 "2018-10-06T08:15:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 6, 2018, 8:15am UTC](https://discourse.julialang.org/t/parsing-and-generating-c-code/15959/1 "2018-10-06T08:15:11Z")

</div>

Hello,

I have a very small task to solve and want to do it with Julia:  
Removing curtain functions from a C code module. The names of the functions to remove are given as string list, there is an input file and an output file.

How would you approach this problem?

Finding the start of a function should be easy, but how to find the end?

Uwe

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [October 6, 2018, 8:59am UTC](https://discourse.julialang.org/t/parsing-and-generating-c-code/15959/2 "2018-10-06T08:59:20Z")

</div>

I guess you could always keep count of how many `{` and `} ` braces you encounter after the beginning of a function.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 6, 2018, 9:05am UTC](https://discourse.julialang.org/t/parsing-and-generating-c-code/15959/3 "2018-10-06T09:05:28Z")

</div>

Thanks, I will try that.

So there is no library for parsing C/C++ code that I could use?

In Python there is pycparser for example.  
[https://github.com/eliben/pycparser](https://github.com/eliben/pycparser)

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 6, 2018, 9:16am UTC](https://discourse.julialang.org/t/parsing-and-generating-c-code/15959/4 "2018-10-06T09:16:26Z")

</div>

You need to ignore comments and solve problems with conditional `#ifdef`-s as well

There could be also evil `#include`-s and evil macros with odd numbers of braces

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 6, 2018, 9:59am UTC](https://discourse.julialang.org/t/parsing-and-generating-c-code/15959/5 "2018-10-06T09:59:40Z")

</div>

> **[GitHub - JuliaInterop/Clang.jl: C binding generator and Julia interface to...](https://github.com/JuliaInterop/Clang.jl)**
>
> C binding generator and Julia interface to libclang - GitHub - JuliaInterop/Clang.jl: C binding generator and Julia interface to libclang

[http://nbviewer.jupyter.org/github/ihnorton/Clang.jl/blob/master/examples/parsing\_c\_with\_clangjl/notebook.ipynb](http://nbviewer.jupyter.org/github/ihnorton/Clang.jl/blob/master/examples/parsing_c_with_clangjl/notebook.ipynb)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 6, 2018, 12:18pm UTC](https://discourse.julialang.org/t/parsing-and-generating-c-code/15959/6 "2018-10-06T12:18:14Z")

</div>

Thanks for the link!

One more question: Any idea to obtain the line number of a function, using Clang.jl?

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [October 6, 2018, 2:34pm UTC](https://discourse.julialang.org/t/parsing-and-generating-c-code/15959/7 "2018-10-06T14:34:14Z")

</div>

[https://clang.llvm.org/doxygen/group\_\_CINDEX\_\_CURSOR\_\_SOURCE.html](https://clang.llvm.org/doxygen/group __CINDEX__ CURSOR__SOURCE.html)
