Download Presentation

Loading in 3 Seconds

This presentation is the property of its rightful owner.

X

Sponsored Links

- 67 Views
- Uploaded on
- Presentation posted in: General

Key Answers for Final (Sample)

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

1. Let x be a string of length n 0 over alphabet {a, b}, Prove by induction that x is regular expression according to the definition of the regular expressions.

Answer: (1) Base: If x = , , a or b, then by the definition of regular expression, x is regular.

(2) Suppose that for some regular expression r1 and r2, x is written as r1 + r2, r1r2 or (r1)*.

(3) Then according to the definition of regular expression, x must be regular.

2. To prove that a language is not regular, sometimes it is convenient to use the properties of regular languages (see the handout for the properties). For example, suppose that we are going to prove the following language L1 is not regular.

L1 = {aibj | i j, i, j 1 }

Lets try to show that the pumping lemma fails for string z = anbn+1 , which is in L1.

(a) Show that for this string z, the pumping lemma satisfies.

Answer: The pumping lemma says that there exist u, v, and w such that

(i) z = uvw (ii) |uv| n, (iii) |v| 1 , and (iv) for all i 0, uviw L1

Notice that v contains only some a’s. It can contain at least one and at most n. If it contains more than one a’s, then it is easy to see that uviw L1 for all i 0. Thus string z satisfies the lemma.

L3 = L1 L2 , where L2 = {ambn | m, n 0 }

Before we apply the pumping lemma, lets transform the language to other language that can easily be shown to be not

regular as follows. Consider language L3 below.

( b) What is language? Show it in terms of the set property. If L1 is regular, why L3 is regular?

Answer: L3 = {aibi | i 0 }. Since regular languages are closed under complementation and intersection, if = L1 is regular, then its complement is also regular. Since L2 is regular (a*b* is regular expression that shows L2 is regular). Language L3, which is the intersection of the two regular languages, is regular.

(c) Prove that L1 cannot be regular by showing that L3 is not regular.

Answer: We proved that L3 is not regular in the class. (See the lecture note.)

< A >

< S >

< C >

x

a

b

< S >

< C >

< A >

a

b

e

3. Consider the following syntax flow graph.

< A >

(a)Transform the above syntax flow graph to a CFG which generates the same language defined by the graph.

(b) Show that your grammar in part (a) above is ambiguous.

(c) Convert your grammar to a grammar which is not ambiguous, and explain your idea. (Your new grammar should generate the same language.)

S

S

S

(iii)

a C b A e A

a C b A

a C b S

x

b

S

b

S

b

a C b A e A

a C b A e A

a C b A

b

x x

b

x

x

b

x

Answer: Notice that this language is actually has the structure of Pascal’s If-statement, with a in <S> corresponds to

if, <C> to a conditional expression, b to then, e to else, and <A> to statement.

(a) S aCbA | aCbAeA A S | x C a | b

(b) We have the following two parse trees shown in (i) and (ii) below for string abbabbxex:

(i)

(ii)

(c) We change the grammar such that else part, which is e, is always generated together with the nearest if part (the a in production rules for S) located to the left. String abbabbxex can only be generated as shown in figure (iii) above.

S aCbS | aCbAeS | aCbA | aCbAeA

A x C a | b

a

a

a

a

a

4. (a) What is the language accepted by the following finite state automaton? Black nodes are accepting states.

(b) Is this the smallest automaton that accepts the language? Prove your answer.

Answer: (a) L = {an | n mod 5 0}

(b) Label the states with the numbers 1 through 5 going from left to right. The non-accepting state 1 cannot be equivalent to other accepting states. We examine whether some of the accepting states can equivalent. Suppose that state i and state j are equivalent, where 2 i < j 5. Let n = 5 – j + 1. For input of n a’s, state j takes transition to state 1, which is non-accepting state, while state i take transition to an accepting state. Hence, i and j cannot be equivalent. We cannot reduce the number of state. The automaton is the smallest one accepting the language.

We can also prove that the above automaton is reduced one by using the partitioning technique discussed in the class.

(c, a/),

(a, Z0/AZ0),

(a, A/aA),

(a, a/aa),

(b, a/ ),

(b, a/ ),

(c, A/ )

start

(c, A/)

5. Which of the following languages are context-free? Proveyour answer.

L0 = {abnabnabiabi | i, n 1 }

L1 = {abnabiabnabi | i, n 1 }

L2 = {anbmcn-m | n > m > 1 }

Answer:L0= {abnabnabiabi | i, n 1 }is context-free. Here is a context-free grammar which generate L0 .

S aAaA A bAb | bab

