B
B
becks2014-07-04 14:02:17
PHP
becks, 2014-07-04 14:02:17

How to determine what kind of PL is from the contents of the file (without extension)?

Actually interested in the definition of C ++, PHP and javascript. I understand that there may be constructions by which it is not possible to say unequivocally what kind of language it is. I understand that you can search the file yourself for any language-specific constructions, but what if there are already ready-made solutions?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
W
whats, 2014-07-04
@whats

php file by <? ?>
java script - alert, Declaring variables as var
C++ - #include
is just a part, every language has specific constructs for it. Another point is that they may not be in the file, so it cannot be unambiguously determined. But you can compose language instructions and make stacked arrays. Which one will have more matches, therefore it will be this language

L
lookid, 2014-07-04
@lookid

Read about compilers. Perhaps somehow it can be determined at one of the stages of compilation. Look at the interpreters of handwritten languages, maybe there will be something.
At a glance, you can do this:
1. Pass removes comments
2. Pass through global variables (excluding functions)
Here you can find out some results on writing variables and so on.
3. Passing through functions (excluding global variables)
Here you already parse the body of the function line by line and block by block (for example, if, for, while)
In any case, you need a function that can parse the { ... } block.
The output will be something like a list of strings where this or that language has been detected.
Javascript 1,2,3,4,5
php 6,7,8,9
c++ 2,4,5
Here is an example of some kind of interpreter www.williamspublishing.com/Books/5-8459-0786-1.html#ogl
Chapter 3
Implementing a language interpreter in Java 53

A
alexanius, 2014-07-05
@alexanius

You can look at the file command , it gives a good first guess.

A
AxisPod, 2014-07-07
@AxisPod

Collect the grammars of all the necessary languages ​​​​and try to parse them?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question