50% found this document useful (2 votes)
3K views4 pages

Assignment 2 Jatin Pawar

The document discusses a grammar and its LL(1) properties. It provides: 1) The first and follow functions for the grammar. 2) The LL(1) parsing table for the grammar. 3) Descriptions of predictive parsing and operator precedence parsing. 4) Checks if a given grammar is LL(1) and determines the given grammar is not LL(1) due to violations of the LL(1) criteria.

Uploaded by

Jatin Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
3K views4 pages

Assignment 2 Jatin Pawar

The document discusses a grammar and its LL(1) properties. It provides: 1) The first and follow functions for the grammar. 2) The LL(1) parsing table for the grammar. 3) Descriptions of predictive parsing and operator precedence parsing. 4) Checks if a given grammar is LL(1) and determines the given grammar is not LL(1) due to violations of the LL(1) criteria.

Uploaded by

Jatin Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment-2

Subject- Compiler Design

1. Consider the following grammar.


a) Calculate the first and follow functions for the given grammar-

S → aBDh
B → cC
C → bC / ∈
D → EF
E→g/∈
F→f/∈
Answer:

First Follow

S {a} {$}

B {c} {g,f,h}

C {b,∈} {g,f,h}

D {g,f,∈} {h}

E {g,∈} { f, h }

F {f,∈} {h}
Calculate “First”
Step 1. For ‘S’ : Given ​S → aBDh, We know for any terminal symbol ‘x’ first(X) = {x}
accordingly we will have first(​S​)​ = {a} ​For the start symbol S, place $ in Follow(S).
Step 2. For ‘B’: Similarly B has terminal symbol c.
Step3. For ‘C’: For this production rule first will be {​∈} ⋃ {f irst of C}
Step 4. For ‘D’ : Similarly we calculate first for D using the production rule X → Y​1​Y​2​Y​3​,
Calculating First(X)

● If ∈ ∉ First(Y​1​), then First(X) = First(Y​1​)


● If ∈ ∈ First(Y​1​), then First(X) = { First(Y​1​) – ∈ } ∪ First(Y​2​Y​3​)
​Calculating First(Y​2​Y​3​)
● If ∈ ∉ First(Y​2​), then First(Y​2​Y​3​) = First(Y​2​)
● If ∈ ∈ First(Y​2​), then First(Y​2​Y​3​) = { First(Y​2​) – ∈ } ∪ First(Y​3​)
so we use ​First(D) = { First(E) – ∈ } ∪ First(F)
Step 5. For ‘E’: First(E) = { g , ∈ } and Similarly for ‘F’ First(F) = { f , ∈ }
Calculate “Follow”:
Step 1. For the start symbol S, place $ in Follow(S).
Step 2. ​But for follow of B we follow
if the production rule A → αB, Follow(B) = Follow(A)
Given B here is S which has a different basis of production rule and that is
Note; If production rule A → αBβ​,
● If ∈ ∉ First(β), then Follow(B) = First(β)
● If ∈ ∈ First(β), then Follow(B) = { First(β) – ∈ } ∪ Follow(A)
Follow(B) = { First(D) – ∈ } ∪ First(h) = { g , f , h }
Step 3. For any production rule A → αB, Follow(B) = Follow(A)
from above we can say Follow(C) = { g , f , h }
Step 4. In step 2 we mentioned the Note: we are going to follow that.
Follow(D) = First(h) = { h }
Step 5. Follow(E) = { First(F) – ∈ } ∪ Follow(D) = { f , h }
Step 6. Follow(F) = Follow(D) = { h }
b. ​Draw the LL(1) parsing table for the grammar.
Answer:

a b c f g h `$

S s​→​aBDh

B B​→cC

C C ​→bC C ​→∈ C ​→∈ C ​→∈


D D​→EF D​→​EF D​→​EF

E E ​→∈ E​→g E ​→∈

F F ​→f F ​→∈
Method followed to construct the table:
1. Find the FIRST and FOLLOW of the given grammar.[ we have table 1. ]
2. Create a Table where Row fields are all Non-Terminals and Column fields are all terminals
including $.
3. Productions are added into Table as entries.
The way to fill entries is as follows:
For a production X->Y,
i. if Y is not ϵ, then add production X->Y into a row of X and column of FIRST(Y).
ii. If Y is ϵ, then add production X->Y into a row of X and column of FOLLOW(X)
1- Describe:

c. Predictive parsing
Answer: A type of top-down parsing approach, which is also a type of recursive descent parsing,
[means a set of mutually recursive procedures where each procedure implements one of the
non-terminal in the grammar] that does not involve any backtracking.
● Predictive parsing identifies what production to use to replace the input string.
● It does not have backtracking.
● The predictive parser uses a look ahead pointer.
● It points to the next input symbols.
● In order to make the parser free of backtracking, it uses some constraints on the grammar.
Therefore, it will only accept grammar called LL(k) grammar.

d. Operator precedence parsing


Answer: ​Operator precedence grammar is kinds of shift reduce parsing method. It is applied
to a small class of operator grammar.

A grammar is said to be operator precedence grammar if it has two properties:

○ No R.H.S. of any production has a∈.

○ No two non-terminals are adjacent.

Operator precedence can only established between the terminals of the grammar. It ignores
the non-terminal.

2. ​What is a LL(1) grammar. Check if the given grammar is LL(1).

E iAcE|iAcEeE|a
A b
Answer: A grammar is LL(1) if it is possible to choose the next production by looking at the next
token in the input string.

The first ‘L’ in LL (1) stands for scanning the input from left to right. The second ‘L’ stands for
producing a leftmost derivation. ‘1’ stands for using one input symbol of lookahead at each step
in making parsing action decisions.

Formally, Grammar G is LL(1) if and only if

a. for all productions A α1 | α2 | ............ | αn ,

first ( αi ) ⋂ first ( αj ) = Φ or Null where 1 ≤ i , j ≤ n ; i =/ j

b. For every nonterminal A such that first (A) contains ​∈; ​ first (A) ⋂ follow (A) = Φ

Given grammar is

E iAcE|iAcEeE|a
A b

We can inspect as per rule (a) mentioned above this given grammar violates that.

First ( α1 ) ⋂ first( α2 ) = i so this is not a LL(1) grammar.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy