SlideShare a Scribd company logo
1
COMPILER
A compiler is a computer program that transforms
source code written in a programming language (the
source language) into another computer language (the
target language), with the latter takes binary form
known as object code
It create an executable program
2
Cause
Software for early computers was written in
assembly language
The benefits of reusing software on
different CPUs started to become
significantly greater than the cost of writing
a compiler
The first real compiler
FORTRAN compilers of the late 1950s
18 person-years to build
3
Any compiler must perform two major tasks
Analysis of the source program
Synthesis of a machine-language program
Structure of Compiler
4
THE STRUCTURE OF A COMPILER (2)
5
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all Phases of The Compiler)
(Character Stream)
Intermediate
Representation
Target machine code
THE STRUCTURE OF A COMPILER (3)
6
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Scanner
 The scanner begins the analysis of the source program by
reading the input, character by character, and grouping
characters into individual words and symbols (tokens)
 RE ( Regular expression )
 NFA ( Non-deterministic Finite Automata )
 DFA ( Deterministic Finite Automata )
 LEX
(Character Stream)
Intermediate
Representation
Target machine code
THE STRUCTURE OF A COMPILER (4)
7
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Parser
 Given a formal syntax specification (typically as a [CFG] ),
the parse reads tokens and groups them icontext-free
grammar nto units as specified by the productions of the
CFG being used.
 As syntactic structure is recognized, the parser either calls
corresponding semantic routines directly or builds a syntax
tree.
 CFG ( Context-Free Grammar )
 BNF ( Backus-Naur Form )
 GAA ( Grammar Analysis Algorithms )
 LL, LR, SLR, LALR Parsers
 YACC
(Character Stream)
Intermediate
Representation
Target machine code
THE STRUCTURE OF A COMPILER (5)
8
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program
(Character Stream)
Tokens Syntactic
Structure
Intermediate
Representation
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Semantic Routines
 Perform two functions
 Check the static semantics of each construct
 Do the actual translation
 The heart of a compiler
 Syntax Directed Translation
 Semantic Processing Techniques
 IR (Intermediate Representation)
Target machine code
THE STRUCTURE OF A COMPILER (6)
9
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Optimizer
 The IR code generated by the semantic routines is
analyzed and transformed into functionally equivalent but
improved IR code
 This phase can be very complex and slow
 Peephole optimization
 loop optimization, register allocation, code scheduling
 Register and Temporary Management
 Peephole Optimization
(Character Stream)
Intermediate
Representation
Target machine code
THE STRUCTURE OF A COMPILER (7)
10
Source
Program
(Character Stream)
Scanner
Tokens
Parser
Syntactic
Structure
Semantic
Routines
Intermediate
Representation
Optimizer
Code
Generator
Code Generator
 Interpretive Code Generation
 Generating Code from Tree/Dag
 Grammar-Based Code Generator
