0% found this document useful (0 votes)
6 views15 pages

UNIT II Worksheets

The document is a worksheet for a course on Compiler Design at SRM Institute of Science and Technology, focusing on concepts such as Leftmost and Rightmost Derivation, Left Recursion Removal, and Predictive Parsing. It includes multiple-choice questions, fill-in-the-blanks, problem-solving activities, and exercises for students to practice their understanding of formal grammars and parsing techniques. Additionally, it features a bonus research question on how modern compilers handle ambiguous grammars.
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
0% found this document useful (0 votes)
6 views15 pages

UNIT II Worksheets

The document is a worksheet for a course on Compiler Design at SRM Institute of Science and Technology, focusing on concepts such as Leftmost and Rightmost Derivation, Left Recursion Removal, and Predictive Parsing. It includes multiple-choice questions, fill-in-the-blanks, problem-solving activities, and exercises for students to practice their understanding of formal grammars and parsing techniques. Additionally, it features a bonus research question on how modern compilers handle ambiguous grammars.
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/ 15

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY

SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTING TECHNOLOGIES
INNOVATIVE TEACHING PEDAGOGY ACTIVITIES
21CSC304J COMPILER DESIGN

Reg No :
KNOW YOUR TERMINOLOGIES Name :

Re- -ey Par-

String

Par-

&
SRM INSITITUTE OF SCIENCE AND TECHNOLOGY
SCHOOL OF COMPUTING
INNOVATIVE TEACHING PEDAGOGY ACTIVITIES
21CSC304J COMPILER DESIGN

REGISTER NO :

NAME :

WORKSHEET FOR LEFTMOST AND RIGHTMOST DERIVATION IN FORMAL


GRAMMARS

OBJECTIVE:

To understand and practice the concept of Leftmost and Rightmost Derivation in context-
free grammars. This worksheet is designed to help reinforce your ability to derive strings from a
given grammar using both leftmost and rightmost derivations.

ACTIVITY 1: MULTIPLE CHOICE QUESTIONS (MCQS)

1. What is the main difference between Leftmost and Rightmost Derivation?

a) Leftmost derivation always applies the leftmost non-terminal first, whereas Rightmost
applies the rightmost non-terminal first.
b) Leftmost derivation applies production rules from left to right, and Rightmost applies
from right to left.
c) Leftmost derivation involves a recursive process, while Rightmost is iterative.
d) There is no difference between the two.

2. Which of the following grammars would be used for Leftmost Derivation?

a) S → A B | C
b) S → A | B | C
c) S → A → B → C
d) S → A B → C → D
3. Given the production rule: A → aA | b, which string can be derived using Leftmost
Derivation?

a) "aaab"
b) "bbaa"
c) "bab"
d) "bbba"

4. Rightmost derivation of a string involves:

a) Expanding the leftmost non-terminal first


b) Expanding the rightmost non-terminal first
c) Expanding every terminal symbol first
d) Using only left recursion in the grammar

5. Which of the following strings can be derived using Rightmost Derivation from the
grammar S → aSb | ε?

a) "ab"
b) "aabb"
c) "abab"
d) "aaabbb"

ACTIVITY 2: FILL-IN-THE-BLANKS

6. In a Leftmost Derivation, at each step, the _____________________ non-terminal is


replaced by a production rule.
7. The process of constructing a string by applying production rules starting from the
rightmost non-terminal is called ______________________Derivation.
8. For the grammar S → A B | C, to derive the string "AB," the correct Leftmost Derivation
sequence is:
o S → ________ → ________ → AB
9. A key difference between Leftmost and Rightmost Derivation is the order of applying
production rules. Leftmost focuses on the ______________________ non-terminal, while
Rightmost focuses on the ______________________ non-terminal.
10. In a Rightmost Derivation, we apply production rules starting from the ________ non-
terminal.

ACTIVITY 3: PROBLEM-SOLVING ACTIVITY

11. Given the grammar:

S→AB|C
A→a
B→b
C→ε

Derive the string "ab" using Leftmost Derivation. Show all steps.

12. Given the grammar:

S → aSb | ε

Derive the string "aaabbb" using Rightmost Derivation. Show all steps.
13. Given the following grammar:

S→AB
A → aA | ε
B → bB | ε

Derive the string "aabbb" using both Leftmost and Rightmost Derivations. Show all steps.

14. Given the grammar:

S → aSb | ε

Derive the string "ab" using Leftmost Derivation and Rightmost Derivation. Show all steps
for each.
15. Consider the grammar:

S → aSb | ε

Derive the string "aaaabbbb" using a Leftmost Derivation. Show all steps.

ACTIVITY 4: TRUE / FALSE

16. True or False: Leftmost and Rightmost Derivations are two different methods of
constructing strings, but they always lead to the same derivation sequence for any string.
17. True or False: A Leftmost Derivation can always be reversed by a Rightmost
Derivation, and vice versa.
18. True or False: The grammar S → aSb | ε has multiple valid derivation sequences for the
string "ab" when using Leftmost and Rightmost Derivations.
19. True or False: Rightmost Derivation is typically more efficient than Leftmost Derivation
in parsing algorithms because it uses fewer production rules.
ACTIVITY 5: BONUS: RESEARCH QUESTION

20. Explore how modern compilers handle ambiguous grammars. What role do parsers
play in resolving ambiguity? Provide examples.
WORD SEARCH ACTIVITY
21CSC304J - COMPILER DESIGN – UNIT- II

1. What component of a compiler reads and interprets the source code to


build a structure that can be further processed?
2. These are rules that define the structure of valid statements in a
programming language. What are they called?
3. What do we call a problem encountered during compilation, which
often halts the process?
4. What type of grammar allows for rules where the left-hand side consists
of a single non-terminal symbol?
5. What term describes a situation where a grammar can generate
multiple parse trees for the same input string?
6. In parsing, what type of rule leads to a non-terminal referring to itself,
often causing infinite loops if not handled properly?
7. What technique is used to rewrite a grammar to avoid ambiguity by
factoring out common prefixes?
8. What type of parsing strategy starts from the root of the parse tree and
works its way down to the leaves?
9. What type of parsing uses lookahead tokens to predict which rule to
apply next in a parse?
10. In parsing, what set helps determine which rule to apply based on the
first terminal symbol that can be derived from a non-terminal?
11. What set in predictive parsing helps decide which rule to apply by
determining what terminal symbols can follow a non-terminal?
12. What structure is used in predictive parsing to map the combination of
non-terminals and lookahead tokens to specific production rules?
13. What do we call a step-by-step procedure for solving problems, such as
those used in predictive parsing?

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, DEPARTMENT OF COMPUTING TECHNOLOGIES, SCHOOL OF COMPUTING, KATTANKULATHUR CAMPUS, CHENNAI
SRM INSITITUTE OF SCIENCE AND TECHNOLOGY
SCHOOL OF COMPUTING
INNOVATIVE TEACHING PEDAGOGY ACTIVITIES
21CSC304J COMPILER DESIGN

REGISTER NO :

NAME :

WORKSHEET FOR LEFT RECURSION AND LEFT FACTORING

PART -1 LEFT RECURSION REMOVAL

EXAMPLE 1:

Given the following grammar, remove the left recursion and rewrite the grammar
in an equivalent form that is suitable for LL(1) parsing.

A → Aα | β
1. Identify the left recursion in the grammar.

2. Remove the left recursion by introducing a new non-terminal.

3. Write the new set of production rules without left recursion.


EXAMPLE 2:

Given the following grammar, remove the left recursion and rewrite the grammar
in an equivalent form that is suitable for LL(1) parsing.

1. A → A b | c
Identify the left recursion in the grammar.

2. Remove the left recursion by introducing a new non-terminal.

3. Write the new set of production rules without left recursion.


PART -2 LEFT RECURSION REMOVAL

EXAMPLE 1:

Given the following grammar, apply left factoring to make it suitable for LL(1)
parsing.
S→aA|aB|bA
1. Identify the common prefixes in the productions of S.

2. Apply left factoring to factor out the common prefix.

3. Write the new set of production rules after left factoring.


PART -3 COMBINED PROBLEM (LEFT RECURSION + LEFT FACTORING)

EXAMPLE 1:

Given the following grammar, perform both left recursion removal and left
factoring:
S→AB|AC
A→a|Aα
B→b|c
C→d|c
1. First, remove the left recursion from the grammar (if applicable).

2. Apply left factoring to the grammar to make it suitable for LL(1).

3. Write the final set of production rules.


SRM INSITITUTE OF SCIENCE AND TECHNOLOGY
SCHOOL OF COMPUTING
INNOVATIVE TEACHING PEDAGOGY ACTIVITIES
21CSC304J COMPILER DESIGN

REGISTER NO :

NAME :

WORKSHEET FOR PREDICTIVE PARSING

OBJECTIVE:

To help students practice the process of constructing LL(1) parse tables and using them for
predictive parsing.

1.Part 1: Construct the LL(1) Parse Table

Consider the following grammar:


S→AB|CD
A→a
B→b|ε
C→c
D→d

1. Construct the First sets for the non-terminals.


2. Construct the FOLLOW sets for the non-terminals.

3. Build the LL(1) parse table based on the given grammar


Part 2: Predictive Parsing Process

1. Input string: ab

2. Input string: cd

You might also like