Docstoc

3515ICT Theory of Computation Tutorial problems Context-free

Document Sample
3515ICT Theory of Computation Tutorial problems Context-free Powered By Docstoc
					3515ICT Theory of Computation

Tutorial problems: Context-free languages

(All references are to IALC by Hopcroft et al.)
   1. Design context-free grammars for the following languages.

       (a) { 0n 1n | n ≥ 0 }
       (b) {ai bj ck | i = j or j = k }
       (c) {ai bj ck | i = j or j = k }
       (d) The set of strings of a’s and b’s with an equal number of a’s and b’s.
       (e) The set of strings of a’s and b’s with more a’s than b’s.
       (f) The set of even-length strings of a’s and b’s that are not of the form ww. (Hard)
       (g) The set of strings of a’s and b’s with twice as many a’s as b’s. (Very hard.)

   2. (Exercise 5.1.3) Prove by structural induction on regular expressions that every regular
      language is context-free.

   3. (Exercise 5.1.4) A CFG is called right-linear if every production has the form P → wB
      or P → w, where A and B are variables and w is a string of zero or more terminals.
      Prove that a language is regular if and only if it has a right-linear grammar.
      Hint. In one direction, describe how to convert the right-regular language to an equivalent
       -NFA. In the other direction, describe how to convert a DFA (or regular expression) to
      an equivalent right-linear grammar.

   4. Construct a right-linear grammar for the (now familiar) DFA that accepts strings in
      {0, 1}∗ representing binary numbers evenly divisible by 3.

   5. A grammar is called an extended context-free grammar (ECFG) if every production has
      the form P → E, where E is a regular expression over V ∪ T .
      Prove that, for every ECFG G, G has an equivalent CFG, i.e., there exists a CFG G such
      that L(G) = L(G ).

   6. (Exercise 5.3.5) Convert the DTD of Fig. 5.16 to an equivalent context-free grammar.
      Here is the essence of the DTD:
      Courses → Course+
      Course → Name Prof Student∗ Tutor?
      Name → text
      Prof → text
      Student → text
      Tutor → text

   7. Prove that every context-free language over Σ = {1} is regular.




                                                1
 8. Design an unambiguous grammar for the language Leq of strings of a’s and b’s with an
    equal number of a’s and b’s.

 9. Design an unambiguous grammar for the language Lgt of strings of a’s and b’s with more
    a’s than b’s.

10. Suppose a derivation of string w from variable A takes n steps. What can you say about
    the number of nodes in the corresponding parse tree with root A and yield w?

11. Consider the grammar

          S → | SS | iS | iSeS

    where Σ = {i, e}. Show that the string iieie has at least two parse trees (does it have
    more?), and hence conclude that the grammar is ambiguous.

12. (Exercise 5.4.1) Consider the grammar

          S → aS | aSbS |

    where = {a, b}. Show that the string aab has two parse trees, and that the grammar
    is hence ambiguous. (This grammar generates all strings of a’s and b’s such that every
    prefix has at least as many a’s as b’s.)

13. (Exercise 5.4.3) Find an unambiguous grammar for the language of the previous exercise.

14. Suppose L1 and L2 are CFLs. Prove (by considering CFGs for L1 and L2 ) that L1 ∪
    L2 , L1 L2 , L∗ and LR are all CFLs. Construct counterexamples to show that L1 (the
                  1      1
    complement of L1 ) and L1 ∩ L2 are not CFLs.

15. (Exercises 6.2.1 to 6.2.3) Design a PDA to accept each of the following languages.

     (a) { 0n 1n | n ≥ 1 }
     (b) The set of all strings of 0s and 1s such that every prefix has at least as many 0s as
         1s.
     (c) The set of all strings of 0s and 1s with an equal number of 0s and 1s.
     (d) The set of all strings of 0s and 1s with more 0s than 1s.
     (e) The set of all strings of 0s and 1s with twice as many 0s as 1s. (Hard.)
     (f) The set of all strings of as and bs that are not of the form ww, i.e., not equal to some
         string repeated. (Hard)

16. (Exercise 6.3.1) Transform the grammar

         S → 0S1 | A
         A → 1A0 | S |

    to a PDA that recognises the same language. (Describe this language in English or
    mathematics as precisely as you can.)




                                              2
17. (Exercise 6.3.2) Transform the grammar

         S → aAA
         A → aS | bS | a

    to a PDA that recognises the same language. (Describe this language in English or
    mathematics as precisely as you can.)

18. (Exercises 6.3.3 and 6.3.4) These both ask you to transform a given PDA into a context-
    free grammar that generates the same language.

19. Design deterministic PDAs to recognise the following languages:

     (a) Lbal , the set of balanced parenthesis strings.
     (b) Leq , the set of strings of a’s and b’s with an equal number of a’s and b’s.
     (c) Lexp , the set of arithmetic expressions over identifier a.

    For convenience, assume each input string is followed by an “end of text” symbol, $.

20. (Exercises 7.2.1) Use the pumping lemma for context-free languages to show each of the
    following languages is not context-free:

     (a) { ai bj ck | i < j < k }
     (b) { an bn ci | i ≤ n }
     (c) { ap | p is prime }
     (d) { ai bj | j = i2 }
     (e) { am bm cn | m ≤ n ≤ 2m }
      (f) { wwR w | w ∈ {a, b}∗ }

21. (Exercises 7.3.1 and 7.3.2) Closure questions.

22. Determine whether each of the following languages is context-free or not. If the language
    is context-free, give a grammar for the language; otherwise, use the pumping lemma to
    prove it is not context-free.

     (a) { am bn cm+n | m, n ≥ 0 }
     (b) { am bm+n cn | m, n ≥ 0 }
     (c) { am bn cm×n | m, n ≥ 0 }
     (d) Let Σ be a finite alphabet not containing c. Let symbols(w) be the set of distinct
         symbols in the string w.
              { vcw | v, w ∈ Σ∗ , symbols(v) ⊇ symbols(w) }
         (This language is intended to capture the property that variables need to be declared
         before they are used.)

23. (Exercise 7.4.1 (a) and (b)) Decidability questions. Discussed later.

24. (Exercises 7.4.3 and 7.4.5) Ditto.



                                              3

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:192
posted:3/7/2010
language:English
pages:3
Description: 3515ICT Theory of Computation Tutorial problems Context-free