Sample Scenario Based Questions
Sample Scenario Based Questions
Sample Scenario Based Questions
1. Determine the Lead and Trail for the following regular expression Where APPLE, ORANGE
AND GRAPES are Non-Terminals and fruit is a terminal
APPLE → APPLE + ORANGE | APPLE – ORANGE | ORANGE
ORANGE → ORANGE * GRAPES | ORANGE / GRAPES | GRAPES
GRAPES → (APPLE) | fruit
E->E+T|E-T|T
T->T*F|T/F|F
F->(E)|id
2. Compute the operator precedence matrix, precedence relation and precedence function for the
following grammar and generate the stack implementation for the expression
water+water*water.
Where GIRAFFE, ELEPHANT, TIGER, FOX are Non-Terminals and food, water are terminals
GIRAFFE→ ELEPHANT
F → num | id | (E)
Head: (2 Marks)
G={+,-,*,/,(,id,num}
E={+,-,*,/,(,id,num}
T={*,/,(,id,num}
F={(,id,num}
Trail: (2 Marks)
G={+,-,*,/,),id,num}
E={+,-,*,/,),,id,num}
T={*,/,),id,num}
F={),id,num}
+ - * / ( ) id num $
3. Write a CFG for the following for Variable identifier declaration with the following
restriction
i. Only for int datatype ii. No need to consider white space iii. The identifier can start with a
letter (only small letters) or underscore(_) followed by any number of either digit or letter” iv.
Restrict the small letter alphabets to set of {a,b} and restrict the digit number to set of {1,2,3} for
convenience.
For the created grammar Construct Non-Recursive predictive parser table Parse the string
“int_a1”
4. Simon has 4 sons adam, belvin ,clement, and lived with his wife Anitha in the city of New
Jersey .When Simon reached 60 , he settled his properties equally to his 4 sons. During Summer
six months, Simon’s stays 3 months with his wife and adam , 3 months peerthi stays with belvin
and for winter season : Anitha stays with darwin for 3 months and during this period Simon’s
goes to world tour and for last 3 months Anitha Stays with Simon and clement.If their family
stay can be expressed in the productions rule, then the order becomes,
5. Sukesh travel to market in left side to purchase an apple and returned in opposite direction ,
the next day he travelled in opposite direction and purchased an apple and spend the wholeday in
the market .travelled to Japan for a month officially with his suitcase and returned back with his
same suitcase and have not carried anything in addition .The Productions is given as follows:
SukeshàappleSukeshapple’
Sukesh’àappleSukesh’|∈
Check whether the grammar is LL(1) or not by constructing predictive parsing table
Solution:
Assume:
Sukesh=S,apple=a,Sukesh’=S’
S->aSS'
S'->aS'| ∈
6. A well reputed university has allowed students to choose the courses in particular pattern.
It is represented as the given CFG where the term ‘Course_n’ denotes non terminals and
the subject name denotes terminals:
Course_1→Course_2Course_3| discrete_ mathematics |fundamentals_of_computing
Course_2→Course_3 digital_electronics | ε
Course_3→ data_structures
Please help the students to determine which will be the subjects, that can be enrolled in
the first instance.
Ans:
Compute First() for each course
First(Course_1)= {discrete_ mathematics, fundamentals_of_computing, data_structures,
ε}
First(Course_2)= {data_structures, ε}
First(Course_3)= {data_structures}
Follow()
Follow(Course_1)= {$}
Follow(Course_2)= {digital_electronics,$ }
Follow(Course_3)= {digital_electronics,$ }
Ans:
Step 1: Elimination of Left recursion
optionList🡪 optionList option |ε
‘ A=optionList; ∝ = option ; β=ε
A→ βA’
A’ → ∝ A’ | ε
optionList→ optionList’
optionList’ → option optionList’ | ε
Stmt Stmt🡪
declare
id
optionLi
st
8. Derive the sentence “ A lion saw the deer under the tree” from the following grammar
using top down and bottom-up parse trees.
S NP VP
NPDT N| NP PP
PPPRP NP
VPV NP| VP PP
DT’a’| ‘the’
N’Lion’|’deer’| ‘tree’
PRP’under’ |’with’ |’above’
V’ate’|’saw’|’ran’
9.
Answer:
Two distinct parse trees can be constructed from the same sentential form.
10.
11. Create CFG for “while” and “for” loop.