HW 1
HW 1
then x R+ y.
Solution:
A:
To prove: there is a chain of at least 1 R element when x transitively ‘R’s y.
Base case: k = 1
This yields z0 , z1 such that z0 R z1 . By definition, z0 = x and zk = z1 = y, so by substitution x R y. Which
tracks because it is given that x R+ y, which by definition of R+ means x R y.
Proven: there indeed always exists some k (trivial case: k = 1). I don’t see the need for induction…
Inductive step: for n > 1, P (n)⇒P (n + 1)
B:
2. 20 points Let Σ := {a, b} and L ⊆ Σ∗ be the language of words that do not contain ”aa“ as a substring. Give
a regular expression that generates L. Justify that your regular expression generates L.
Solution:
First, lets start with generating any sequence of a and b: b∗ (a∗ b∗ )∗
This lets us start with any amount of b, then alternate independent amounts of a and b, for an undetermined
amount of alternations.
However, this definition allows “bab” to be interpreted as “one b, (zero a zero b), (one a one b)”. That middle
term is quite distracting noise, so we can redefine it to not generate those epsilon terms:
equivalent: b∗ (aa∗ bb∗ )∗
This still generates any sequence of a and b. It just makes sure that in each alternation, progress is actually
made.
For the second requirement, to not contain “aa”: any repetition of more than 1 a contains “aa”. So it is trivial
to amend the expression to disallow consecutive as by simply removing a∗ .
L = b∗ (abb∗ )∗
Languages and Machines – HW1 Page 2 of 3
(a) 6 points Give a context-free grammar generating the set L(G1 ) ∪ L(G2 ).
(b) 6 points Give a context-free grammar generating the set L(G1 )L(G2 ).
(c) 8 points Give a context-free grammar generating the set L(G1 )∗ .
Justify briefly why your proposed grammars are correct.
Solution:
Let S1 be the start symbol of G1 . Let S2 be the start symbol of G2 .
a. The union of 2 languages is the disjunction of their starting symbols.
G : S → S1 | S2
L(G) produces all strings in G1 as well as all strings in G2 , but does not cross contaminate their individual
production rules. It should however be noted that parsing ambiguities may arise, for example in the trivial
case where S1 = S2 .
b. The concatenation of 2 languages is the concatenation of their starting symbols.
G : S → S1 S2
S1 → ab | a
S2 → ϵ | b
with the string “ab”. Is that S1 → ab and S2 → ϵ, or is that S1 → a and S2 → b? No one knows…
c. The kleene star of a language is repetition in any amount of its starting symbol. To notate repetition in
the language of formal grammars, we must abuse the concept of recursion in a production rule to generate
a degerenate parse tree (one should rather consider it a linked list).
G : S → S1 S | ϵ
This grammar generates either the empty string, or a string in L(G1 ) followed by (recurse here). Ambiguities
may be introduced, for instance in the case that
S1 → x | xx
where x is some other symbol in G1 . In sexpr notation, it is unclear if the string “xx” is (S(S1 xx)(Sϵ)) or
(S(S1 x)(S(S1 x)(Sϵ))).
S → ABS | ASa
A → ϵ | c | AA
B → bb | CA
C→ϵ
Languages and Machines – HW1 Page 3 of 3
Solution:
1. Make start symbol non recursive:
S→T
T → ABT | AT a
A → ϵ | c | AA
B → bb | CA
C→ϵ
2. Remove ϵ productions:
S→T |ϵ
T → ABT | AT a
A → cA | c | AA
B → bb | A
S→T |ϵ
T → ABT | AT a
A → cA | c
B → bb | cA | c