how to find out language of the code

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
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amitkumar PSr. ConsultantCommented:
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 SpecialistCommented:
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.
SStoryCommented:
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.
krakatoaCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.