Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag 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
ASKER CERTIFIED SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.