how to find out language of the code

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
on
hi,
i am making a web application in which user can paste some code...it will also have a drop down which will have list of all languages like java, c, c++
i want to auto detect which language the code is and auto select that language in the drop down.

are there any libraries in javascript etc which can help here ?
or any other way i can achieve this ?

thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Something like this maybe: https://code.google.com/archive/p/google-code-prettify/

If not, I would parse the code to see if any lines end with ";"

If so, that rules out basic, cobol, and a bunch of other language and gets you in the C/C#, javascript, C++ realm.  Maybe then look for keywords and do probabilities or something. If you find "#include" it is not java and not php, but probably C or C++. If you find the keywords include starting a sentence it might be PHP. php may also have "<?php" in it.
If you.  Search for language differences and weed through it like so.

Commented:
Agree with SStory.

You will have to study basic programs for typical keywords of all the languages you are displaying in a drop-down in your application.

few examples of the basic keywords...
C/C++ -> include
Java -> import
Php -> <?php
Cobol -> Identification Division
FoxPro -> Set command
awking00Information Technology Specialist

Commented:
I think you'll get closer to determining what language is being used by making the person entering the code to select the language first from the drop down menu. I also think that would be especially true if someone is entering code snippets and not an entire program that would contain various identifying keywords.
Of course it is easier if the person tells you.  However the question was how to identify it programmatically.  If you can get them to tell you then I'm not sure what this question is.
As awking00 suggests, you are going to have your work cut out, in what will essentially be a dubious parse exercise, open to imprecision. What would you do when faced with a Java programme that had native code included, and vice-versa?

Notepad++ - which imho is a neat editor - formats all types of code very nicely. Once you tell it from the menu, what language it is you are feeding it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial