Last Minute Notes – Compiler Design

LMNs – Compiler Design

We will discuss the important key points useful for GATE exams in summarized form. For details you may refer this.

Phases of Compiler :


Lexical Analysis : Lexical analyzer reads a source program character by character to produce tokens. Tokens can be identifiers, keywords, operators, separators etc.

Syntax Analysis : Syntax analyzer is also known as parser.

Left Recursion
: The grammar : A -> Aa | a is left recursive. Top down parsing techniques cannot handle left recursive grammar so we convert left recursion into right recursion.
Left recursion elimination : A -> Aa | a ⇒ A -> aA’
A’ -> aA’ | a

Left Factoring : If a grammar has common prefixes in r.h.s of nonterminal then suh grammar needs to be left factored by eliminating common prefixes as follows :
A -> ab1 | ac2 ⇒ A -> A -> aA’
A’ -> A -> b1 | c2

FIRST(A) is a set of the terminal symbols which occur as first symbols in string derived from A

FOLLOW(A) is the set of terminals which occur immediately after the nonterminal A in the strings derived from the starting symbol.

Screenshot from 2017-02-09 11-17-55

LL(1) Parser : LL(1) grammar is unambiguous, left factored and non left recursive.
To check whether a grammar is LL(1) or not :
1. If A -> B1 | C2 ⇒ { FIRST(B1) ∩ FIRST(C2 ) = ɸ }
2. If A -> B | ε ⇒ { FIRST(B) ∩ FOLLOW(A) = ɸ }


LR(0) Parser : Closure() and goto() functions are used to create canonical collection of LR items.
Conflicts in LR(0) parser :
1. Shift Reduce (SR) conflict : when the same state in DFA contains both shift and reduce items. A -> B . xC (shifting) B -> a. (reduced)
2. Reduced Reduced (RR) conflict : two reductions in same state of DFA A -> a. (reduced) B -> b. (reduced)

SLR Parser :
It is powerful than LR(0).
Ever LR(0) is SLR but every SLR need not be LR(0).
Conflicts in SLR
1. SR conflict : A -> B . xC (shifting) B -> a. (reduced) if FOLLOW(B) ∩ {x} ≠ ɸ
2. RR conflict :
A -> a. (reduced) B -> b. (reduced) if FOLLOW(A) ∩ FOLLOW(B) ≠ ɸ

CLR Parser : It is same as SLR parser except that the reduced entries in CLR parsing table go only in the FOLLOW of the l.h.s nonterminal.

LALR Parser : It is constructed from CLR parser, if two states having same productions but may contain different lookaheads, those two states of CLR are combined into single state in LALR.
Every LALR grammar is CLR but every CLR grammar need not be LALR.

Parsers Comparison :

LR(0) ⊂ SLR ⊂ LALR ⊂ CLR
LL(1) ⊂ LALR ⊂ CLR
If number of states LR(0) = n1, number of states SLR = n2, number of states LALR = n3, number of states CLR = n4 then, n1 = n2 = n3 <= n4

Image Source – Stackoverflow

Syntax Directed Translation : Syntax Directed Translation are augmented rules to the grammar that facilitate semantic analysis.
Eg – S -> AB {print (*)}
A -> a {print (1)}
B -> b {print (2)}
Synthesized Attribute : attribute whose value is evaluated in terms of attribute values of its children.
Inherited Attribute : attribute whose value is evaluated in terms of attribute values of siblings or parents.

Activation Record
: Information needed by a single execution of a procedure is managed using a contiguous block of storage called activation record. An activation record is allocated when a procedure is entered and it is deallocated when that procedure is exited.

Intermediate Code
: They are machine independent codes. Syntax trees, postfix notation, 3-address codes can be used to represent intermediate code.
Three address code :
1. Quadruples (4 fields : operator, operand1, operand2, result)
2. Triplets (3 fields : operator, operand1, operand2)
3. Indirect triples

Code Optimization : types of machine independent optimizations –
1. Loop optimizations :
Code motion : reduce the evaluation frequency of expression.
Loop unrolling : to execute less number of iterations
Loop jamming : combine body of two loops whenever they are sharing same index.
2. Constant folding : replacing the value of constants during compilation
3. Constant propagation : replacing the value of an expression during compile time.
4. Strength reduction : replacing costly operators by simple operators.

Company Wise Coding Practice    Topic Wise Coding Practice