Structure Of A Compiler

The second lesson of Nathan’s University was made available today.  The goal of this class is create your own programming language so I’ve decided to brush up some of my old notes from when I was getting burned by dragons. I’ve didn’t go in too much detail but if you can do a little of your own research if you want to learn more about components of a compilers.

You can check out the second lesson here.

http://nathansuniversity.com/music.html

What’s A Compiler?
The components of a compiler can be broken down into two categories, analysis aka “frontend” and synthesis aka “backend”.

During the analysis phases, the source program is divided into segments and reorganized with grammatical structure. Next an intermediate version of the source code is created. Also during this phase, information collected on the source code is stored within a symbol table. Together with the symbol table and an intermediate version of the code, the compiler can move onto the second phase, synthesis. At the synthesis stage, machine code is generated from the symbol table and intermediate code.

General Compiler Process Cycle
Here is lame table outlining the process cycle of a compiler. This table is broken down into input, complier phase, and output.

Input Compiler Phase Output
Character Stream Lexical Analyzer Token Stream
Token Stream Syntax Analyzer Syntax Tree
Syntax Tree Semantic Analyzer Syntax Tree
Syntax Tree Intermediate Code Generator Intermediate Representation
Intermediate Representation Code Optimizer Intermediate Representation
Intermediate Representation Code Generator Machine Code
Machine Code Machine Code Optimizer Machine Code

What’s Lexical Analysis?
Lexical Analysis aka “scanning” phases is where the character streams of the source code are grouped into sequential groups called lexemes. A token is created for each lexeme.

What’s Syntax Analysis?
Syntax Analysis aka “parsing” creates a tree like structure using the tokens created in during the lexical analysis.

What is Semantic Analysis?
The role of the semantic analysis phase is to check the source program against the language definition. The main role of semantic analysis is type checking.

What is Intermediate Code Generation?
During the intermediate code generation is where the code is broken down into a more machine like language referred as intermediate representation.

What is Code Optimization?
During the code optimization is where the intermediate representation is altered for performance. Code can be altered to use less power and redundant code can be eliminated or shortened.

What is Code Generation?
During the code optimization is where the intermediate representation is ported to the target language such as machine code or memory locations.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s