Target machine code
THE STRUCTURE OF A COMPILER (8)
11
Scanner
[Lexical Analyzer]
Parser
[Syntax Analyzer]
Semantic Process
[Semantic analyzer]
Code Generator
[Intermediate Code Generator]
Code Optimizer
Tokens
Parse tree
Abstract Syntax Tree w/ Attributes
Non-optimized Intermediate Cod
Optimized Intermediate Code
Code Optimizer
Target machine code
Language Description
Identifier Rules
•Identifier can be of maximum length 6.
•Identifiers are not case sensitive.
•An Indetifier can only have alphanumeric characters( a-z
, A-Z , 0-9 ) and underscore(_).
•The first character of an identifier can only contain
alphabet( a-z , A-Z ).
•Keywords are not allowed to be used as Identifiers.
•No special characters, such as semicolon, period,
whitespaces, slash or comma are permitted to be used in
or as Identifier.
12
Data Types:
Our language supports only 3 datatypes
•Integer
•String
•Character
Expressions
1.Arithmetic operators (+, -, *, /, %)
2.Uniray operator
3.Paranthesis
4.Only Integer supported
5.Relational expression to be supported (>, <, >=, <=, ==, !=)
6. Character string and integer constants
13
Statements
•Declaration statement : int a;
•Declaration and Initialisation : int a=5;
•Assingment Statement : a=6;
Conditional statement
Simple if (nesting not allowed)
if then
Endif
Switch Statement (nesting not allowed)
Switch()
Cases
Value 1:
Break;
Value n:
break;
Endcase
14
Repetition Statement (nesting not allowed)
a.Repeat
Until ()
a.While (relational expression)
Endwhile
a.For = start value, end value, inc/dec
………
Endfor
4
I/O Statement
•Input ;
•Output ;
Program Structure
Decleration:
Start
End 15
1.Sample Program I
#mode 10
declaration
int r
int c
int in
int flg
start
r = 0
flg = 1
while( flg == 1 )
if( c == 0) then
flg = 0
endif
c = c-1
endwhile
end
16
OUTPUT 1
START:
MOV AX, @DATA
MOV DS, AX
MOV AX,
MOV r, AX
MOV AX,
MOV flg, AX
LB01:
MOV AX,
CMP AX,
JNE LB01
MOV AX,
CMP AX,
JNE LB01
MOV AX,
MOV flg, AX
LB02:
MOV AX,
SUB AX,
MOV c, AX
JMP LB01
LB03:
MOV AX, 4C00H
INT 21H
END START
17
Sample Program II
#mode 10
declaration
int a ; b
int i
int k
string mes1
start
k=k*1
if(i<9 )then
i=i+9
k=k*1
endif
i=i-45
repeat
i=i+9*k+b
k=k*1
output "Hello World"
input k
until(i<2 )
while(k>3 )
i=i+9
k=k*1 endwhile
end
18
OUTPUT
START:
MOV AX, @DATA
MOV DS, AX
MOV AX, k
MUL 1
MOV k, AX
MOV AX, i
CMP AX, 9
JGE LB01
MOV AX, i
ADD AX, 9
MOV i, AX
MOV AX, k
MUL 1
MOV k, AX
LB01:
MOV AX, i
SUB AX, 45
MOV i, AX
LB02:
MOV AX, i
ADD AX, 9
MUL k
ADD AX, b
MOV i, AX
MOV AX, k
MUL 1
MOV k, AX 19
OUTPUT
LEA DX, "Hello World"
CALL MESSAGE
CALL INDEC
MOV k, AX
MOV AX, i
CMP AX, 2
JGE LB01
LB03:
MOV AX,
CMP AX, 3
JLE LB01
MOV AX, i
ADD AX, 9
MOV i, AX
MOV AX, k
MUL 1
MOV k, AX
JMP LB01
MOV AX, i
ADD AX, 9
MOV i, AX
MOV AX, k
MUL 1
MOV k, AX
JMP LB01
LB04:
MOV AX, 4C00H
INT 21H
END START
20
SCREENSHOTS
21
22
23
Feasibility and future scope
With the growth of technology ease of working is given
priority.
We have emerged from C , C++ to python ,ruby , etc. which
require less lines of code .
Our project can be extended to form a new language which is
easy to learn, faster , has more inbuilt features and has many
more qualities of a good programming language.
24
Conclusion
In a compiler the process of Intermediate code generation is
independent of machine and the process of conversion of
Intermediate code to target code is independent of language
used.
Thus we have done the front end of compilation process.
It includes 3 phases of compilation
lexical analysis
syntax analysis
semantic analysis
Followed by intermediate code generation.
25
References
•Salomaa, Arto [1973]. Formal Languages. Academic Press,
New York
•Schulz, Waldean A. [1976]. Semantic Analysis and Target
Language Synthesis in a Translator.Ph.D. thesis, University of
Colorado, Boulder, CO.
•https://www.cs.vt.edu/undergraduate/courses/CS4304
26
27

More Related Content

Similar to Structure-Compiler-phases information about basics of compiler. Pdfpdf

Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)
omercomail
 
Cd unit i
Cd unit iCd unit i
Cd unit i
Abhimanyu Mishra
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
DrIsikoIsaac
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Ashwini Sonawane
 
Msc prev completed
Msc prev completedMsc prev completed
Msc prev completed
mshoaib15
 
Msc prev updated
Msc prev updatedMsc prev updated
Msc prev updated
mshoaib15
 
Ch1 (1).ppt
Ch1 (1).pptCh1 (1).ppt
Ch1 (1).ppt
MDSayem35
 
basics of compiler design
basics of compiler designbasics of compiler design
basics of compiler design
Preeti Katiyar
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
DrIsikoIsaac
 
Unit1 cd
Unit1 cdUnit1 cd
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
abdulbaki3
 
Compiler design
Compiler designCompiler design
Compiler design
suganyasanjai
 
Cd econtent link1
Cd econtent link1Cd econtent link1
Cd econtent link1
suganyasanjai
 
