Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sample Scenario Based Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

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

Assign Alphabets to each Non Terminals

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

ELEPHANT→ ELEPHANT + TIGER | ELEPHANT – TIGER | TIGER

TIGER→ TIGER * FOX | TIGER / FOX | FOX

FOX→ food| water | (ELEPHANT)


G → E,
E→E+T|E–T|T
T→T*F|T/F|F

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}

Precedence Table: (4 Marks)

+ - * / ( ) id num $

+ > > < < < > < < >

- > > < < < > < < >

* > > > > < > < < >


/ > > > > < > < < >

( < < < < < = < < e

) > > > > E > e e >

id > > > > E > e e >

num > > > > E > e e >

$ < < < < < e < < Acc

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,

1. Simonà Anitha adam/ Anitha belvin


2. AnithaàAnitha Darwin/ Simon gitesh/€
3. Determine Left Recursion and Left Factoring for the above relation. ( 10 Marks)

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’

The working production rule is :

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,$ }

7. Perform predictive parsing.

Stmt declare id optionList


optionList optionList option |ε
optionmode|scale|base
modereal|complex
scalefixed|floating
basebinary|decimal|octal

Ans:
Step 1: Elimination of Left recursion
optionList🡪 optionList option |ε
‘ A=optionList; ∝ = option ; β=ε
A→ βA’
A’ → ∝ A’ | ε
optionList→ optionList’
optionList’ → option optionList’ | ε

Stmt🡪 declare id optionList


optionList→ optionList’
optionList’ → option optionList’ | ε
option🡪mode|scale|base
mode🡪real|complex
scale🡪fixed|floating
Base🡪binary|decimal|octal

Step 2 :Left factor the grammar


No left factoring
Step 3:Computation of first and follow

Non terminal First() Follow

Stmt {declare} {$}

optionList {ε, real, complex, fixed, {$}


floating, binary,decimal,
octal}

optionList’ { ε, real, complex, fixed, {$,


floating, binary,decimal,
octal}

option {real, complex, fixed, { real, complex, fixed,


floating, binary,decimal, floating, binary,decimal,
octal} octal, $}

mode { real, complex,} { real, complex, fixed,


floating, binary,decimal,
octal, $}

scale {fixed, floating} { real, complex, fixed,


floating, binary,decimal,
octal, $}

base {binary,decimal, octal} { real, complex, fixed,


floating, binary,decimal,
octal, $}

Step 4:Predictive parsing table

declare id real com fixed float bina deci octal $


plex ing ry mal

Stmt Stmt🡪
declare
id
optionLi
st

optionList optio optio optio optio optio optio optio optio


nList nList nList nList nList nList nList nList
→ → → → → → → →
optio optio optio optio optio optio optio optio
nList nList nList nList nList nList nList nList
’ ’ ’ ’ ’ ’ ’ ’

optionList’ optio optio optio optio optio optio optio optio


nList nList nList nList nList nList nList nList
’→ ’→ ’→ ’→ ’→ ’→ ’→ ’→
optio optio optio optio optio optio optio ε
n n n n n n n
optio optio optio optio optio optio optio
nList nList nList nList nList nList nList
’ ’ ’ ’ ’ ’ ’

option optio optio optio optio optio optio optio


n🡪m n🡪m n🡪s n→s n🡪b n→b n🡪b
ode ode cale cale ase ase ase

mode mod mod


e→r e→c
eal ompl
ex

scale scale scale


→fix →flo
ed ating

base base base base


→bi →de →oc
nary cima tal
l

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
NPDT N| NP PP
PPPRP NP
VPV NP| VP PP
DT’a’| ‘the’
N’Lion’|’deer’| ‘tree’
PRP’under’ |’with’ |’above’
V’ate’|’saw’|’ran’
9.

Check whether the given grammar is ambiguous

Answer:
Two distinct parse trees can be constructed from the same sentential form.

10.
11. Create CFG for “while” and “for” loop.

Expr -> for( Expr; Expr; Expr) loop Expr pool

| while(Expr) loop Expr pool

You might also like