UNIT II Worksheets
UNIT II Worksheets
SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTING TECHNOLOGIES
INNOVATIVE TEACHING PEDAGOGY ACTIVITIES
21CSC304J COMPILER DESIGN
Reg No :
KNOW YOUR TERMINOLOGIES Name :
String
Par-
&
SRM INSITITUTE OF SCIENCE AND TECHNOLOGY
SCHOOL OF COMPUTING
INNOVATIVE TEACHING PEDAGOGY ACTIVITIES
21CSC304J COMPILER DESIGN
REGISTER NO :
NAME :
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.
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.
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"
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
S→AB|C
A→a
B→b
C→ε
Derive the string "ab" using Leftmost Derivation. Show all steps.
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.
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.
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
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 :
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.
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.
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.
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).
REGISTER NO :
NAME :
OBJECTIVE:
To help students practice the process of constructing LL(1) parse tables and using them for
predictive parsing.
1. Input string: ab
2. Input string: cd