Compiler Design

 

R. Rajkumar

AP / CSE

SRM IST

Left Recursion Elimination 

1. Learn the fundamentals of the Design of Compilers by applying mathematics and
engineering principles
2. Design a system for parsing the sentences in a compiler grammar 
3. Design a system to translate into various intermediate codes 
4. Analyze the methods of implementing a Code Generator for compilers 
5. Analyze and Design the methods of developing a Code Optimizer 

First and follow  

Text

A compiler translates the code written in one language to some other language without changing the meaning of the program. It is also expected that a compiler should make the target code efficient and optimized in terms of time and space.

Compiler design principles provide an in-depth view of translation and optimization process. Compiler design covers basic translation mechanism and error detection & recovery. It includes lexical, syntax, and semantic analysis as front end, and code generation and optimization as back-end.

Phases of a compiler: A compiler operates in phases. A phase is a logically interrelated operation that takes source program in one representation and produces output in another representation.

The phases of a compiler are shown in below
There are two phases of compilation.


a. Analysis (Machine Independent/Language Dependent)
b. Synthesis(Machine Dependent/Language independent)
Compilation process is partitioned into no-of-sub processes called ‘phases’.

Lexical Analysis:-
LA or Scanners reads the source program one character at a time, carving the source program into a
sequence of automic units called tokens.

Syntax Analysis:-
The second stage of translation is called Syntax analysis or parsing. In this phase expressions,
statements, declarations etc… are identified by using the results of lexical analysis.

Syntax analysis is aided by using techniques based on formal grammar of the programming language.


Intermediate Code Generations:-
An intermediate representation of the final machine language code is produced. This phase bridges
the analysis and synthesis phases of translation.


 

Code Optimization :-
This is optional phase described to improve the intermediate code so that the output runs faster and
takes less space.


Code Generation:-
The last phase of translation is code generation. A number of optimizations to reduce the length of
machine language program are carried out during this phase. The output of the code generator is
the machine language program of the specified computer.

LAB Exp 1 

Exp . No 2

NFA to DFA

INPUT:     An NFA N
OUTPUT: A DFA D accepting the same language.

METHOD: Construct a transition table DTrans. Each DFA state is a set of NFA states. DTran simulates in parallel all possible moves N can make on a given string.

Operations to keep track of sets of NFA states:

ε Closure (S)

Set of states reachable from state S via epsilon.

ε Closure (T)

Set of states reachable from any state in set T via epsilon.

move (T, a)

Set of states to which there is an NFA transition from states in T on a symbol a.

Algorithm:

    initially, ε-Closure (S0) in DTrans.
    While unmarked state T in DTrans
    mark T
    for each input symbol 'a'
        do u = ε Closure (T, a)
            If u is not in DTrans
                then add u to DTrans
            DTrans [T, a] = U

Text

Text

LAB Exercise #4

First and Follow

SLR Parsing  

 Consider the grammar E -> T+E | T

                     T ->id
    Augmented grammar - E’ -> E
                E -> T+E | T
                T -> id

 Every SLR grammar is unambiguous but their are many unambiguous grammars that are not SLR.

CLR PARSER

Title Text

Compiler Design

By R Rajkumar

Compiler Design

  • 1,406