Chap1 (Minimized)
Chap1 (Minimized)
Introduction
Programming languages are notations for describing computations to people and to machines.
One of the language processor that translate a high level language in to low level languages are
compiler
A compiler translates (or compiles) a program written in a high-level programming language that is
suitable for human programmers into the low-level machine language that is required by
computers.
During this process, the compiler will also attempt to detect and report obvious programmer
mistakes.
03/19/2025 Compilers and Programming Languages
• Language Processor = Analysis of Source Program + Synthesis of
Target Program.
called the source language, and translate it into an equivalent program in another
A compiler also reports any errors in the source program that it detects during the
translation process.
A compiler acts as a translator, transforming human-oriented programming languages
into computer oriented machine languages.
It is a program that translates an executable program in HLL language into an
executable program in ML language.
The design of compiler focus on the following activities:
Scanning:-tokenizing-
Tokenization is the process of breaking up a sequence of strings
into pieces such as words, keywords, phrases, symbols and other
elements called tokens.
Parsing:-validating the syntax and semantic validity of the
given instructions.
Creating the symbol table:- for files and strings;
Resolving the forward references: symbol/variable is
referenced before it is declared.
object code conversion:-Converting into the machine language
The assembly language program is then processed by a program called assembler that
Linker: is a computer program that links and merges various object files together in
order to make an executable file. All these files might have been compiled by separate
assemblers.
Large programs are often compiled in pieces, so that the re-locatable machine code
may have to be linked with other re-locatable object files runs on the machine.
Loader: is responsible for loading executable files into memory and execute them.
It calculates the size of a program (instructions and data) and creates memory space
Function of loader
The process of Loading consists of taking re-locatable machine code, altering the
re-locatable addresses and placing the altered instructions and data in memory at
the proper location.
The linker allows us to make a single program from several files of re-locatable
machine code.
1
Lexical Analyzer
2
Syntax Analyzer
3
Semantic Analyzer
5
Code Optimizer
6
Code Generator
Target Program
03/19/2025 Compilers and Programming Languages
Phases of a Compile
characters making up the source program and groups them into meaningful sequences called lexemes.
Examples of tokens are identifiers, reserved words, integers, doubles or floats, delimiters, operators and
special symbols
int a;
a = a + 2;
int reserved word
a identifier
; special symbol
a identifier
= operator
a identifier
+ operator
2 integer constant
; special symbol
03/19/2025 Compilers and Programming Languages
Syntax Analysis or Parsing
The second phase of a compiler is syntax analysis or parsing. The parser uses the
tokens produced by the lexical analyzer to create a tree-like intermediate
representation – called syntax tree – that depicts the grammatical structure of the
token stream.
In a syntax tree, each interior node represents an operation and the children of the
node represent the influences of the operation.
Example of grammar rules:
This tree shows the order in which the operations in the assignment position =
initial + rate * 60 are to be performed.
03/19/2025 Compilers and Programming Languages
Semantic Analysis
The semantic analyzer uses the syntax tree and the information in the symbol table
to check the source program for semantic consistency with the language definition.
Checks source program for semantic errors (e.g. type checking).
Uses hierarchical structure determined by syntactic analysis to determine operators
and operands.
Parse tree is checked for things that violates the semantic rules of the language
Semantic rules may be written with an attribute grammar
• Examples:
– Using undeclared variables
– Array variables used without array syntax
– Type checking of operator arguments
– Left hand side of an assignment must be a variable
source target
code IR code
Front End Back End
Introduction
Advantages and Disadvantages of Compilers:
• Advantages: are Optimization
flexibility.
• Disadvantage is Lack of Speed.