Professional Documents
Culture Documents
Esame Automi PDF
Esame Automi PDF
Chen Tiantian
18/feb/2015
Automi e linguaggi !1
Let’s prove L(S) = (a(bb)*)*a + ε with even number of a’s
Automi e linguaggi !2
To build an automaton for (a(bb)*a)* + ε with even number of a’s we have to build separately
two automata:
A2 to accept (a(bb)*)*a + ε
a b
0A 1B -
1B 1A 2B
2B - 3B
3B 1A 2B
1A 1B 2A
2A - 3A
3A 1B 2A
So we obtain A3:
Automi e linguaggi !3
By renaming the states: We get:
a b
0A = 0 0 1 7
1B = 1 1 4 2
2B = 2 2 7 3
3B = 3 3 4 2
1A = 4 4 1 5
2A = 5 5 7 6
3A = 6 6 1 5
sink = 7 7 7 7
Automi e linguaggi !4
ii) A regular expression that denotes the language L(S) can be obtained, by Kleene theorem,
from the automaton A3 transforming it to a regular expression.
The algorithm to get a regular expression from A3 is the one from “Automata Theory and
Formal Languages”, Third Edition by Alberto Pettorossi, on page 47.
Automi e linguaggi !5
• Reasoning
A → BB B → aBBa | BBb | ε
PROOF)
(Basis) k = 0: b* ⊆ L(B) holds by Lemma 4 (below).
(Step) assume ∀v ∈ (a+b)*. |v|a = 2k ⇒ v ∈ L(B)L(B)
To show: ∀w ∈ (a+b)*. |w|a = 2(k + 1) ⇒ w ∈ L(B)L(B)
we have that every word w ∈ (a+b)* with 2(k + 1) a’s is of the form:
bmavabn for some m, n ≥ 0.
bmavabn ⊆ { by hypothesis } ⊆ bmaL(B)L(B)abn ⊆ b*aL(B)L(B)ab* ⊆
⊆ { by B → aBBa} ⊆ b*L(B)b* ⊆ { by Lemma 3 } ⊆ L(B)L(B)b* ⊆
⊆ { by Lemma 1} ⊆ L(B)L(B)
NOTE: w can be written as bmavabn for some m, n ≥ 0 because we have that every word from
(a+b)* with even number of a’s is of the form a*b*a*b*, or well concatenations of a*’s and b*’s
beginning with a’s or b’s, with the only condition that the number of a’s is even.
Now for w where we have 2(k+1) a’s so we can surely collect an internal substring of w as v
which has 2k a’s and leave an ‘a’ on the left of v and another on its right.
Moreover we put b* at the beginning and at the end because there may be also 0 or more b’s.
The b’s right after the leftmost a and right before the rightmost a are collected into v, too.
Automi e linguaggi !6
LEMMA 3) b* ⊆ L(B)
PROOF ) by induction on n
(Basis) ε ⊆ L(B). Holds because B → ε
(Step) assume for: bn ∈ L(B)
To show: bn+1 ∈ L(B)
bn+1 ⊆ bnb ⊆ { by hypothesis } ⊆ L(B)b ⊆
⊆ { because B → ε } ⊆ L(B)L(B)b ⊆
⊆ { because B → BBb } ⊆ L(B)
• LANGUAGES
Automi e linguaggi !7
Show that a* ∪ {ambn | m > n ≥ 0}* is not a regular language and it satisfies the lemma for
regular languages.
for a*:
We can take p = 1 and all the strings of length ≥ 1 are concatenation of one or more ‘a’.
we have that: the possible strings belonging to {ambn | m > n ≥ 0}* of length > 1 are
concatenations of {ambn | m > n ≥ 0}, while a string of length = 1 must be only ‘a’.
So we have two cases:
1) for ‘a’ we have that it satisfies the pumping lemma by picking:
x = ε, y = a, z = ε
Pumping on y indeed produces ε, which belongs to {ambn | m > n ≥ 0}* due to the Kleene
star, or concatenations of a’s that still belong to {ambn | m > n ≥ 0}*
(it’s enough to concatenate strings with m = 1 and n = 0).
Automi e linguaggi !8
i) To prove that the set of languages accepted by deterministic PDA’s by final state properly
includes the set of languages accepted by deterministic PDA’s by empty stack, we must
prove this two points:
1) For any deterministic PDA M which accepts a language L by empty stack there
exists an equivalent deterministic PDA which accepts L by final state.
2) For any deterministic PDA M1 which accepts a language L by final state, it may
not exist an equivalent deterministic PDA M which accepts L by empty stack.
PROOF 1) Let us consider the PDA M = < Q, Σ, Γ, δ, q0, Z0, ∅ > which accepts by
empty stack and the language L(M) it accepts.
We must build a PDA M’ which accepts by final state equivalent to M.
Now we take M’ to be < Q ∪ {q’0, qf }, Σ, Γ ∪ {$}, δ, q0, $, {qf} >
where q’0 is a new initial state and qf is a new final state and $ is a new stack
symbol.
To get the new transition function, we must add the new instructions to δ:
• for each q ∈ Q,
q ε $ —> push ε goto qf
These instructions simulate the acceptance of M by empty stack. If in a
state the stack is empty, M accepts the input string. This must happen in M’
too. In order to do so, whenever M accepts, M’ makes a transition to its unique
final state qf, causing M to accept, as well, by final state.
L = { aibi | i ≥ 1}
This language is accepted by a DPDA M1 by final state, as we will also show in the
point (ii) of this exercise.
Let’s assume that DPDA’s which accept by empty stack are equally powerful as the
DPDA’s which accept by final state.
L contains also the strings ‘ab’ and ‘abab’.
By our assumption, there must exist a deterministic DPDA M2 which accept by empty
stack that also accepts ‘ab’ and ‘abab’.
But in the case of the string ‘abab’, we have that when the DPDA read the substring
‘ab’ , it has its stack empty, and thus cannot make any move. This implies that it
cannot accept ‘abab’. We have found a language that is accepted by a DPDA by final
state but cannot be accepted by a DPDA by empty stack.
ii)
Automi e linguaggi !9
We have that { aibi | i ≥ 1 } is a deterministic context-free language.
Indeed this language can be accepted by the following DPDA by final state:
Now the language {a, ab}* is accepted by the following finite state automaton:
Indeed by (1), (2), (3), (4) we can say that {a, ab}* - {ai bi | i ≥ 1} is a deterministic context-
free language and by (5) is also a context-free language.
• LR(1)
Powerset construction:
a b c $ B
q0 sh 6 - - - goto 1
q1 sh 9 - - accept goto 2
q2 - a sh 3 b sh 8 c - B-
04HE7
q3 - 8I - - - - - 5F19HEC
-
q8I
4 - - red 1 - red 1 - - --
5F19HEC
q5 red 1DI - - red 1 6G - 2AF
-
q6G
6 red 2 - - - red 2 - - --
q2AF
7 - - red 2 3 red 2 BG - --
3 - - - -
with reduce productions:
BG - - - -
0. S → BBb
DI - - - -
1. B → Bc
2. B → a
a b c $ B
q0 sh 6 - - - goto 1
q1 sh 9 - - accept goto 2
q2 - sh 3 sh 8 - -
q3 - - - - -
S → A | Aa A→b|ε
It is not LL(1) as we will show below building the corresponding parsing table,
because it produces conflicts on at least one entry.
First1(A) = { b, ε } Follow1(S) = { $ }
First1(Aa) = { b, ε } Follow1(A) = { a, $ }
First1(b) = { b }
First1(ε) = { ε }
a b $
S - S → A S → Aa S → A S → Aa
A A→ε A→b A→ε
This grammar produces only the strings { ba, b, ε, a }. These strings can be
derived only by one unique leftmost derivation as we’ll show now:
By the other hand G has B which has a left recursion and more than one production
rule: B → Bc | a
this is not allowed in an LL(1) grammar, because it leads to a conflict on the parsing
table as shown below.
S → BBb B → Bc | a
Answer to (i):
a) Recursively enumerable languages are closed under intersection. This fact can be
shown as follows: given two R.E. languages, in this case we have L1 and L2, there
exist two Turing machines M1 and M2 such that:
• L(M1) = L1
• L(M2) = L2.
So M3 accepts iff both M1 and M2 accept, and loop iff one of M1 or M2 loop or both loop.
This implies that L3 is still a R.E. language.
L4 is generated by the grammar G = <VT, VN, P, S> with the following productions in P:
S → aSBC bB → bb
S → aBC bC → bc
CB → BC cC → cc
aB → ab
So there exists a Turing machine, call it M4, which implements the above algorithm and
answers the membership problem of the language { ai bi ci | i > 0 } ∪{ ε }, always halts and:
Now, from the previous points (a) and (b), we can build another Turing machine, call it M5
which includes M3 and M4 as its subroutine and is built this way:
Indeed this resulting Turing machine M5 recognizes a R.E. language, because it halts if and
only if a given instance belongs to the language, and loops otherwise.
The infinite set of the instances of the problem is {a, b}* from L1, L2, union with {a, b, c}*
from L3. So we have {a, b}* ∪ {a, b, c}* = (because {a, b}* ⊆ {a, b, c}* ) = {a, b, c}*
Answer to (ii):
So let’s pick
• P = Haltall
• Q = Halt
P is a proper subset of Q because we have that if there exists any Turing machine M which
always halts on any input, it must halt also on a given w, so the coding <M> of this T.M. M
must be an element of both P and Q.
On the contrary, there may exist at least one Turing machine M which halts on a given w but
does not halt on any input. This coding <M> would be an element of Q but not of P.
{N≥0}
n = N; res = 1;
while ( n > 1 ) do
res = res * ( n2 - n ); n = n - 2;
od
{ res = N! }
Let’s try to run the program with two given numbers 5 and 6:
for N = 5:
for N = 6:
I = { 0 ≤ n ≤ N ∧ res*n! = N! }
Let’s prove the correctness of the given program:
To do so we need to show that these implications hold:
ii.1) 2≤n≤N⇒0≤n-2≤N
this holds because n≥2 ⇒ n-2≥0 (they’re the same)
and n≤N ⇒ n-2≤N
(subtracting 2 to n, it’s obvious that n is still smaller than N)
iii.1) n = 0:
res*0! = N! ⇒ res = N! holds because 0! = 1
iii.2) n = 1:
res*1! = N! ⇒ res = N! holds because 1! = 1
For the termination of the program we can see its behavior from the following flowchart: