Compiler Constructer
Compiler Constructer
COMPILER CONSTRUCTER
COMPILER
Definition
A program that reads a program written in one language (source language) and translates it
into an equivalent program in another language (target language).
• Two components
• Understand the program (make sure it is correct)
• Rewrite the program in the target language
Traditionally, the source language is a high level language and the target language is a low level
language (machine code).
Source Target
compiler
program program
Error message
2
COMPILER (DEFINITIONS)
Definition
A compiler is a special program that translates a programming languages
source code into machine code, bytecode or another programming language.
The source code is typically written in a high-level, human-readable language
such as java, C++.
OR
A program that convert a high-level language into low-level language or machine code or
Assembly language.
3
•Identify the lexical units in a source code
•Classify lexical units into classes like constants,
reserved words, and enter them in different
PHASES OF COMPILER
Lexical Analysis tables. It will Ignore comments in the source
program
•Identify token which is not a part of the language
•Helps you to store type information gathered and save it in symbol table
or syntax tree
•Allows you to perform type checking
Semantic Analysis •In the case of type mismatch, where there are no exact type correction
rules which satisfy the desired operation a semantic error is shown
•Collects type information and checks for type compatibility
4
•Checks if the source language permits the operands or not
LEXICAL ANALYSIS
Lexical Analysis is the first phase when compiler scans the source code. This process can be left to right,
character by character, and group these characters into tokens.
Here, the character stream from the source program is grouped in meaningful sequences by identifying the
tokens. It makes the entry of the corresponding tickets into the symbol table and passes that token to next
phase.
SYNTAX ANALYSIS
Syntax analysis is all about discovering structure in code. It determines whether or not a text follows the
expected format. The main aim of this phase is to make sure that the source code was written by the
programmer is correct or not.
Syntax analysis is based on the rules based on the specific programing language by constructing the parse
tree with the help of tokens. It also determines the structure of source language and grammar or syntax of the
language.
SEMANTIC ANALYSIS
Semantic analysis checks the semantic consistency of the code. It uses the syntax tree of the previous phase
along with the symbol table to verify that the given source code is semantically consistent. It also checks
whether the code is conveying an appropriate meaning.
Semantic Analyzer will check for Type mismatches, incompatible operands, a function called with improper
arguments, an undeclared variable, etc.
867
Terminologies of lexical analysis
There are three terminologies-
• 1. Token
• 2. Pattern
• 3. Lexeme
Token: It is a sequence of characters that represents a unit of information in the source
code.
Pattern: The description used by the token is known as a pattern.
Lexeme: A sequence of characters in the source code, as per the matching pattern of a
of a token, is known as lexeme. It is also called the instance of a token.
Roles and Responsibility of Lexical Analyzer
8
program gcd ( input ,
output ) ;
var i , j :
integer ; begin
read ( i , j
) ; while
LEXICAL i
if i
<> j
>
do
j
then i := i -
ANALYSIS :=
j
i
else
-
j
i ; writeln (
i
) end .
9
Y= 2*X
Y identifier
= assignment
* operator
2 number
X identifier
Example:
x = y + 10
Tokens
X identifier
= Assignment operator
Y identifier
+ Addition operator
10 Number
Roles and Responsibilities of Syntax Analyzer
EXAMPLE
PRESENTED
BY
AMJAD ALI THANK YOU