Beruflich Dokumente
Kultur Dokumente
First and Follow sets are needed so that the parser can properly apply the needed production rule at
the correct position.
First Function-
First(α) is a set of terminal symbols that begin in strings derived from α.
Example-
Consider a production rule- A → abc / def / ghi
Then, First(A) = { a , d , g }
Rule-02:
For any terminal symbol ‘a’ First(a) = { a }
Rule-03:
For a production rule X → Y1Y2Y3
Calculating First(X)
If ∈ ∉ First(Y1), then First(X) = First(Y1)
If ∈ ∈ First(Y1), then First(X) = { First(Y1) – ∈ } ∪ First(Y2Y3)
Calculating First(Y2Y3)
If ∈ ∉ First(Y2), then First(Y2Y3) = First(Y2)
If ∈ ∈ First(Y2), then First(Y2Y3) = { First(Y2) – ∈ } ∪ First(Y3)
Similarly, we can expand the rule for any production rule X → Y1Y2Y3…..Yn
Follow Function-
Follow(α) is a set of terminal symbols that appear immediately to the right of α.
Rule-02:
For any production rule A → αB Follow(B) = Follow(A)
Rule-03:
For any production rule A → αBβ
If ∈ ∉ First(β), then Follow(B) = First(β)
If ∈ ∈ First(β), then Follow(B) = { First(β) – ∈ } ∪ Follow(A)
Important Notes-
Note-01:
∈ may appear in the first function of a non-terminal.
∈ will never appear in the follow function of a non-terminal.
Note-02:
It is recommended to Eliminate Left Recursion from the grammar if present before calculating the
first and follow functions.
Note-03:
We will calculate the follow function of a non-terminal by looking where it is present on the RHS of a
production rule.
Solution-
The first and follow functions are-
First Functions-
First(S) = { a }
First(B) = { c }
First(C) = { b , ∈ }
First(D) = { First(E) – ∈ } ∪ First(F) = { g , f , ∈ }
First(E) = { g , ∈ }
First(F) = { f , ∈ }
Follow Functions-
Follow(S) = { $ }
Follow(B) = { First(D) – ∈ } ∪ First(h) = { g , f , h }
Follow(C) = Follow(B) = { g , f , h }
Follow(D) = First(h) = { h }
Follow(E) = { First(F) – ∈ } ∪ Follow(D) = { f , h }
Follow(F) = Follow(D) = { h }
Problem-02
Calculate the first and follow functions for the given grammar-
S→A
A → aB / Ad
B→b
C→g
Solution-
Since the given grammar is left recursive, so we first remove left recursion from the given grammar.
After eliminating left recursion, we get the following grammar-
S→A
A → aBA’
A’ → dA’ / ∈
B→b
C→g
Now, let us calculate the first and follow functions-
First Functions-
First(S) = First(A) = { a }
First(A) = { a }
First(A’) = { d , ∈ }
First(B) = { b }
First(C) = { g }
Follow Functions-
Follow(S) = { $ }
Follow(A) = Follow(S) = { $ }
Follow(A’) = Follow(A) = { $ }
Follow(B) = { First(A’) – ∈ } ∪ Follow(A) = { d , $ }
Follow(C) = NA
Problem-03:
Calculate the first and follow functions for the given grammar-
S → (L) / a
L → SL’
L’ → ,SL’ / ∈
Solution-
The first and follow functions are-
First Functions-
First(S) = { ( , a }
First(L) = First(S) = { ( , a }
First(L’) = { , , ∈ }
Follow Functions-
Follow(S) = { $ } ∪ { First(L’) – ∈ } ∪ Follow(L) ∪ Follow(L’) = { $ , , , ) }
Follow(L) = { ) }
Follow(L’) = Follow(L) = { ) }
Problem-04:
Calculate the first and follow functions for the given grammar-
S → AaAb / BbBa
A→∈
B→∈
Solution-
The first and follow functions are-
First Functions-
First(S) = { First(A) – ∈ } ∪ First(a) ∪ { First(B) – ∈ } ∪ First(b) = { a , b }
First(A) = { ∈ }
First(B) = { ∈ }
Follow Functions-
Follow(S) = { $ }
Follow(A) = First(a) ∪ First(b) = { a , b }
Follow(B) = First(b) ∪ First(a) = { a , b }
Problem-
Calculate the first and follow functions for the given grammar-
E→E+T/T
T→TxF/F
F → (E) / id
Solution-
Since the given grammar is left recursive, so we first remove left recursion from the given grammar.
After eliminating left recursion, we get the following grammar-
E → TE’
E’ → + TE’ / ∈
T → FT’
T’ → x FT’ / ∈
F → (E) / id
Now, let us calculate the first and follow functions-
First Functions-
Follow Functions-
Follow(E) = { $ , ) }
Follow(E’) = Follow(E) = { $ , ) }
Follow(T) = { First(E’) – ∈ } ∪ Follow(E) ∪ Follow(E’) = { + , $ , ) }
Follow(T’) = Follow(T) = { + , $ , ) }
Follow(F) = { First(T’) – ∈ } ∪ Follow(T) ∪ Follow(T’) = { x , + , $ , ) }
Problem-06:
Calculate the first and follow functions for the given grammar-
S → ACB / CbB / Ba
A → da / BC
B→g/∈
C→h/∈
Solution-
The first and follow functions are-
First Functions-
First(S) = { First(A) – ∈ } ∪ { First(C) – ∈ } ∪ First(B) ∪ First(b) ∪ { First(B) – ∈ } ∪ First(a) = { d , g , h
,∈,b,a}
First(A) = First(d) ∪{ First(B) – ∈ } ∪ First(C) = { d , g , h , ∈ }
First(B) = { g , ∈ }
First(C) = { h , ∈ }
Follow Functions-
Follow(S) = { $ }
Follow(A) = { First(C) – ∈ } ∪ { First(B) – ∈ } ∪ Follow(S) = { h , g , $ }
Follow(B) = Follow(S) ∪ First(a) ∪{ First(C) – ∈ } ∪ Follow(A) = { $ , a , h , g }
Follow(C) = { First(B) – ∈ } ∪ Follow(S) ∪ First(b) ∪ Follow(A) = { g , $ , b , h }
o Sift reduce parsing performs the two actions: shift and reduce. That's why it is known as shift
reduces parsing.
o At the shift action, the current symbol in the input string is pushed to a stack.
o At each reduction, the symbols will replaced by the non-terminals. The symbol is the right side
of the production and non-terminal is the left side of the production.
Example:
Grammar:
1. S → S+S
2. S → S-S
3. S → (S)
4. S → a
Input string:
1. a1-(a2+a3)
Parsing table:
Operator precedence can only established between the terminals of the grammar. It ignores the non-
terminal.
a ⋖ b means that terminal "a" has the lower precedence than terminal "b".
a ≐ b means that the terminal "a" and "b" both have same precedence.
Precedence table:
Parsing Action
o Both end of the given input string, add the $ symbol.
o Now scan the input string from left right until the ⋗ is encountered.
o Scan towards left over all the equal precedence until the first left most ⋖ is encountered.
o Everything between left most ⋖ and right most ⋗ is a handle.
o $ on $ means parsing is successful.
Example
Grammar:
1. E → E+T/T
2. T → T*F/F
3. F → id
Given string:
1. w = id + id * id
On the basis of above tree, we can design following operator precedence table:
Now let us process the string with the help of the above precedence table:
LR Parser
LR parsing is one type of bottom up parsing. It is used to parse the large class of grammars.
In the LR parsing, "L" stands for left-to-right scanning of the input.
"K" is the number of input symbols of the look ahead used to make number of parsing decision.
LR parsing is divided into four parts: LR (0) parsing, SLR parsing, CLR parsing and LALR parsing.
LR algorithm:
The LR algorithm requires stack, input, output and parsing table. In all type of LR parsing, input,
output and stack are same but parsing table is different.
Input buffer is used to indicate end of input and it contains the string to be parsed followed by a $
Symbol.
A stack is used to contain a sequence of grammar symbols with a $ at the bottom of the stack.
Parsing table is a two dimensional array. It contains two parts: Action part and Go To part.
LR (1) Parsing
Various steps involved in the LR (1) Parsing:
Augment Grammar
Augmented grammar G` will be generated if we add one more production in the given grammar G. It
helps the parser to identify when to stop the parsing and announce the acceptance of the input.
Example
Given grammar
1. S → AA
2. A → aA | b
1. S`→ S
2. S → AA
3. A → aA | b
A compiler can broadly be divided into two phases based on the way they compile.
(draw syntax tree for expr c=a+b)
Optimization is a program transformation technique, which tries to improve the code by making it
consume less resources (i.e. CPU, Memory) and deliver high speed.
In optimization, high-level general programming constructs are replaced by very efficient low-level
programming codes. A code optimizing process must follow the three rules given below:
The output code must not, in any way, change the meaning of the program.
Optimization should increase the speed of the program and if possible, the program should
demand less number of resources.
Optimization should itself be fast and should not delay the overall compiling process.
Code generation can be considered as the final phase of compilation. Through post code
generation, optimization process can be applied on the code, but that can be seen as a part of
code generation phase itself. The code generated by the compiler is an object code of some
lower-level programming language, for example, assembly language. We have seen that the
source code written in a higher-level language is transformed into a lower-level language that
results in a lower-level object code, which should have the following minimum properties: