model answer of compilers june spring 2013

5

Click here to load reader

Upload: arab-open-university-and-cairo-university

Post on 29-Jun-2015

175 views

Category:

Education


1 download

DESCRIPTION

Model answer of compilers June spring 2013, faculty of computers and information Cairo University

TRANSCRIPT

Page 1: Model answer of compilers june spring 2013

Page 1 of 5

Cairo University Faculty of Computers and Information

Final Exam

Department: CS Course Name: Compilers Date: Tuesday 4-June-2013 Course Code: CS419 Duration: 2 hours Instructor(s): Dr. Hussien Sharaf Total Marks: 60 Question 1 [12 marks] For each of the following languages do the following:

i. Understand the languages without any assistance; ii. Write sample words for the language where you show the smallest possible word.

iii. Construct a regular expression that represents the language. Note: Draw and fill the following table in your answer sheet:

Sample Regular Expression

a. Σ ={a, b} Only words with exactly two consecutive a's [2 marks] Solution: Sample = {aa, aab, baa, baab, bbaabbb …..} b*aab*

b. Σ ={a, b} Only words such that { an bm | n is even and m is odd} where n and m indicate number of “a”s and “b”s respectively. [3 marks]

Solution: Sample = {b,aab,aabbb,..} (aa)* b(bb)* + (aa)* a (bb)* b

c. Σ ={a, b} Words where “a” appears ONLY in even positions or doesn’t appear at all. [3 marks]

Solution: Sample = { Λ b ba bbb bbba baba …..} b+(bb+ba)*b* (b(a+b))* + b* (ba)*(bb)*(ba)* b* + (b(bb)* (a+b) )*

d. Σ ={a, b} Construct a regular expression for all strings that have exactly one double letter in

them. “One double letter” implies two equal touching letters; triples or more are excluded. [4 marks] Solution: Sample = { aa, baa, …..} (b + Λ)(ab)*aa(ba)*(b + Λ) + (a + Λ)(ba)*bb(ab)*(a + Λ)

Question 2 [8 marks] For each of the following languages do the following:

i. Understand the languages without any assistance; ii. Write sample words for the language where you show the smallest possible word. iv. Draw a deterministic finite automaton that represents the language and handles incorrect

inputs.

Page 2: Model answer of compilers june spring 2013

Page 2 of 5

a. Σ = {a, b} words where “a” appears ONLY in the second position or doesn’t appear at all. [3 marks] Solution: Sample = { Λ b ba bbb bab babbb …..}

b. Σ = {a, b} Words that begin and ending with the same letter. [5 marks] Solution: Sample = { Λ b ba bbb bab babbb …..}

Question 3 [10 marks] An if-statement indicated by L is a language where multiple executable instructions “S” can be used only by embedding them inside curly brackets pair where execution starts with the inner curly brackets pair; i.e. {{{S1}S2}S3} means executing s1, s2 then s3. One statement can only be embedded inside one curly bracket pair where the statement “S” should always stick to the closing curly bracket. Finally a curly bracket pair must always contain a statement i.e. { {S} } is NOT valid. Sample for if-statement in L: if ((c4) OR (c5 AND c6)) {{{s1}s2}s3} else {{s5}s6} a. Understand the language without any assistance and check if the following grammar represents

the conditional statements “C” indicated by L. If NO then explain why? If YES then show left derivation for “((C4) OR (C5 AND C6))”; Do each step in a separate line and show rule number. [3 marks]

R1: C→(C) R2: C→C AND C R3: C→C OR C R4: C→c1|c2|c3|c4|c5|c6| …|c10 Note: Draw and fill the following table in your answer sheet:

a

b

b -+1

2 a,b

+3

a

a -+1

b

4

a

b

3

a

+2

b

b

a

a

5

b

+4

Page 3: Model answer of compilers june spring 2013

Page 3 of 5

Derivation Rule Number

Solution: YES

Derivation Rule Number C →(C) R1 →(C OR C) R3 →((C) OR C) R1 →((c4) OR C) R4 →((c4) OR (C)) R1 →((c4) OR (C AND C)) R2 →((c4) OR (c5 AND C)) R4 →((c4) OR (c5 AND c6)) R4

b. Write a CFG for the instructions “S” similar to the CFG of the “C”. Can you avoid using

Lambda? [4 marks] Solution: R1: S→{ST} R2: S→Λ R3: T→s1|s2|s3|s4|s5|s6| …|s10 Λ must be used to recursion of S

c. Can language L be described using regular expressions? Why? [3 marks]

Solution: • No. • Because L have nested structures brackets and parenthesis that must be balanced. RE cannot

