Preview

String and Regular Expression

Satisfactory Essays
Open Document
Open Document
367 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
String and Regular Expression
CS 3133 Foundations of Computer Science
C term 2014

Solutions of the Sample Problems for the Midterm
Exam
1. Give a regular expression that represents the set of strings over Σ =
{a, b} that contain the substring ab and the substring ba.
Solution:
a+ b+ a(a ∪ b)∗ ∪ b+ a+ b(a ∪ b)∗
(20 points)
2. Consider the following grammar G:
S → SAB|λ
A → aA|a
B → bB|λ
(a) Give a leftmost derivation of abbaab.
(b) Build the derivation tree for the derivation in part (1).
(c) What is L(G)?
Solution:

1

(a) The following is a leftmost derivation of abbaab:
S

⇒ SAB
⇒ SABAB
⇒ ABAB
⇒ aBAB
⇒ abBAB
⇒ abbBAB
⇒ abbAB
⇒ abbaAB
⇒ abbaaB
⇒ abbaabB
⇒ abbaab

(b)
S

A

S

S

A

B a a

B

b

A

b

B

B a b

B

(c)
L(G) = a(a ∪ b)∗ ∪ λ
(20 points)
3. Construct a regular grammar over the alphabet Σ = {a, b, c, d} whose language is the set of strings that contain exactly two b-s.
Solution:
The following is a regular grammar over {a, b, c, d} whose language is the set of strings containing exactly two b-s:
S → aS | cS | dS | bB
B → aB | cB | dB | bC
C → aC | cC | dC | λ
2

(20 points)
4. Consider the following grammar G:
S → aSA|λ
A → bA|λ
(a) Give a regular expression for L(G).
(b) Is G ambiguous? Explain your answer.
Solution:
(a) The following is a regular expression for L(G): a+ b∗ ∪ λ
(b) Yes the grammar is ambiguous. Here are two different leftmost derviations for the string aabb:
S

and

⇒ aSA
⇒ aaSAA
⇒ aaAA
⇒ aabAA
⇒ aabbAA
⇒ aabbA
⇒ aabb

S

⇒ aSA
⇒ aaSAA
⇒ aaAA
⇒ aaA
⇒ aabA
⇒ aabbA
⇒ aabb

(20 points)
5. Design a DFA that accepts the language consisting of the set of those strings over {a, b, c} in which the number of a’s plus the number of b’s plus twice the number of c’s is divisible by six.
3

Solution:
The state diagram of a DFA is

a,b q 1

q
2

a,b

a,b

c c c

c

c

q0

q3 c a,b q 5

a,b

(20 points)

4

q

a,b

You May Also Find These Documents Helpful

  • Satisfactory Essays

    Review Sheet 38 Physioex

    • 317 Words
    • 2 Pages

    10. 1) C, 2) B, 3) E, 4) F, 5) J, 6) P, 7) G, 8) J, 9) A…

    • 317 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Sequence A: AGA AGG GAG GAU UUG CAA GGU GGC CAA GAA UUA GGC GGC GGU CCC GGG GCG GGG AGU CUU CAA CCA…

    • 366 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    Unit 4 Review

    • 863 Words
    • 5 Pages

    3. Analysis of radial cracks from three impacts revealed that cracks from A stopped at cracks from Z. Cracks from Z stopped at cracks from B. Which is the correct order of the impacts from first to last?…

    • 863 Words
    • 5 Pages
    Good Essays
  • Powerful Essays

    Geometry Practice Quizz

    • 3469 Words
    • 14 Pages

    Name: ______________________ ____ 3. Assume all angles are right angles. What is the area of the figure?…

    • 3469 Words
    • 14 Pages
    Powerful Essays
  • Good Essays

    Part I: Vocabulary: Look up each word below. Write a short definition, the part of speech, and then use each word in a sentence.…

    • 529 Words
    • 2 Pages
    Good Essays
  • Satisfactory Essays

    Mechanism and Rate Law

    • 603 Words
    • 2 Pages

    3A → products or A + 2B → products or A + B + C → products…

    • 603 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Lab 4 the Microscope

    • 661 Words
    • 3 Pages

    | Assume that the magnification of the ocular lens of a compound light microscope is 10X. What is the total magnification of the microscope if the objective lens is 4X?…

    • 661 Words
    • 3 Pages
    Satisfactory Essays
  • Good Essays

    Annotating In Cold Blood

    • 684 Words
    • 3 Pages

    A. This is a forum to strengthen your analysis skill for the AP language Exam. You and your…

    • 684 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Petries Week 5

    • 372 Words
    • 2 Pages

    a. The words should all be in the same language. Some is in English some is in a different language.…

    • 372 Words
    • 2 Pages
    Good Essays
  • Satisfactory Essays

    LIN 204 Assignment 1

    • 553 Words
    • 3 Pages

    PART I (8 points): Identify the syntactic category of each of the underlined words in the following sentences. For nouns, indicate whether they are count or mass; for verbs, indicate whether they are intransitive, transitive, or ditransitive. Your answers should be based on how the words are used in the sentences, not on other ways in which the same words might be used in different contexts. The first one has been done for you as an example.…

    • 553 Words
    • 3 Pages
    Satisfactory Essays
  • Powerful Essays

    MAth

    • 1081 Words
    • 5 Pages

    Type your answers to the following questions using complete sentences and correct grammar, spelling, and syntax. Note that the form is expandable. Indeed, you may need more pages to include the information that is requested in the guidelines for this assignment.…

    • 1081 Words
    • 5 Pages
    Powerful Essays
  • Satisfactory Essays

    Ane Frank

    • 270 Words
    • 2 Pages

    Answer the following questions in complete sentences as you work on Lessons 1–3. Be sure to turn this assignment in to your teacher when you finish Lesson 3.…

    • 270 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Online Job Posting

    • 443 Words
    • 2 Pages

    2. Proofread each of the following sentences, and then make changes so that the sentences are clear, without any errors.…

    • 443 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    assignment 2R

    • 388 Words
    • 2 Pages

    2. Proofread each of the following sentences, and then make changes so that the sentences are clear, without any errors.…

    • 388 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    2 Vocab in Context 1. a 2. a 3. c 4. d 5.…

    • 2040 Words
    • 9 Pages
    Satisfactory Essays

Related Topics