An Introduction to Programming: Compiler and Syntax

1 Conversation

Compiler

What is a Compiler?



A compiler is a vital part of a programming language. The compiler turns the instructions, which the programmer wrote in text (or dragged&dropped, in some languages) into instructions that the computer's Central Processing Unit (i.e. CPU, aka MPU - Main Processing Unit) can understand. The only language a CPU can understand is machine code, which is just binary numbers. For example, when using assembly language with a Motorola's 6802 microcontroller,the instruction INCA* would be compiled into this machine code: 01001100

How does it work?


The compiler sweeps through the codes from one to as many number of times it requires to make it into machine code. In the old days, the compiler would just turn everything you wrote into machine code, even if your code would attempt doing some irrational things, as long as it was constructed of instructions and not undefined symbols.If you don't know, an instruction is a beforehand agreed upon set of characters, which the compiler can make into machine code (for example, the INCA instruction I mentioned above).
Today, the compilers won't accept just anything, and would even tell you where in your code the problem is.

If you wondered why it would take the compiler several sweeps through the code before it's finished, well, you could imagine someone writing a riddle, and when you solve the riddle, the answer you get is a riddle too, and so on, until you get to the final answer, which is in plain and easily understood English. So, the compiler takes the code and simplifies it (separating it into different instructions, checking each instruction for validity, etc.) until it makes it into machine code. Also, when a part of the code relates to another part of the code, the compiler checks them against each other and makes sure that they "fit", so to speak.

Syntax

What is Syntax?



Syntax is the set of rules that defines the way a programming language's code must be written. It is just like the rules of any spoken language. For example, when I write
It took a genius to develop an aspirin bottle that couldn't be opened by child capable of operating a VCR.

- Doug Larson

you understand that the text is a quote by Doug Larson. And so, when And so, when I write in C language

IF(money<price)

{

print 'Sorry, not enough money.';

}

it might be obvious to you what this code is for, because it makes sense: if the available amount of money I have is smaller than the price of the item, then I can't buy it. But this bears no meaning to the computer. The compiler makes this C code into machine code instructions of comparison, branching and data moving.


Why do we need Syntax?



Syntax can be viewed as a set of conventions the programmer has with the compiler. The programmer follows these rules, and later on the compiler uses them to translate the language's code into machine code. For example, in Pascal language, if I would write Write(MyInteger) the compiler would understand that I want the value of the variable MyInteger to be written on the screen, but if I were to write
Write('MyInteger') the compiler would understand that I want the actual string of characters MyInteger to be written on the string. The ' character tells the compiler when a string begins and ends. This is one of the rules of Pascal's syntax.




Now that we know what a compiler is and what syntax is and how they relate to each other, we can explain some basic concepts in programming:


Block Enclosures



When a programmer wants to repeat a set of instructions (i.e., a block) a specific number of times or as a result of a conditional state, he uses enclosures to tell the compiler when the block begins and ends. In C language brackets are used ({ }), like so:

Do
   {
    instruction

    instruction

    instruction

    .

    .

    .

    instruction
   }

Loop Until (condition)

This code will make the CPU repeat the instructions between the brackets until the condition is true.

instrucion termination symbols



In order to tell the compiler where an instruction ends the programmer uses special symbols. For example, in Pascal the instruction termination symbols is ;. So, a set of instructions in Pascal can look like this:
Write(MyInteger);Write('MyInteger');

Bookmark on your Personal Space


Conversations About This Entry

Entry

A546608

Infinite Improbability Drive

Infinite Improbability Drive

Read a random Edited Entry


Written and Edited by

Disclaimer

h2g2 is created by h2g2's users, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the Not Panicking Ltd. Unlike Edited Entries, Entries have not been checked by an Editor. If you consider any Entry to be in breach of the site's House Rules, please register a complaint. For any other comments, please visit the Feedback page.

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more