Skip to main content

How a computer language compiler works

The programs that are written in languages like C++ or C are just text. Computers don't understand that text as it is. The program (the text) has to be compiled (translated into another language) into machine language which is the actual language that the CPU understands.

Why ? you ask. Well it's because development in pure machine language is slow and prone to errors. And machine language doesn't look like our spoken language which makes it hard to learn and understand.And because it takes a long time to learn machine language. And because people after a long time programming in machine language have observed some patterns emerging and they gave those patterns names like : for loops, do while loops, while loops, if decisional blocks, functions etc.

Like spoken languages, computer programming languages have an alphabet(lexicum) of words that mean(do) something. So the first thing that a compiler does is to analyze the words of a program, that's lexical analysis.

After the compiler checks using lexical analysis that the words used are the ones from the language it does does a syntax analysis. Syntax analysis checks if the combination of words used follow a predetermined grammar.

After syntax analysis comes semantical analysis in which the compiler checks if the meaning of the expressions made from the language's words make sense.

After that the compiler starts replacing expressions in the high programming language with expressions in assembly language. Before this step there can be a step of intermediate representation and intermediate optimization.

Here's a picture from a book called the "Dragon Book", a book about compilers.
This picture gives a good example how an expression in a high language is transformed(translated) into an expression in machine language :

how a compiler works


Comments

Popular posts from this blog

AMAZING!

"One day in 1939, Berkeley doctoral candidate George Dantzig arrived late for a statistics class taught by Jerzy Neyman. He copied down the two problems on the blackboard and turned them in a few days later, apologizing for the delay — he’d found them unusually difficult. Distracted, Neyman told him to leave his homework on the desk. On a Sunday morning six weeks later, Neyman banged on Dantzig’s door. The problems that Dantzig had assumed were homework were actually unproved statistical theorems that Neyman had been discussing with the class — and Dantzig had proved both of them. Both were eventually published, with Dantzig as coauthor. “When I began to worry about a thesis topic,” he recalled later, “Neyman just shrugged and told me to wrap the two problems in a binder and he would accept them as my thesis.”"

Ever wondered what your chinese/japanese pictogram tattoo really means?

So you got drunk on a night out. So you suffer and felt like doing something about it. You had a bad love life etc. Chances are that you have a tattoo because of that. And chances are that you have a tattoo with chinese/japanses letters. You think you know the meaning of the tattoo. You may be wrong. Visit this blog , these guys really know Chinese and Japanese and they will decipher that tattoo you think it says "strong" but actually means "use spoon"(they do it by request): LOLOLOLOL!    " 災 means "calamity, disaster, catastrophe", and definitely not "beauty", which is 美 ."

Have you noticed the storms we've had lately ?

I just saw a pretty weird report on the TV. A reporter with horrible acting skills was discussing with a stereotypically looking "scientist" (huge glasses, comically unkempt, poorly dressed) about Ivan. Not the Russians but the latest hurricane that swept the north Atlantic. The main point was whether or not global warming plays a role in the increasing number of powerful hurricanes that hit the North American coasts yearly. The discussion didn't have a real conclusion, just a lot of almost hysterical pleas about fighting global warming, blaming the authorities, and urging the people to recycle. Leaving me slightly amused, it also made me curious to dig some stuff up and this is what I've stumbled upon. Page with basic info on the subject, short sentences, pretty pictures and links. Alarmist (but not necessarily false) article of the let's-run-around-like-headless-chickens-while-waving-our-hands-because-the-Apocalypse-is-upon-us type, like the ones we are l...