Compiler
Compiler Compiler
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt
Rakesh Kumar
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
NesredinTeshome1
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
akila viji
 
Workshop Assembler
Workshop AssemblerWorkshop Assembler
Workshop AssemblerTuhin_Das
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Dr. Jaydeep Patil
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
A. S. M. Shafi
 

Similar to Structure-Compiler-phases information about basics of compiler. Pdfpdf (20)

Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)
 
Cd unit i
Cd unit iCd unit i
Cd unit i
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Msc prev completed
Msc prev completedMsc prev completed
Msc prev completed
 
Msc prev updated
Msc prev updatedMsc prev updated
Msc prev updated
 
Ch1 (1).ppt
Ch1 (1).pptCh1 (1).ppt
Ch1 (1).ppt
 
basics of compiler design
basics of compiler designbasics of compiler design
basics of compiler design
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
 
Unit1 cd
Unit1 cdUnit1 cd
Unit1 cd
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 
Compiler design
Compiler designCompiler design
Compiler design
 
Cd econtent link1
Cd econtent link1Cd econtent link1
Cd econtent link1
 
Compiler
Compiler Compiler
Compiler
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
 
Workshop Assembler
Workshop AssemblerWorkshop Assembler
Workshop Assembler
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 

Recently uploaded

Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 

Recently uploaded (20)

Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 

