Programming: Compilers and Interpreters

    Programming: Compilers and Interpreters

      You're in a foreign country right now, Shmooper. Not one of the ones that with a Union Jack in its flag. Nope, here in computer land, everyone's speaking a language and it isn't the one you do.

      Binary's the native language, but it's hard to learn. Really hard.

      What's a Shmooper to do? One option—okay, the only option—is to see if you can figure out an intermediary language between the two of you. It comes out sounding kind-of like English and kind-of like computer talk and you still have no idea how to ask where the bathroom is, but you'll work out the kinks later.

      What's that, you have to go now? You are aware that computer land doesn't actually exist, right?

      Anyway, the language you've developed is called a high-level language and it sounds vaguely English-y. That's great, but computers don't actually speak anything except binary. Something needs to translate it from that high-level language to machine language.

      Enter the compiler.

      Compilers

      A compiler is a program like any other, but it translates your code into machine language and finds simple bugs along the way. Like any good translator, really.

      Say the computers in computerland have finally asserted their dominance over the human race (we all knew it was coming). To prove your loyalty, you decide to offer them three gifts.

      You ask your translator to give King Computer (his friends call him Kingsy) your gifts. But one box is just a pile of junk. The translator, respecting your privacy, wouldn't know which box is filled with ugly Christmas sweaters and so won't be able to save you from the wrath of King Computer. However, if the gift itself is literally trash—like a broken Styrofoam cup—they would notice right away. They warn you that sending trash is equivalent to heresy, and you're spared from being offered as sacrifice to CompuGod.

      This time.

      Where were we? Oh yeah.

      The compiler can't catch every error, but it can see the big, glaringly annoying ones. And if it does, it'll yell until you give a suitable gift. After it thinks you aren't going to offend CompuGod, it'll compile the code.

      Although, when you think about it, the compiler's the reason computers were able to take over in the first place. BC (Before Compilers), computers couldn't really talk with each other: they're all built differently, so they all speak a different language.

      Coders just accepted the fact that they'd need to rewrite their code to talk with each new computer. But compilers now let us translate between each computer and a high-level language. When a new computer comes out, all we need to do is write a new compiler and the code will run.
      (Source)

      So blame the compiler.

      Interpreters

      Once the code's compiled, it's ready to run through an interpreter. The interpreter goes through the program line by line and actually tells the computer to run the steps in the computer's language. Without it, we'd have a nice set of instructions that do nothing.

      Fancy.

      Think of the Combinations

      Not every language has a compiler. Some languages (we're looking at you, Python) go straight from the language to the interpreter. Some languages just have a compiler and no interpreter (calling you out, C). Other languages want to be absolutely, positively sure that you mean exactly what you're saying (not pointing fingers, Java), and include both a compiler and an interpreter. However they do it, programming languages need something that goes from high-level to low-level language.

      Interpreters sound great, and...they are, but they also have some drawbacks. Their rival, compilers, put all computer code into memory while interpreters go one line at a time. The problem? Because compilers have more stuff to look at, they can also root out potential problems in the code before the problems crash the program. Interpreters can't always do that.

      Also: because an interpreter doesn't save anything to memory, it has to re-interpret the code. Every. Time. It. Runs. Compilers can just compile once and run a million times. Compilers definitely win in the efficiency realm.

      Then again, that compilation won't happen until every single error is gone from the program. Sometimes the error won't even be an error according to you, the programmer, because you know exactly the types of input a part of code will receive. According to the compiler, though, there could be big errors, and you'll need to negotiate with it to make the programming magic happen. (Source)