Avatar of Rohit Bajaj
Rohit Bajaj
Flag for India asked on

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
JavaJavaScriptCOBOL

Avatar of undefined
Last Comment
krakatoa

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
SStory

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Am P

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
awking00

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.
SStory

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
krakatoa

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.