Home > Uncategorized > pycparser: a serious entry in the not-written-in-C C-parser category

pycparser: a serious entry in the not-written-in-C C-parser category

Sometimes it feels like everybody and his dog has had a go at writing a C parser. On the whole these parsers are small subsets and the choice of subset seems to be driven by the developer’s taste, what they find easy to do and how much time they are willing to put into the project.

Some time ago a blog post discussing parsing C type declarations made me think that pycparser might be a cut above the usual learning experience projects and a quick look showed it was quite good. I recently tried it out again on the examples from my C book and it did a surprisingly good job of handling this rather weird set of edge cases (it failed to handle the code in 20 out of 957 files).

There is a type of person who insists that the C parser used by the C source analysis project they are working on be written in a ‘high-level’ language, i.e., they don’t want to use one of the perfectly adequate (and correct) parsers written in C/C++. I’m not sure whether this is because having to actually use C would expose the poor state of their knowledge of the language, language snobbery (its ok to analyse C source, but write it, heaven forbid) or they are members of a One True Language guild.

Up until now the parser of choice, for people not wanting to use C/C++, was CIL (a slightly more up to date version on Github); used by Coccinelle and many other tools.

If you really don’t want to parse C source using a parser written in C/C++, I think pycparser has now reached the stage where it is worth considering, along with CIL.

Tags: ,
  1. Daniel
    August 17th, 2016 at 14:00 | #1

    “I’m not sure whether this is because having to actually use C would expose the poor state of their knowledge of the language, language snobbery (its ok to analyse C source, but write it, heaven forbid) or they are members of a One True Language guild.”

    Or… OR… because they have an existing project in some language they suddenly need a C-parser for and don’t want to re-implement the whole project?

  2. August 17th, 2016 at 14:28 | #2

    @Daniel
    While this could happen, I think it would be very rare. Wanting to parse parts of C or spot particular constructs is more likely.

    A more likely case is that a project has to produce a stand alone tool and want to keep everything in the same language (to make it easier to hire new people).

  1. No trackbacks yet.

A question to answer *