Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CPSC 323 – Final Spring 2021 – 300 points – 8 Questions

Section ______ Name Jacob Hajjar

NUMBER AND ANSWER ALL THE QUESTIONS (30-40 points each)


SHOW YOUR WORK AND ADD MORE PAGES AS NEEDED
Technically CLOSED BOOK – NO COPY & PASTE

1. Given the following NFSM, convert it into a DFSM using the subset method (40)
1 2 ε
A {} {C} {D}
B {A,E} {} {C,D}
C {} {E} {B}
D {E} {} {}
E {} {} {D} with q0 =A and F = {D}

ε-Closure(A)=(A, D)
ε-Closure(B)=(B, C, D)
ε-Closure(C) = (C,B)
ε-Closure(D)=(D)
ε-Closure(E)=(E, D)

1 2
A {} {C, B}
C,B {A, E, D} {E, D}
E, D {E, D} {}
A, E, D {E, D} {C, B}

2. Use Thompson’s construction method to convert the following RE= a+ ab | bc* into a
NFA diagram (30 points)
3. Find the First and Follow sets for each non-terminal symbol. (40)

D −> ONE
O −> a | b | ε
N −> Ec | d | ε
E −> f | gDg | ε

First(O)={a, b, ε}
First(N)={f, g, c, d, ε}
First(E)={f, g, ε}
First(D)={a, b, f, g, c, d, ε}
Follow(O)= {f, g, c ,d, $}
Follow(N)={f, g, $}
Follow(E)={$}
Follow(D)={$}
4. Given the following productions, construct the parsing table for table driven predictive
parser - it is a top-down parser and remove left recursion if needed. (40)

1) S −> S * C 2) S −> S / C 3) S −> S @ C 4) S −> C 5) C −> x

S-> CS’
S’-> *CS’ | /CS’ | @CS’ | ε
C->x

{0, *, /, @, x, $},
{S, 0, 0, 0, CS’, 0},
{S’, *CS’, /CS’, @CS’, 0, ε},
{C, 0, 0, 0, x, 0};

5. Given the following predictive parsing table, parse the string “ (aba) ” to show if it is
accepted. (30)

a b ( ) $

S CB CB
B bCB ε ε
C a (S)

Show the following: Stack input Production/Action


$S aba$ pop(S), push(B, C)
$BC aba$ pop(C), push(a)
$Ba aba$ pop(a), lexer()
$B ba$ pop(B), push(B.C.b)
$BCb ba$ pop(b), lexer
$BC a$ pop(C), push(a)
$Ba a$ pop(a), lexer()
$B $ pop(B), push(ε)
$ $ stack empty
6. Use the following Operator Precedence table and grammar to parse
the string “ a * b + c * d “ to see if it is accepted (40 points).

Productions
1) E −> E + E
2) E −> E * E
3) E −> id
Use the following : Stack Compare Input Production Used
$ <. a * b + c * d$
$<.a >. * b + c * d$ E −> id
$E <. * b + c * d$
$<.E* <. b + c * d$
$<.E*<.b .> + c * d$ E->id
$<.E*E <. + c * d$
$<.E*<.E+ <. c * d$
$<.E*<.E+c >. * d$ E->id
$<.E*<.E+E <. *d$
$<.E*<.E+<.E* <. d$
$<.E*<.E+<.E*d >. $ E->id
$<.E*<.E+<.E*E .> $ E->E * E
$<.E*<.E+E .> $ E −> E + E
$<.E*E .> $ E −> E * E
$E $ Finished

7. Using the following LR table, parse the string “ xyx = x “ (40 points).
State x = y $ S E Productions

0 S3 1 2 1) S −> E = E
1 ACCT 2) S −> x
2 S4 S5 3) E −> E y x
3 R4 R4 R2 4) E −> x
4 S7 6
5 S8
6 S5 R1
7 R4 R4 R4
8 R3 R3 R3
Use the following : Stack input Action Production
$0 xyx=x$ S3 push(x);push(3)
$0x3 yx=x$ R4 E->x;Table[0, E]=2
$0E2 yx=x$ S5 push(y);push(5)
$0E2y5 x=x$ S8 push(x);push(8)
$0E2y5x8 =x$ R3 E −> E y x; Table[0, E] = 2
$0E2 =x$ S4 push(=);push(4)
$0E2=4 x$ S7 push(x);push7
$0E2=4x7 $ R4 E->x; Table[4, E]=6
$0E2=4E6 $ R1 S −> E = E; Table[0, S]=1
$0E1 $ ACCT

8. Given the following production rules, write a RDP function that returns a boolean value
for the DO WHILE production #9. Write the function using C, C++, C#, Java, python (40
points) or pseudo code (30 points). Use any of the pre-existing functions Condition(),
Statement(), Expression(), lexer(), getNextToken(), currentToken(), getNextChar(),
currentChar(), first(), follow(), backup(), error() or match() if needed. (40 points total)

1) <Expression> −> identifier


2) <Expression> −> <Condition>
3) <Expression> −> <Assign-Expression>

4) <Condition> −> <Expression> <RelationalOperator><Expression>

5) <Statement> −> <Select-Statement>


6) <Statement> −> <Loop-Statement>
7) <Statement> −> <Expression>

8) <Loop-Statement> −> while( <Condition> ) { <Statement> }


9) <Loop-Statement> −> do { <Statement> } while( <Condition> )

You might also like