×
In order to succeed, we must first believe that we can.
--Your friends at LectureNotes
Close

Note for Compiler Design - CD By Sai Swaroop Bedamatta

  • Compiler Design - CD
  • Note
  • 88 Views
  • 3 Offline Downloads
  • Uploaded 10 months ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

Lecture Notes on Principles of Complier Design By D.R.Nayak,Aasst.prof Govt.College of Engg.Kalahandi,Bhawanipatna 1. Introduction to Compilers What is Compiler? Compiler is a program which translates source program written in one language to an equivalent program in other language (the target language). Usually the source language is a high level language like Java, C, C++ etc. whereas the target language is machine code or "code" that a computer's processor understands. Simple Design of Complier Many modern compilers have a common 'two stage' design. The "front end" translates the source language or the high level program into an intermediate representation. The second stage is the "back end", which works with the internal representation to produce low level code . The Enhanced Design

Text from page-2

Phases of complier Lexical Analysis  Recognizing words is not completely trivial. For example: ist his ase nte nce?  Therefore, we must know what the word separators are  The language must define rules for breaking a sentence into a sequence of words.  Normally white spaces and punctuations are word separators in languages.  In programming languages a character from a different class may also be treated as word separator.  The lexical analyzer breaks a sentence into a sequence of words or tokens: - If a == b then a = 1 ; else a = 2 ; - Sequence of words (total 14 words) if a == b then a = 1 ; else a = 2 ; In simple words, lexical analysis is the process of identifying the words from an input string of characters, which may be handled more easily by a parser. These words must be separated by some predefined delimiter or there may be some rules imposed by the language for breaking the sentence into tokens or words which are then passed on to the next phase of syntax analysis. The Second step Syntax checking or parsing Syntax analysis is a process of imposing a hierarchical structure on the token stream. It is basically like generating sentences for the language using language specific grammatical rules.

Text from page-3

Semantic Analysis Since it is too hard for a compiler to do semantic analysis, the programming languages define strict rules to avoid ambiguities and make the analysis easier. This has been done by putting one outside the scope of other so that the compiler knows that these two aditya are different by the virtue of their different scopes. { int Aditya = 4; { int Aditya = 6; cout << Aditya; } } Code Optimization .It is the optional phase and Run faster - Use less resource (memory, registers, space, fewer fetches etc.) - Common sub-expression elimination - Copy propagation - Dead code elimination - Code motion - Strength reduction - Constant folding Example1: int x = 3; int y = 4; int *array[5]; for (i=0; i<5;i++) *array[i] = x + y; Because x and y are invariant and do not change inside of the loop, their addition doesn't need to be performed for each loop iteration. Almost any good compiler optimizes the code. An optimizer moves the addition of x and y outside the loop, thus creating a more efficient loop. Thus, the optimized code in this case could look like the following: int x = 3; int y = 4; int z = x + y; int *array[5]; for (i=0; i<5;i++) *array[i] = z; Some of the different optimization methods are: 1) Constant Folding - replacing y= 5+7 with y=12 or y=x*0 with y=0 2) Dead Code Elimination - e.g., If (false) a = 1; else

Text from page-4

a = 2; with a = 2; 3) Peephole Optimization - a machine-dependent optimization that makes a pass through low-level assembly-like instruction sequences of the program( called a peephole), and replacing them with a faster (usually shorter) sequences by removing redundant register loads and stores if possible. 4) Flow of Control Optimizations 5) Strength Reduction - replacing more expensive expressions with cheaper ones - like pow(x,2) with x*x 6) Common Sub expression elimination - like a = b*c, f= b*c*d with temp = b*c, a= temp, f= temp*d; Code Generation Usually a two step process - Generate intermediate code from the semantic representation of the program - Generate machine code from the intermediate code Intermediate Code Generation 1. Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user defined, system defined or libraries) 2. Abstraction at the target level memory locations, registers, stack, opcodes, addressing modes, system libraries, interface to the operating systems 3. Code generation is mapping from source level abstractions to target machine abstractions 4. Map identifiers to locations (memory/storage allocation) 5. Explicate variable accesses (change identifier reference to relocatable/absolute address Intermediate code generation

Lecture Notes