Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
COMPILER DESIGN
INTRODUCTION
Richa Sharma 1
MAIN REFERENCE BOOK:
• COMPILERS – PRINCIPLES, TECHNIQUES AND TOOLS, SECOND EDITION BY
ALFRED V. AHO, RAVI SETHI, JEFFERY ...
COMPILER
• IT’S A SOFTWARE UTILITY THAT TRANSLATED HIGH LANGUAGE CODE INTO TARGET LANGUAGE
CODE ,AS COMPUTER DOESN’T UNDER...
INTERPRETER
• IT’S ANOTHER SOFTWARE UTILITY THAT TRANSLATES HIGH LANGUAGE CODE INTO TARGET
LANGUAGE CODE LINE BY LINE UNLI...
EXAMPLES
MOST LANGUAGES ARE USUALLY THOUGHT OF AS USING EITHER
ONE OR THE OTHER:
• COMPILERS: FORTRAN, COBOL, C, C++, PASC...
Preprocessor
Compiler
Assembler
Linker/Loader
Source program
Modified Source program
Target assembly program
Relocatable m...
PHASES OF COMPILER
THE TRANSLATION OF INPUT FILE INTO TARGET CODE IS DIVIDED INTO 2 STAGES :
1. FRONT END (ANALYSIS): TRAN...
PHASES OF COMPILER
Richa Sharma 8
Symbol table Error handler
Front End
Back End
LEXICAL ANALYSIS/SCANNING
 READS THE STREAM OF CHARACTERS MAKING UP THE SOURCE PROGRAM AND GROUP THE
CHARACTERS INTO MEAN...
SYNTAX ANALYSIS/PARSING
• RECOGNIZES “SENTENCES” IN THE PROGRAM USING THE SYNTAX OF THE LANGUAGE
• CREATES TREE LIKE STRUC...
SEMANTIC ANALYSIS
• INFERS INFORMATION ABOUT THE PROGRAM USING THE SEMANTICS OF THE LANGUAGE
• USES SYNTAX TREE AND INFO. ...
INTERMEDIATE CODE GENERATION
• GENERATES “ABSTRACT” CODE BASED ON THE SYNTACTIC STRUCTURE OF THE PROGRAM AND THE SEMANTIC
...
CODE OPTIMIZATION
• REFINES THE GENERATED CODE USING A SERIES OF OPTIMIZING TRANSFORMATIONS.
• Eg: REMOVING DEAD CODE .
RE...
CODE GENEARTION
• MAP INSTRUCTIONS IN THE INTERMEDIATE CODE TO SPECIFIC MACHINE INSTRUCTIONS.
• SUPPORTS STANDARD OBJECT F...
SYMBOL TABLE
• SYMBOL TABLE – DATA STRUCTURE WITH A RECORD FOR EACH IDENTIFIER AND ITS ATTRIBUTES
• ALL THE PHASES ARE CON...
Richa Sharma
16
OVERALL WORKING OF COMPILER PHASES
COMPILER CONSTRUCTION TOOLS
• DEVELOPER MAY USE MODERN SOFTWARE DEVELOPMENT ENVIRONMENT CONTAINING
TOOLS Ex: LANGUAGE EDIT...
APPLICATION OF COMPILER TECHNOLOGY
• IMPLEMENTATION OF HIGH LEVEL PROGRAMMING LANGUAGES
- CONCEPTS OF OOPS
• OPTIMIZATION ...
Richa Sharma 19
Upcoming SlideShare
Loading in …5
×

Compiler Design Introduction

2,527 views

Published on

Compiler Design is quite important course from UGCNET /GATE point of view .This course clarifies different phases of language conversion.To have more insight refer http://tutorialfocus.net/

Published in: Engineering
  • Be the first to comment

