Nithin Bekal Posts About

What's This Programming Language?

02 Jun 2012

A couple of weeks ago, I came across the site “What’s that programming language?”, in which you’re shown a snippet of code and you have to identify the programming language.

I looked around to see if there was a way to do the reverse, ie. identify the language, given a snippet of code. Since I have no idea how to actually parse code and do this, I tried doing this using the highlight.js syntax highlighting library. Highlight.js has a highlightAuto() method that can be used as a simple way of doing this.

See the demo here.

The highlightAuto() method tries to highlight the snippet using all available languages and returns the language for which it identifies most syntactic structures. Obviously, longer code snippets are more likely to be identified.

You can fork the code for the demo on Github. Feel free to send in a pull request if you’ve made improvements to the demo. :)

Nithin Bekal
Hi, I’m Nithin! This is my blog about programming. Ruby is my programming language of choice and the topic of most of my articles here, but I occasionally also write about Elixir, and sometimes about the books I read. I'm @nithinbekal on Twitter.