0% found this document useful (0 votes)
2 views

Assignment_4_Compiler Design -Solution

The document is an assignment for a Compiler Design course from IIT Kharagpur, consisting of 11 multiple-choice questions related to formal language theory and grammar. It covers topics such as ambiguity in grammars, left recursion, operator precedence, and parsing techniques. Each question includes answers and explanations to reinforce understanding of the concepts presented.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment_4_Compiler Design -Solution

The document is an assignment for a Compiler Design course from IIT Kharagpur, consisting of 11 multiple-choice questions related to formal language theory and grammar. It covers topics such as ambiguity in grammars, left recursion, operator precedence, and parsing techniques. Each question includes answers and explanations to reinforce understanding of the concepts presented.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

NPTEL Online Certification Courses Indian

Institute of Technology Kharagpur

Compiler Design
Assignment- Week 4
TYPE OF QUESTION:MCQ
Number ofquestions:11 Total mark: 11 X 1 = 11

1.
Ans: a)

• Solution: In formal language theory and automata, a language consists of words


(strings) that are formed from a set of terminals.
• Terminals are the basic symbols from which strings (words) of a language are
composed.
• Non-terminals are used in the production rules of a grammar but do not appear
in the final words of the language.

Thus, the words of a language are made up only of terminals.

2.
Ans: a)
Solution:

 A grammar is ambiguous if a single string (sentence) derived from the grammar has
more than one distinct parse tree or derivation.

 The given grammar:

E→E+E ∣ E∗E ∣ id

allows multiple ways to derive the same expression. For example, for the string id + id *
i, we can have two different parse trees:

3.
• These two distinct parse trees show that the grammar does not enforce a unique
structure for the expression, making it ambiguous.

How to remove ambiguity?

• Introduce operator precedence and associativity explicitly (e.g., * has higher


precedence than +).
• Use different non-terminals for different levels of precedence.

Thus, the given grammar is ambiguous.

Ans: a)
Explanation: Deterministic CFGs are always unambiguous , and are an important
subclass of unambiguous CFGs; there are non-deterministic unambiguous CFGs,
however.
4. A language that admits only ambiguous grammar:

a) Inherent Ambiguous language


b) Inherent Unambiguous language
c) Context free language
d) Context Sensitive language
Answer: a
Explanation: A language is called inherently ambiguous if every possible grammar that
generates it is ambiguous.

• This means that no unambiguous grammar can be written for such a language.
• Some context-free languages (CFLs) are inherently ambiguous, meaning that
ambiguity cannot be removed by rewriting the grammar.

5.
Ans: b)

Solution:
6. For the grammar rules {S  Aa | bB, A  c |ε}, FIRST(S) is

Ans: c)
Solution:

Explanation:

To determine FIRST(S) for the given grammar:

Given grammar rules:

1. S → Aa | bB
2. A → c | e (where e represents ε, i.e., an empty string)

Step 1: Calculate FIRST(A)

• A → c ⇒ FIRST(A) includes {c}


• A → e (ε) ⇒ A can derive ε
Thus, FIRST(A) = {c, ε}

Step 2: Calculate FIRST(S)

• S → Aa
o A can be c or ε
o If A → c, then Aa starts with c
o If A → ε, then Aa starts with a
o Thus, from this production, FIRST(S) includes {c, a}
• S → bB
o The first symbol is b, so FIRST(S) includes {b}

Final FIRST(S) set:


FIRST(S)=a,b,c

7. The grammar {E E + T | T, T T * F | F, F  id} is

Ans: b)
Solution:

A grammar is unambiguous if every string in the language has a unique parse tree (or
derivation).

Given Grammar:

1. E → E + T | T
2. T → T * F | F
3. F → id

This is a standard unambiguous expression grammar because:

• Operator precedence is correctly enforced:


o Multiplication (*) has higher precedence than addition (+).
• Associativity is properly defined:
o Both + and * are left-associative.

Step 1: Checking for Ambiguity

Consider the input "id + id * id".

Using the given grammar, the only valid parse tree follows standard precedence rules:

r
CopyEdit
E
/ \
E + T
| / \
T T * F
| | |
F F id
| |
id id

• The multiplication (*) happens first, followed by addition (+), which is correct
operator precedence.
• There is only one unique parse tree, meaning the grammar is unambiguous.

Step 2: Why is This Grammar Unambiguous?

• The grammar explicitly enforces operator precedence (* before +).


• The grammar ensures left-associativity.
• Every valid string has only one possible parse tree, proving that the grammar
is unambiguous.

8.
Ans: a)
Solution:

A top-down parser constructs the parse tree from the start symbol (root) to the leaves,
applying production rules from left to right.

• In a leftmost derivation, at each step, the leftmost non-terminal is expanded first.


• This ensures that the derivation follows the leftmost rule application, making it
compatible with top-down parsing techniques such as Recursive Descent Parsing
and LL(1) Parsing.

Example:

Given the grammar:

1. S → A B
2. A → a
3. B → b

For the input "ab", a leftmost derivation (LMD) would be:

1. S ⇒ A B (Expand S)
2. A B ⇒ a B (Expand A first, leftmost non-terminal)
3. a B ⇒ a b (Expand B)

Here, the leftmost non-terminal is always expanded first, confirming leftmost derivation.

9.
Ans: a)

Solution:

Left recursion removal is mandatory for top-down parsing because top-down parsers
(like Recursive Descent and LL(1) parsers) cannot handle left-recursive grammars.

What is Left Recursion?

A grammar has left recursion if a non-terminal refers to itself as the leftmost symbol in
its production.
Example of left recursion:

A→Aα∣βA \rightarrow Aα | βA→Aα∣β

Here, the non-terminal A appears at the beginning of its own production (A → Aα),
causing an infinite recursion in a top-down parser.

Why is Left Recursion a Problem?

• Top-down parsers expand the leftmost non-terminal first.


• If the grammar contains left recursion, the parser keeps calling the same rule
indefinitely, leading to infinite recursion.

Solution: Removing Left Recursion

The left-recursive rule:

A→Aα∣

Can be converted to an equivalent right-recursive form:

A→βA
A′→αA′∣ε

This transformation eliminates left recursion, making the grammar suitable for top-
down parsing.

10
.

Ans: b)
Solution:

A grammar is ambiguous if there exists at least one string that has more than one
distinct parse tree or derivation.

Key Concept of Ambiguity:

• Ambiguity arises when a string can be derived in multiple ways, leading to


different parse trees.
• This can happen when there is more than one leftmost derivation or more than
one rightmost derivation.

11
.
Ans: a)
Explanation: Backtracking problem is solved by constructing a tree of choices called as
the state-space tree. Its root represents an initial state before the search for a solution
begins.
END of Assignment

You might also like