6

I know the term "scripting languages" is just a subset of programming languages, but I want to refer to programming languages such as Python and Ruby among others.

  1. First of all, why don't we need a compiler for these languages? For example, IDEs like Visual Studio or Eclipse have their own compiler in order to translate the code and execute the program. What does it mean for a programming language to be interpreted and how are they (Python, Ruby) compiled before execution in the terminal without a compiler?

  2. Also, when we install Python or Ruby in our computers before we start coding, what are we actually installing? packages? files? something that lets our computer understand the language?

  • Some scripting languages (e.g. Lua or Python or Neko) are compiled to a bytecode. See also this – Basile Starynkevitch Oct 15 '15 at 20:39
  • ... Others (for example perl) may be compiled into an internal representation (perl is always compiling to an internal (but not bytecode) representation). That representation may be then fed through the interpreter, or to another compiler (that produces C source code). This can be further confused in that the interpreter part may be invoked in the middle of a compilation phase (the BEGIN block). The line between compilers and interpreters is very fuzzy now. IMHO: its best to forget about 'scripting' languages. They are languages. That's it. – user40980 Oct 15 '15 at 20:48
  • 2
    You don't need a compiler for any language. Not even C. – immibis Oct 16 '15 at 5:01
  • There are lots of interesting question with "Python" and "compile" tags. I recommend reading those ( shameless plug : programmers.stackexchange.com/questions/243269/… ) – Euphoric Oct 16 '15 at 6:33
  • C interpreters exist. – reinierpost Oct 16 '15 at 9:27
15

What does it mean for a programming language to be interpreted and how are they compiled before execution in the terminal?

Compilers and interpreters are very similar things, right up until the last step. For a compiler, the last step is to generate code in the output language and save it. For an interpreter, it's not trying to save your code; it's trying to execute it immediately. It does this by breaking down the program into basic semantic commands, much like the compiler does, and then executing those commands via a runtime that implements them in software.

2) Also, when we install Python or Ruby in our computers before start coding, what are we actually installing? packages? files? something that lets our computer understand the language?

Generally speaking, you're installing the interpreter and the standard libraries. Most likely some basic tools (such as a REPL, in the case of many scripting languages) get installed too as part of the standard package.

1

An interpreter is basically an on the fly compiler. At run time it takes your code and translates that down to machine code. This is exactly what a compiler does except a compiler does it all at once and before you run the program. An interpreter translates it as it needs it and runs it straight away. Think of a compiler as someone translating a book from another language to your native language and then giving you the finished product to you to read. Think of an interpreter as someone who reads a book in another language and as they read it they translate it and say the translation out loud to you. Some reasons that you would want to do compilation at run time rather than before run time are,

  • Your code will run on every platform as the interpreter can take into account the platform specific details at runtime. A standard compiler can only compile for the system that is being used to compile and so will have to be recompiled on every new system (Windows,OS X,Linux, etc).

  • When your code is interpreted at run time the interpreter can make machine specific optimisations to make use of special technologies (such as special a cpu instruction) that not all machines have. This can sometimes make your code run faster. A standard compiler can only optimises your code so much as the end result has to be able to run on a wide variety of hardware and operating system versions and therefore can only use a generic CPU instruction set and generic is features and can't take advantage of special technologies that may be available as easily (compiled programs still can but it has to be done manually and isn't as easy).

There many are other reasons why you might want to use an interpreter instead of a compiler. If you want to know more just do a Google search "compiled vs interpreted".

When you install an interpreter say Python for instance you are installing the interpreter (the on the fly compiler) and a standard set of libraries (eg math). There is probably some other stuff that is being installed as well which isn't really important such as IDLE.

  • 4
    I did not downvote, because 50% of your answer I agree. However, providing type safety in a language (or not) has absolutely nothing to do with the way it gets compiled (before or just in time). For instance, C# got the dynamic keyword that acts similar to the way Perl variables work. – modiX Oct 16 '15 at 7:35
  • Yeah that's true bad example. I was trying to think of examples of the abstraction that interpreted languages usually have. – lochlanna Oct 16 '15 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.