0% found this document useful (0 votes)
14 views

Compiler Constructer

Amjad Ali presented on compiler construction. A compiler is a program that translates source code written in one language into another target language. It has three main phases - lexical analysis, syntax analysis, and semantic analysis. Lexical analysis identifies tokens. Syntax analysis checks syntax and constructs a parse tree. Semantic analysis performs type checking and ensures semantic consistency.

Uploaded by

Amjad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Compiler Constructer

Amjad Ali presented on compiler construction. A compiler is a program that translates source code written in one language into another target language. It has three main phases - lexical analysis, syntax analysis, and semantic analysis. Lexical analysis identifies tokens. Syntax analysis checks syntax and constructs a parse tree. Semantic analysis performs type checking and ensures semantic consistency.

Uploaded by

Amjad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

NAME AMJAD ALI

DEPARTMENT COMPUTER SCIENCE


BATCH 10

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

•Obtain tokens from the lexical analyzer


•Checks if the expression is syntactically correct or not
Syntax Analysis •Report all syntax errors
•Construct a hierarchical structure which is known as a parse
tree

•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

The lexical analyzer performs the following tasks-


•The lexical analyzer is responsible for removing the white spaces and comments from the
• source program.
• It corresponds to the error messages with the source program.
•It helps to identify the tokens.
•The input characters are read by the lexical analyzer from source code.
program gcd (input,
output);
var i, j : integer;
begin
read (i, j);
LEXICAL while i <> j do
if i > j then
ANALYSIS i := i - j else j :=
j - i;
writeln (i)
end.

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

•Note syntax errors.


•Helps in building a parse tree.
•Acquire tokens from the lexical analyzer.
•Scan the syntax errors.
•it can defines the syntactic categorizes for language constructs language s for
language constructs
Statements
Expressions
Declarations
In Parse Tree
•Interior node: record with an operator filed and two files for children
•Leaf: records with 2/more fields; one for token and other information about the token
•Ensure that the components of the program fit together meaningfully
•Gathers type information and checks for type compatibility
•Checks operands are permitted by the source language
Roles and Responsibilities of Semantic Analyzer:

•Saving collected data to symbol tables or syntax trees.


•It notifies semantic errors.
•Scanning for semantic errors.
 Helps you to store type information gathered and save it in symbol table or syntax tree
 Allows you to perform type checking
 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
 Checks if the source language permits the operands or not

float x = 20.2; float y = x*30;

EXAMPLE
PRESENTED
BY
AMJAD ALI THANK YOU

You might also like