describe balanced structures such as anbn Question 4 [30 marks] Ζ delta is a language that allows for nested if-statements L, executable expressions “E” and conditional statements “C”. CFG: R1: Z →E R2: Z →L R3: L →if C Z R4: L →if C Z else Z R5: C→(C) R6: C→N and N R7: E →id = N * N R10: N→ 0 R11: N→ 1 Sample for a Z statement: if (0 and 1) if (0 and 0) id = 1*1

a. Specify which pair of rule(s) contains the left factoring and rewrite all the CFG with numbers

(after removing left factoring) factoring? Hint: if you need to introduce a new variable then use letter M. [3 marks]

Solution: R3 and R4 CFG: • R1: Z →E • R2: Z →L

Page 4: Model answer of compilers june spring 2013

Page 4 of 5

• R3: L→ if C Z M • R4:M→Λ • R5:M→ else Z • R6: C→ (C) • R7: C→N and N • R8: E →id = N * N • R9: N→ 0 • R10: N→ 1

b. Construct a parsing table for Z. Note that the set of terminals is {if, else, (,),id=,*, and, 0,1} and

set of non-terminals is {Z, E, L, M, C, N} [3 marks] Solution:

If else ( ) and id = * 0 1 $ Z R2 R1 E R8 L R3 M R5 R4 R4 R4 C R6 R7 R7 N R9 R10

c. Show the top-down parsing steps of the sample above using the parsing table constructed in the

previous section. [16 marks] Solution:

Stack Input Parser action Z if (0 and 1) if (0 and 0) id = 1*1

$ R2

L .. R3 if C Z M if .. Match C Z M (0 and 1) .. R6 (C) Z M (0 and 1) .. Match C) Z M 0 and 1) .. R7 N and N) Z M 0 and 1) .. R9 0 and N) Z M 0 and 1) .. Match N) Z M 1) … R9 1) Z M 1) if (0 and 0) id = 1*1 $ Match Z M if (0 and 0) id = 1*1 $ R2 L M if (0 and 0) id = 1*1 $ R3 if C Z M M if (0 and 0) id = 1*1 $ Match C Z M M (0 and 0) id = 1*1 $ R6 (C) Z M M (0 and 0) id = 1*1 $ Match C) Z M M 0 and 0) id = 1*1 $ R7 N and N) Z M M 0 and 0) id = 1*1 $ R9 0 and N) Z M M 0 and 0) id = 1*1 $ Match N) Z M M 0) id = 1*1 $ R9 0) Z M M 0) id = 1*1 $ Match Z M M id = 1*1 $ R1 E M M id = 1*1 $ R8 id = N * N M M id = 1*1 $ Match N * N M M 1*1 $ R10 1 * N M M 1*1 $ Match N M M 1 $ R10 1 M M 1 $ Match M M $ R4 M $ R4

Page 5: Model answer of compilers june spring 2013

Page 5 of 5

Empty $ Accept d. Show the 10 steps for bottom-up parsing of the sample above. [10 marks] Solution:

Stack Input Action $ if (0 and 1) if (0 and 0) id = 1*1

$ Shift

$if (0 and 1) … Shift $if( 0 and 1) …. Shift $if(0 and 1) …. Reduce R9 $if(N and 1) …. Shift $if(N and 1) …. Shift $if(N and 1 ) if (0 and 0) id = 1*1 $ Reduce R10 $if(N and N ) if (0 and 0) id = 1*1 $ Reduce R7 $if(C ) if (0 and 0) id = 1*1 $ Shift $if(C) if (0 and 0) id = 1*1 $ Reduce R6 $if C if (0 and 0) id = 1*1 $ Shift $if C if (0 and 0) id = 1*1 $ Shift $if C if( 0 and 0) id = 1*1 $ Shift $if C if(0 and 0) id = 1*1 $ Reduce R9 $if C if(N and 0) id = 1*1 $ Shift $if C if(N and 0) id = 1*1 $ Shift $if C if(N and 0 ) id = 1*1 $ Reduce R9 $if C if(N and N ) id = 1*1 $ Reduce R7 $if C if(C ) id = 1*1 $ Shift $if C if(C) id = 1*1 $ Shift $if C if(C) id = 1*1 $ Reduce R6 $if C if C id = 1*1 $ Shift $if C if C id 1*1 $ Shift $if C if C id = 1*1 $ Shift $if C if C id = 1 *1 $ Reduce R10 $if C if C id = N *1 $ Shift $if C if C id = N * 1 $ Shift $if C if C id = N * 1 $ Reduce R10 $if C if C id = N * N $ Reduce R8 $if C if C E $ Reduce R1 $if C if C Z $ Reduce R4 $if C if C Z M $ Reduce R3 $if C L $ Reduce R2 $if C Z $ Reduce R4 $if C Z M $ Reduce R3 $ L $ Reduce R2 $Z $ Accept