Parsing in Compiler Design

4,916 views 38 slides Jul 16, 2021
Slide 1
Slide 1 of 38
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38

About This Presentation

This ppt gives all details about the parsing phase of compiler design.


Slide Content

Parsing Introduction

What is Parsing? In the syntax analysis phase, a compiler verifies whether or not the tokens generated by the lexical analyzer are grouped according to the syntactic rules of the language. This is done by a parser. It detects and reports any syntax errors and produces a parse tree from which intermediate code can be generated .

What is Parsing?

Context-Free Grammars Terminals - Basic symbols from which strings are formed. Non-terminals - Syntactic variables that denote sets of strings. Start Symbol - Denotes the nonterminal that generates strings of the languages Productions - A = X … X - Head/left side (A) is a nonterminal - Body/right side (X … X) zero or more terminals and non-terminals

Context-Free Grammars Non-terminals , terminals can be derived from productions. First production defines start symbol.

CFG Notation A, B, C: non-terminals l: terminals a, b, c: strings of non-terminals and terminals (alpha, beta, gamma in math) w, v: strings of terminal symbols

Derivations Derivation - derives in zero or more steps E =>* "-" "(" ID "+" ID ")" D erivation step: replace symbol by RHS of production. Repeatedly apply derivations

Derivations Left-most derivation: Expand left-most non-terminal at each step. Right-most derivation: Expand right-most non-terminal at each step

Types of Parser The process of deriving the string from the given grammar is known as derivation (parsing). Depending upon how derivation is done we have two kinds of parsers :- Top Down Parser Bottom Up Parser

Top Down Parser Top down parsing attempts to build the parse tree from root to leave. Top down parser will start from start symbol and proceeds to string. It follows leftmost derivation.

Bottom Up Parser Bottom-up parsing starts from the leaf nodes of a tree and works in upward direction till it reaches the root node . Here , we start from a sentence and then apply production rules in reverse manner in order to reach the start symbol.

Left Factoring

Left F actoring - Examples Consider the following grammar:- S → iEtS / iEtSeS / a E → b Solution:- S → iEtSS’ / a S’ → eS / ∈ E → b

Left F actoring - Examples Consider the following grammar:- A → aAB / aBc / aAc Solution:- A → aA ’ A’ → AB / Bc / Ac // This has to done again A → aA ’ A’ → AD / Bc D → B / c

Left Factoring - Examples Consider the following grammar:- S → aAd / aB A → a / ab B → ccd / ddc Solution :- S → aS’ S’ → Ad / B A → aA ’ A’ → b /  ∈

Eliminating Left Recursion

Left Recursion - Examples Consider the following grammar - A → ABd / Aa / a B → Be / b The grammar after eliminating left recursion is- A → aA ’ A’ → BdA ’ / aA ’ / ∈ B → bB ’ B’ → eB ’ / ∈

Left Recursion - Examples Consider the following grammar and eliminate left recursion- E → E + E / E x E / a The grammar after eliminating left recursion is- E → aA A → +EA / xEA / ∈

Left Recursion - Examples Consider the following grammar and eliminate left recursion- E → E + T / T T → T x F / F F → id E → E + T E → T T → T x F T → F F → id OR

Left Recursion - Examples The grammar after eliminating left recursion is- E → TE’ E’ → +TE’ / ∈ T → FT’ T’ → xFT ’ / ∈ F → id

First & Follow FIRST(X) for a grammar symbol X is the set of terminals that begin the strings derivable from X . Follow(X) to be the set of terminals that can appear immediately to the right of Non-Terminal X in some sentential form. Why calculate FIRST? It is useful to calculate FOLLOW. Both are useful in parsing (both Top-Down and Bottom-Up)

First & Follow Rules Rules to compute FIRST set:- If x is a terminal, then FIRST(x) = { ‘x’ } If x-> Є, is a production rule, then add Є to FIRST(x). If X->Y 1 Y 2 Y 3 …. Y n is a production, then FIRST(X) = FIRST(Y1) If FIRST(Y1) contains Є then FIRST(X) = { FIRST(Y1) – Є } U { FIRST(Y2) } If FIRST (Yi) contains Є for all i = 1 to n, then add Є to FIRST(X).

First - Examples Calculate the first functions for the given grammar-   S  → aBDh B → cC C → bC / ∈ D → EF E → g / ∈ F → f / ∈

First - Examples First(S) = { a } First(B) = { c } First(C) = { b , ∈ } First(D) = { First(E) – ∈ } ∪ First(F) = { g , f , ∈} First(E) = { g , ∈ } First(F) = { f , ∈ }

First - Examples Calculate the first functions for the given grammar- S  → A A → aB / Ad B → b C → g

First - Examples First(S) = First(A) = { a } First(A) = { a } First(A’) = { d , ∈ } First(B) = { b } First(C) = { g }

First & Follow Rules Rules to compute FOLLOW set :- Follow(S) = {$} where S is the start symbol. If A-> pBq is a production where p , B, q any grammar symbols then everything in FIRST(q) except Є is in FOLLOW(B ). If A-> pB is a production or a production A-> pBq where FIRST(q ) contains Є then everything in FOLLOW(A) is in FOLLOW(B ).

Follow - Examples Calculate the follow functions for the given grammar-   S  → aBDh B → cC C → bC / ∈ D → EF E → g / ∈ F → f / ∈

Follow - Examples 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 }

Follow - Examples Calculate the follow functions for the given grammar- S  → A A → aB / Ad B → b C → g

Follow - Examples Follow(S) = { $ } Follow(A) = Follow(S) = { $ } Follow(A’) = Follow(A) = { $ } Follow(B) = { First(A’) – ∈ } ∪ Follow(A) = { d , $ } Follow(C) = NA

First & Follow - Examples Calculate the first and follow functions for the given grammar-   S  → AaAb / BbBa A → ∈ B → ∈

First & Follow - Examples First(S) = { First(A) – ∈ } ∪ First(a) ∪ { First(B) – ∈ } ∪ First(b) = { a , b } First(A) = { ∈ } First(B) = { ∈ } Follow(S) = { $ } Follow(A) = First(a) ∪ First(b) = { a , b } Follow(B) = First(b) ∪ First(a) = { a , b }

First & Follow - Examples Calculate the first and follow functions for the given grammar-   E → E + T / T T → T x F / F F → (E) / id

First & Follow - Examples The given grammar is left recursive . After eliminating left recursion, we get - E → TE’ E’ → + TE’ / ∈ T → FT’ T’ → x FT’ / ∈ F → (E) / id

First & Follow - Examples First(E) = First(T) = First(F) = { ( , id } First(E’) = { + , ∈ } First(T) = First(F) = { ( , id } First(T’) = { x , ∈ } First(F) = { ( , id }

First & Follow - Examples 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 , + , $ , ) }