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.
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. :)