Compiler Design Introduction

  1. 1. COMPILER DESIGN INTRODUCTION Richa Sharma 1
  2. 2. MAIN REFERENCE BOOK: • COMPILERS – PRINCIPLES, TECHNIQUES AND TOOLS, SECOND EDITION BY ALFRED V. AHO, RAVI SETHI, JEFFERY D. ULLMAN • PRINCIPLES OF COMPILER DESIGN BY V R RAGHAVAN . Richa Sharma 2
  3. 3. COMPILER • IT’S A SOFTWARE UTILITY THAT TRANSLATED HIGH LANGUAGE CODE INTO TARGET LANGUAGE CODE ,AS COMPUTER DOESN’T UNDERSTAND HIGH LANGUAGE. DATA OUTPUT • IMPORTANT ROLE OF COMPILER IS TO REPORT THE ERROR IN THE SOURCE PROGRAM BY TRANSLATING THE PROGRAM IN ONE GO. • STRUCTURE OF THE COMPILER IS OFFLINE, MEANING THAT WE PRE-PROCESS THE PROGRAM FIRST AND CREATES AND EXECUTABLE CODE AND THIS CODE CAN RUN ON DIFFERENT INPUTS OR DATA . Richa Sharma 3 COMPILER Source Program Target Program
  4. 4. INTERPRETER • IT’S ANOTHER SOFTWARE UTILITY THAT TRANSLATES HIGH LANGUAGE CODE INTO TARGET LANGUAGE CODE LINE BY LINE UNLIKE COMPILER . OUTPUT • INTERPRETER IS ONLINE MODE, IN WHICH DATA AND SOURCE PROGRAM ARE EXECUTED SIMULTANEOUSLY GIVING THE OUTPUT .NO PRE-PROCESSING IS DONE EARLIER. Richa Sharma 4 COMPILER Source Program Data
  5. 5. EXAMPLES MOST LANGUAGES ARE USUALLY THOUGHT OF AS USING EITHER ONE OR THE OTHER: • COMPILERS: FORTRAN, COBOL, C, C++, PASCAL, PL/1 • INTERPRETERS: LISP, SCHEME, BASIC, APL, PERL, PYTHON, SMALLTALK Richa Sharma 5
  6. 6. Preprocessor Compiler Assembler Linker/Loader Source program Modified Source program Target assembly program Relocatable machine code Target machine code Library filesRicha Sharma STEPS FOR LANGUAGE PROCESSING SYSTEM.
  7. 7. PHASES OF COMPILER THE TRANSLATION OF INPUT FILE INTO TARGET CODE IS DIVIDED INTO 2 STAGES : 1. FRONT END (ANALYSIS): TRANSFORM SOURCE CODE INTO INTERMEDIATE CODE ALSO CALLED INTERMEDIATE REPRESENTATION (IR) . IT’S A MACHINE-INDEPENDENT REPRESENTATION. 2. BACK END (SYNTHESIS): IT TAKES IR AND GENERATES THE TARGET ASSEMBLY LANGUAGE PROGRAM. FRONT END BACK END 1) LEXICAL ANALYZER (SCANNING) 5) CODE OPTIMIZATION 2) SYNTAX ANALYZER (PARSING) 6) MACHINE CODE GENERATION 3) SEMANTIC ANALYZER 4) INTERMEDIATE CODE GENERATION Richa Sharma 7
  8. 8. PHASES OF COMPILER Richa Sharma 8 Symbol table Error handler Front End Back End
  9. 9. LEXICAL ANALYSIS/SCANNING  READS THE STREAM OF CHARACTERS MAKING UP THE SOURCE PROGRAM AND GROUP THE CHARACTERS INTO MEANINGFUL SEQUENCES CALLED LEXEMES.  LEXEME ---- TOKEN < TOKEN-NAME, ATTRIBUTE-VALUE>  TOKEN NAME – IS AN ABSTRACT SYMBOL USED DURING SYNTAX ANALYSIS.  ATTRIBUTE VALUE – POINTS TO AN ENTRY IN THE SYMBOL TABLE FOR THIS TOKEN. POSITION = INITIAL + RATE * 60 <ID,1> < = > <ID,2> < +> <ID,3> < *> <60> Richa Sharma 9
  10. 10. SYNTAX ANALYSIS/PARSING • RECOGNIZES “SENTENCES” IN THE PROGRAM USING THE SYNTAX OF THE LANGUAGE • CREATES TREE LIKE STRUCTURE FROM TOKENS (SYNTAX TREE) • NODE REPRESENTS OPERATION • CHILDREN REPRESENTS ARGUMENTS • REPRESENTS THE SYNTACTIC STRUCTURE OF THE PROGRAM, HIDING A FEW DETAILS THAT ARE IRRELEVENT TO LATER PHASES OF COMPILATION. Richa Sharma 10
  11. 11. SEMANTIC ANALYSIS • INFERS INFORMATION ABOUT THE PROGRAM USING THE SEMANTICS OF THE LANGUAGE • USES SYNTAX TREE AND INFO. IN SYMBOL TABLE TO CHECK FOR SEMANTIC CONSISTENCY. • GATHERS TYPE INFO. AND SAVES IT IN EITHER THE SYNTAX TREE OR SYMBOL TABLE FOR USE IN ICG • TYPE CHECKING – CHECKS THAT EACH OPERATOR HAS MATCHING OPERANDS. E.G ARRAY INDEX SHOULD BE INTEGER. • TYPE CONVERSIONS CALLED COERCIONS • BINARY ARITHMETIC OPERATOR (INT OR FLOAT) • IF 6+7.5, THEN CONVERT 6 TO 6.5 Richa Sharma 11
  12. 12. INTERMEDIATE CODE GENERATION • GENERATES “ABSTRACT” CODE BASED ON THE SYNTACTIC STRUCTURE OF THE PROGRAM AND THE SEMANTIC INFORMATION FROM PHASE 2.  SYNTAX TREE ARE ALSO IR  COMPILER MAY PRODUCE EXPLICIT IR  IR HAS TWO PROPERTIES:  EASY TO PRODUCE  EASY TO TRANSLATE INTO TARGET MACHINE  E.G IR – THREE ADDRESS CODE  T1 = INTTOFLOAT(60)  T2 = ID3 * T1  T3 = ID2 + T3  ID1 = T3 Richa Sharma 12
  13. 13. CODE OPTIMIZATION • REFINES THE GENERATED CODE USING A SERIES OF OPTIMIZING TRANSFORMATIONS. • Eg: REMOVING DEAD CODE . REDUCING ITERATIONS AND LOOPS ETC.. • APPLY A SERIES OF TRANSFORMATIONS TO IMPROVE THE TIME AND SPACE EFFICIENCY OF THE GENERATED CODE. • PEEPHOLE OPTIMIZATIONS: GENERATE NEW INSTRUCTIONS BY COMBINING/EXPANDING ON A SMALL NUMBER OF CONSECUTIVE INSTRUCTIONS. • GLOBAL OPTIMIZATIONS: REORDER, REMOVE OR ADD INSTRUCTIONS TO CHANGE THE STRUCTURE OF GENERATED CODE. Richa Sharma 13
  14. 14. CODE GENEARTION • MAP INSTRUCTIONS IN THE INTERMEDIATE CODE TO SPECIFIC MACHINE INSTRUCTIONS. • SUPPORTS STANDARD OBJECT FILE FORMATS. • GENERATES SUFFICIENT INFORMATION TO ENABLE SYMBOLIC DEBUGGING.  IR -> CG -> TARGET LANGUAGE (E.G MACHINE CODE)  REGISTERS AND MEMORY LOCATIONS ARE SELECTED FOR EACH VARIABLE USED BY THE PROGRAM.  LDF R2, ID3  MULF R2, R2, #60.0  LDF R1, ID2  ADDF R1, R1, R2  STF ID1, R1  R1,R2 – REGISTERS F – FLOATING POINT NUMBERS # - IMMEDIATE CONST. Richa Sharma 14
  15. 15. SYMBOL TABLE • SYMBOL TABLE – DATA STRUCTURE WITH A RECORD FOR EACH IDENTIFIER AND ITS ATTRIBUTES • ALL THE PHASES ARE CONNECTED TO THE SYMBOL TABLE. • ATTRIBUTES INCLUDE STORAGE ALLOCATION, TYPE, SCOPE, ETC • ALL THE COMPILER PHASES INSERT AND MODIFY THE SYMBOL TABLE Richa Sharma 15
  16. 16. Richa Sharma 16 OVERALL WORKING OF COMPILER PHASES
  17. 17. COMPILER CONSTRUCTION TOOLS • DEVELOPER MAY USE MODERN SOFTWARE DEVELOPMENT ENVIRONMENT CONTAINING TOOLS Ex: LANGUAGE EDITORS, VERSION MANAGERS, TEC. • SOME SPECIAL TOOLS CAN ALSO BE USED. THESE TOOLS ARE THOSE WHICH HIDE THE DETAILS OF GENERATION ALGORITHM AND PRODUCE COMPONENTS THAT CAN BE EASILY INTEGRATED TO REMAINDER OF THE COMPILER. • PARSER GENERATOR: TAKES GRAMMAR DESCRIPTION AND PRODUCES SYNTAX ANALYSER. • SCANNER GENERATOR: TAKES REGULAR EXPRESSION AND PRODUCES LEXICAL ANALYSER. • AUTOMATIC CODE- GENERATOR : TAKES INTERMEDIATE AND CONVERT TO MACHINE LANGUAGE) • DATA FLOW ANALYSIS ENGINES (FOR OPTIMIZATION) • COMPILER CONSTRUCTION TOOLKIT. Richa Sharma 17
  18. 18. APPLICATION OF COMPILER TECHNOLOGY • IMPLEMENTATION OF HIGH LEVEL PROGRAMMING LANGUAGES - CONCEPTS OF OOPS • OPTIMIZATION FOR COMPUTER ARCHITECTURE - PARALLELISM - MEMORY HIERARCHIES OF MACHINES(REG, ARRAYS ETC) • DESIGN OF NEW COMPUTER ARCHITECURES - RISC - CISC - SIMD ETC • PROGRAM TRANSLATIONS • SOFTWARE PRODUCTIVITY TOOLS Richa Sharma 18
  19. 19. Richa Sharma 19

×