How does a computer use a compiler to gather information of code?


I’m relating to how my computer actually “reads” the code I’m providing. So on a very basic level – how does it “understand” what the compiler tells it?

Is this some kind of electronic signal transmission or how does it work?

In: 0

Compiler is basically a translator that translates a language specific code to machine code based on the architecture of the machine the code is running on. This is to create a level of abstraction between the higher and lower level languages.

It’s hard to answer this question without asking more questions. Simple as possible

We write code in a human readable language.
Compilers translate that into a thing that a computer can run

We wrote the compiler so that it could translate the code. Part of coding is we use libraries of pre written code so that we don’t have to rewrite everything ourselves and so we don’t have to know everything about everything

Some of those libraries are never changing and the same everywhere. Some of the libraries are specific to the computer but they have agreed to “names” and special “methods”. And the compiler puts those names and methods into the “compiled” code. It’s not really the compiler but the linker that does this. These are pulled in when the computer starts running your application

That’s how your application know specific things about your specific computer


A compiler just replaces words in programming language to words in machine language. For some programming languages it’s a bit more complicated, because the grammar is a bit different too, like if you wanted to translate English to Latin.

I suspect you also want to know how a computer then knows how to execute the machine code. I would compare this to a musical machine, like how a barrel organ “knows” which instruments to play without actually being able to read notes like a human. [Other example of musical machine]( – very much in the ELI5 spirit, don’t you think?

“Reading” an instruction means that a certain circuit is closed that connects some input data from memory to the calculator and the result is connected to a certain memory location as well. Devices like keyboards and screens are just memory locations with numbers in them as well.

Transistors act as railway switches to guide to data along the correct path. Each instruction just sets up the switches in a certain way. One memory location is called the “program counter” and it determines where the *next instruction* should come from. It’s incremented by one (or four or eight or however much instructions are spaced apart) automatically unless it’s set by the current line if code. That’s how you do loops and function calls.

The calculator inside a computer works like the calculator in a simple electronic calculator. Are you interested in that as well?


You have a wall full of [light switches](×1127/smart/filters:no_upscale()/200526811-001-56a5a63e5f9b58b7d0ddd35e.jpg). Top to bottom, edge to edge, it’s just light switches arranged in a huge grid. Can you picture it? The whole wall is covered by them.

**PART I – Interpreting Switches**

Everyone has agreed that if switches 2, 544, 1097, and 45541 are turned on, that means the letter “A”.

Carry that idea to a larger scale. Which switches are on at the same time, “means” something. Maybe it’s a letter “A”, maybe it’s the number 4, maybe it’s a blue pixel at x-y coordinate (45, 98).

The important part is that we all agree how to “interpret” the current state of the switches.

After interpretation, the computer can be told to do things. Move the mouse cursor, turn this pixel to red, play a beep sound. “If these nine switches are on, play this song”.

**PART II – Turning On/Off Switches**

I tell the computer, “Anytime /u/PalaceOfPleasure69 types the letter G on the keyboard, turn switches 65, 101, and 876 on”.

I could also say, “Computer, anytime /u/PalaceOfPleasure69 moves the mouse to the left by one pixel, turn on switch 56, then 57, then 58.

So INPUTS from you, can control the current state of all the switches.

I could also make the switches react on or off based on a streaming video coming in. If the next byte off the ethernet is this, then turn those switches on.

So the important part here is that there are many things, both user-initiated and otherwise, that CHANGE the STATE of the switches.

**PART III – Go very fast!**

Now speed everything up to a million times a second. A million times a second, something changes some of the switches. A million times a second, some part of the computer looks at the switches and interprets them.

**PART IV – Putting it Together**

Inside your computer, are tens of millions of switches. They can either be on or off.

Let’s take the program Notepad as an example.

* When the user types an A, turn the appropriate switches on
* When these certain switches are on, show an A on the screen
* When the user clicks Save, turn other switches on.
* When these certain switches are on, take the contents of the Notepad, save it to disk.

This scales up to TREMENDOUS proportions. Everything on your computer is both setting switches on/off, and reading certain switches to make decisions. Sometimes it’s in reaction to you doing something, sometimes it’s in reaction to bytes coming in from the internet.

Does that help?