L1 = {abnabiabnabi | i, n 1 } is not context-free. Suppose that it is context-free language. Then it should satisfy the pumping lemma for context-free languages. Let p be the constant of the lemma. Consider string z = abpabpabpabp. Notice that among the four blocks of bp, the number of b’s in the first and in the third should be equal, and the number of b’s in the second block and the number of b’s in the fourth block should be same.

Now, consider u, v, w, x and y of the lemma such that z = uvwxy. Since, |vwx| p, if v and x contain only b’s, then these b’s belong to at most two adjacent blocks of bp. It follows that z’ = uv2wx2y L1 . If v and x contain a, then clearly z’ = uv2wx2y L1,because z’ contains consecutive a’s.

L2 = {anbmcn-m | n > m > 1 } is context-free, because this language can be recognized by the following PDA.

(1) (2) (3)

Answer:(a) Lets label the rules of the grammar as follows; S aSB | a B aab

We choose w = aaaaabaab, which can be derived by the leftmost derivation as follows;

(1) (1) (2) (3) (3)

S aSB aaSBB aaaBB aaaaabB aaaaabaab

6. Construct an LL(k) parser with smallest possible k for the following CFG.

S aSB | a B aab

For your answer, show the following:

(a) A parsing profile (i.e., a sequence of configurations) for a typical string that requires k look ahead, together with a clear explanation of why and where the parser needs k look ahead.

(b) Show the parse table of your parser.

(a) LL(k) parsing profile: The parser needs 4 look ahead. If it looks aaaa ahead, next a to be read is generated by rule (1). If it looks aaab ahead, then next a on the input is generated by rule (2).

(q0, aaaaabaab, Z0) (q1, aaaaabaab, SZ0) (q1, aaacbbb, aSBZ0) (q1, aaaabaab, aSBBZ0)

4 look ahead is aaaa. Rule (1) is applied.

4 look ahead is aaab. Rule (2) is applied.

(q1, aaabaab, SBBZ0) (q1, aaabaab, aBBZ0) (q1, aabaab, BBZ0) (q1, aabaab, aabBZ0) . . .

(q1, aab, BZ0) (q1, aab, aabZ0) . . . (q1, , Z0)

4 Look ahead

(b) Parse Table:

aaaa

aaab

xxxx

Stack

top

S

aSB

a

B

aab

3 look ahead

aaa

aab

xxx

BBB

Stack top

portion

a

Shift-in

S

S

aab

B

aSB

S

7. Construct an LR(k) parser for the same grammar above. Your answer should show the following.

(a) A parsing profile (i.e., a sequence of configurations) for a typical string that requires k look ahead, together with a clear explanation of why and where the parser needs k look ahead.

(b) Show the parse table of your parser.

Answer: (a), (b): This grammar has similar rules of the grammar that appears in homework #8, problem 3-(b), which is shown below together with the grammar of this problem. The only difference is nonterminal A (corresponding to B in this problem) and the string generated by A (aab in this problem). So we can apply the same analysis shown in the key answers for homework #7. For this grammar, we can build LR(3) parser (it was LR(6) for the homework), which is shown below.

S aSB | a B aab

S aSA | a A aaaaab

(e) Two states are equivalent if both of them are accepting states.

Flase. Obviously, the two accepting states of the automaton at right are not equivalent.

a

8. Which of the following statements are true? Justify your answer. To justify a negative answer, it is a good strategy to

show a counter example. Your answer should be short and to the point. No point will be given to a simple yes or no

answer.

(a) {a5b5c5} is a regular language. True, because S aaaaabbbbbccccc is regular grammar that generates the language.

(b) For a CFG, at most one epsilon-production rule (together with other rules) is enough to generate any CFL.

True. We learned how to reduce the number of -production rules such that only when the language has as a member, the resulting grammar has the only -production rule S . If the language has no , we can completely eliminate -production rules.

(c) All regular languages are context-sensitive languages. True according to the Chomsky hierarchy.

(d) By the fact that regular expressions are not defined in terms of intersection operation, intersection of two languages

cannot be expressed with regular expression.

False. By the property of regular languages, intersection of two regular languages is regular. Hence, intersection

of two regular languages can be expressed by regular expression.

(f) There is an LR(0) parser. True. Obviously, for grammar S a we can build LR(0) parser.

(g) Every regular languages can be recognized by a deterministic pushdown automaton(DPDA).

True. We can easily build a DPDA that simulates a DFA which recognizes the regular language.

(h) Only regular languages satisfy the pumping lemma for regular languages.

False. See the lecture note showing a non-regular language which satisfies the pumping lemma for regular languages.