Structure-Compiler-phases information about basics of compiler. Pdfpdf

  • 1. 1
  • 2. COMPILER A compiler is a computer program that transforms source code written in a programming language (the source language) into another computer language (the target language), with the latter takes binary form known as object code It create an executable program 2
  • 3. Cause Software for early computers was written in assembly language The benefits of reusing software on different CPUs started to become significantly greater than the cost of writing a compiler The first real compiler FORTRAN compilers of the late 1950s 18 person-years to build 3
  • 4. Any compiler must perform two major tasks Analysis of the source program Synthesis of a machine-language program Structure of Compiler 4
  • 5. THE STRUCTURE OF A COMPILER (2) 5 Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) (Character Stream) Intermediate Representation Target machine code
  • 6. THE STRUCTURE OF A COMPILER (3) 6 Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) Scanner  The scanner begins the analysis of the source program by reading the input, character by character, and grouping characters into individual words and symbols (tokens)  RE ( Regular expression )  NFA ( Non-deterministic Finite Automata )  DFA ( Deterministic Finite Automata )  LEX (Character Stream) Intermediate Representation Target machine code
  • 7. THE STRUCTURE OF A COMPILER (4) 7 Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) Parser  Given a formal syntax specification (typically as a [CFG] ), the parse reads tokens and groups them icontext-free grammar nto units as specified by the productions of the CFG being used.  As syntactic structure is recognized, the parser either calls corresponding semantic routines directly or builds a syntax tree.  CFG ( Context-Free Grammar )  BNF ( Backus-Naur Form )  GAA ( Grammar Analysis Algorithms )  LL, LR, SLR, LALR Parsers  YACC (Character Stream) Intermediate Representation Target machine code
  • 8. THE STRUCTURE OF A COMPILER (5) 8 Scanner Parser Semantic Routines Code Generator Optimizer Source Program (Character Stream) Tokens Syntactic Structure Intermediate Representation Symbol and Attribute Tables (Used by all Phases of The Compiler) Semantic Routines  Perform two functions  Check the static semantics of each construct  Do the actual translation  The heart of a compiler  Syntax Directed Translation  Semantic Processing Techniques  IR (Intermediate Representation) Target machine code
  • 9. THE STRUCTURE OF A COMPILER (6) 9 Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) Optimizer  The IR code generated by the semantic routines is analyzed and transformed into functionally equivalent but improved IR code  This phase can be very complex and slow  Peephole optimization  loop optimization, register allocation, code scheduling  Register and Temporary Management  Peephole Optimization (Character Stream) Intermediate Representation Target machine code
  • 10. THE STRUCTURE OF A COMPILER (7) 10 Source Program (Character Stream) Scanner Tokens Parser Syntactic Structure Semantic Routines Intermediate Representation Optimizer Code Generator Code Generator  Interpretive Code Generation  Generating Code from Tree/Dag  Grammar-Based Code Generator Target machine code
  • 11. THE STRUCTURE OF A COMPILER (8) 11 Scanner [Lexical Analyzer] Parser [Syntax Analyzer] Semantic Process [Semantic analyzer] Code Generator [Intermediate Code Generator] Code Optimizer Tokens Parse tree Abstract Syntax Tree w/ Attributes Non-optimized Intermediate Cod Optimized Intermediate Code Code Optimizer Target machine code
  • 12. Language Description Identifier Rules •Identifier can be of maximum length 6. •Identifiers are not case sensitive. •An Indetifier can only have alphanumeric characters( a-z , A-Z , 0-9 ) and underscore(_). •The first character of an identifier can only contain alphabet( a-z , A-Z ). •Keywords are not allowed to be used as Identifiers. •No special characters, such as semicolon, period, whitespaces, slash or comma are permitted to be used in or as Identifier. 12
  • 13. Data Types: Our language supports only 3 datatypes •Integer •String •Character Expressions 1.Arithmetic operators (+, -, *, /, %) 2.Uniray operator 3.Paranthesis 4.Only Integer supported 5.Relational expression to be supported (>, <, >=, <=, ==, !=) 6. Character string and integer constants 13
  • 14. Statements •Declaration statement : int a; •Declaration and Initialisation : int a=5; •Assingment Statement : a=6; Conditional statement Simple if (nesting not allowed) if then Endif Switch Statement (nesting not allowed) Switch() Cases Value 1: Break; Value n: break; Endcase 14
  • 15. Repetition Statement (nesting not allowed) a.Repeat Until () a.While (relational expression) Endwhile a.For = start value, end value, inc/dec ……… Endfor 4 I/O Statement •Input ; •Output ; Program Structure Decleration: Start End 15
  • 16. 1.Sample Program I #mode 10 declaration int r int c int in int flg start r = 0 flg = 1 while( flg == 1 ) if( c == 0) then flg = 0 endif c = c-1 endwhile end 16
  • 17. OUTPUT 1 START: MOV AX, @DATA MOV DS, AX MOV AX, MOV r, AX MOV AX, MOV flg, AX LB01: MOV AX, CMP AX, JNE LB01 MOV AX, CMP AX, JNE LB01 MOV AX, MOV flg, AX LB02: MOV AX, SUB AX, MOV c, AX JMP LB01 LB03: MOV AX, 4C00H INT 21H END START 17
  • 18. Sample Program II #mode 10 declaration int a ; b int i int k string mes1 start k=k*1 if(i<9 )then i=i+9 k=k*1 endif i=i-45 repeat i=i+9*k+b k=k*1 output "Hello World" input k until(i<2 ) while(k>3 ) i=i+9 k=k*1 endwhile end 18
  • 19. OUTPUT START: MOV AX, @DATA MOV DS, AX MOV AX, k MUL 1 MOV k, AX MOV AX, i CMP AX, 9 JGE LB01 MOV AX, i ADD AX, 9 MOV i, AX MOV AX, k MUL 1 MOV k, AX LB01: MOV AX, i SUB AX, 45 MOV i, AX LB02: MOV AX, i ADD AX, 9 MUL k ADD AX, b MOV i, AX MOV AX, k MUL 1 MOV k, AX 19
  • 20. OUTPUT LEA DX, "Hello World" CALL MESSAGE CALL INDEC MOV k, AX MOV AX, i CMP AX, 2 JGE LB01 LB03: MOV AX, CMP AX, 3 JLE LB01 MOV AX, i ADD AX, 9 MOV i, AX MOV AX, k MUL 1 MOV k, AX JMP LB01 MOV AX, i ADD AX, 9 MOV i, AX MOV AX, k MUL 1 MOV k, AX JMP LB01 LB04: MOV AX, 4C00H INT 21H END START 20
  • 22. 22
  • 23. 23
  • 24. Feasibility and future scope With the growth of technology ease of working is given priority. We have emerged from C , C++ to python ,ruby , etc. which require less lines of code . Our project can be extended to form a new language which is easy to learn, faster , has more inbuilt features and has many more qualities of a good programming language. 24
  • 25. Conclusion In a compiler the process of Intermediate code generation is independent of machine and the process of conversion of Intermediate code to target code is independent of language used. Thus we have done the front end of compilation process. It includes 3 phases of compilation lexical analysis syntax analysis semantic analysis Followed by intermediate code generation. 25
  • 26. References •Salomaa, Arto [1973]. Formal Languages. Academic Press, New York •Schulz, Waldean A. [1976]. Semantic Analysis and Target Language Synthesis in a Translator.Ph.D. thesis, University of Colorado, Boulder, CO. •https://www.cs.vt.edu/undergraduate/courses/CS4304 26
  • 27. 27