นายฉัตรชัย อัศวินโกวิท
545020154-5
Assignment 3 : 322721 Theory of Computation
Context-Free Language and Parsing
1. Consider the grammar given below for the Language defined by 0*10*
SA1A
AA0|^
(a) Convert this grammar to one without ^-productions
(b) Transform to CNF
(c) Find all strings that have derivation trees that have no self-embedded nonterminals
a) จาก grammar เดิม จะเห็นว่า A เป็ น nullable variable ดังนัน้ แทน A=^ จะได้ grammar ใหม่เป็ น
SA1A | 1A | A1 | 1
AA0 | 0
b) แปลง grammar จากข้ อ a) เป็ น CNF
ให้ R00 , R11 และ RA1A1 (RA1AR1) เมื่อแทนเข้ าไปใน grammar เดิมจะได้ CNF เป็ น
SRA1A | R1A | AR1 | 1
AAR0 | 0
RA1AR1
R00
R11
c) เขียนทุกแบบ นอกจากแบบทีเ่ รี ยกตัวเอง (AAR0) ได้ ดังนี ้
S
RA1
A
R1
0
1
S
S
S
A
R1
A
A
R1
0
1
0
0
1
1
นายฉัตรชัย อัศวินโกวิท
545020154-5
2. (a) Prove that the following language is not context free language
L1 = { an bn cj | n≤j≤2n }.
(b) Simplify the following grammar
S->AB
A->a
B->C
B->b
C->D
D->E
a)
จาก pumping lemma : w = uvxyz , |vxy| ≤ 2p , |x| > 0 และ |v|+|y| > 0
สมมติให้ L1 เป็ น context-free language
u=^
v = an
x = bn
y = cj
z=^
pump 2 :
w = anbncj
w’ = a2nbnc2j
จะได้ w’∉ L1 ดังนัน้ L1ไม่เป็ น context-free language
b) ลดรูป grammar โดย
SAB
Aa
BC ตัดออกเพราะเป็ นการเพิ่ม nonterminal โดยไร้ ประโยชน์
ต้ นทางซ ้า นาไปเพิม่ ที่ BC
CD ตัดออกเพราะเป็ นการเพิ่ม nonterminal โดยไร้ ประโยชน์
DE ตัดออกเพราะเป็ นการเพิ่ม nonterminal โดยไร้ ประโยชน์
จะได้ grammar ใหม่ดงั นี ้
Bb
SAB
Aa
Bb
หรื อ
Sab
นายฉัตรชัย อัศวินโกวิท
545020154-5
3. Convert the following grammar into Chomsky normal form (CNF).
SAaD
AaB|bAB
Bb
Dd
กาหนดให้ R1 a , RAA Aa (RAA AR1) , RBA bA (RBA BA)
จะได้ CNF ดังนี ้
S RAAD
A R1B | RBAB
RAA AR1
RBA BA
R1 a
Bb
Dd
4. What is meant by ambiguous grammar? Test whether the grammar is ambiguous or not.
S->A|B
A->aAb|ab
B->abB|^
Ambiguous grammar หมายถึง grammar ที่คลุมเครื อ โดยการที่มี string ใดๆ สามารถถูกสร้ างโดย grammar
หนึง่ ๆ ได้ หลายทาง หรื อ สามารถนาไปเขียน tree ได้ มากกว่าหนึง่ แบบ
ทดสอบสร้ าง tree สร้ าง string : ab โดยใช้ grammar ข้ างต้ น
แบบที่ 1
แบบที่ 2
SS
SS
AA
BB
ab
ab
ab
ab
BB
^^
สามารถเขียน tree ในการสร้ างข้ อความ ab ได้ มากกว่าหนึง่ แบบแสดงว่า grammar นี ้คลุมเครื อ
นายฉัตรชัย อัศวินโกวิท
545020154-5
Turing Machine
5. For each of the following languages construct a Turing machine that recognizes the
language.
a. { xyx | x and y are in {a, b}* and |x| ≥1 }
b. { xy | xy is in {a, b, c}*, |x| = |y|, and (the number of a's in x) = (the number of a's in y) }
c. { xy | xy is in {a, b, c}*, |x| = |y|, and (the number of a's in x) ≠(the number of a's in y) }
d. { aixbi | x is in {a, b}*, and i = (the number of a's in x) = (the number of b's in x) }
e. { aibicjdj | i≠j }
f. { aba2b2a3b3 …anbn | n ≥ 0 }
a) { xyx | x and y are in {a, b}* and |x| ≥1 }
(a,a,R)
(b,b,R)
(b,b,R)
(b,b,L)
(a,a,R)
(a,#,R)
q1
(#,#,L)
q2
(a,#,R)
(#,#,L)
q3
(a,#,L)
(#,#,R)
q0
(#,#,R)
q4
HALT
(#,a,L)
q5
(a,a,R)
(a,b,L)
(b,a,L)
(b,#,R)
q8
(a,a,R)
(b,b,R)
(#,#,L)
q9
(b,b,L)
(b,#,R)
(#,#,L)
q10
q6
(b,b,R)
(b,b,L)
(#,#,R)
(#,b,L)
q7
นายฉัตรชัย อัศวินโกวิท
545020154-5
b) { xy | xy is in {a, b, c}*, |x| = |y|, and (the number of a's in x) = (the number of a's in y) }
(a,a,L)
(b,b,L)
(c,c,L)
(a,a,L)
(b,b,L)
(c,c,L)
q0
(#,X,R)
q1
(a,a,R)
(b,b,R)
(c,c,R)
(#,Y,L)
(b,b,R)
(a,X,L)
(a,a,R)
(b,b,R)
q2
(#,#,L)
q3
(b,X,L)
q4
(X,a,R)
q5
(a,a,L)
(b,b,L)
(c,c,L)
(X,a,R)
q7
(Y,Y,L)
(a,Y,R)
q8
(b,Y,R)
q9
q10
(Y,a,L)
(Y,b,L)
(c,X,L)
(X,a,R)
(Y,Y,L)
q6
(a,a,L)
(b,b,L)
(c,c,L)
q13
(b,b,R)
(#,#,R)
(a,A,L) (b,b,R)
(c,c,R)
q14
(X,X,R)
(a,A,R)
(b,b,R)
(c,c,R)
q15
(X,X,R)
(Y,Y,R)
HALT
(Y,c,L)
(c,Y,R)
q11
(b,b,R)
q12
นายฉัตรชัย อัศวินโกวิท
545020154-5
c) { xy | xy is in {a, b, c}*, |x| = |y|, and (the number of a's in x) ≠(the number of a's in y) }
(a,a,L)
(b,b,L)
(c,c,L)
(a,a,L)
(b,b,L)
(c,c,L)
q0
(#,X,R)
q1
(a,a,R)
(b,b,R)
(c,c,R)
(#,Y,L)
(b,b,R)
(a,X,L)
(a,a,R)
(b,b,R)
q2
(#,#,L)
q3
(b,X,L)
q4
(X,a,R)
q5
(a,a,L)
(b,b,L)
(c,c,L)
(X,a,R)
q7
(a,Y,R)
(Y,Y,L)
q8
q9
(b,Y,R)
q10
(Y,a,L)
(Y,b,L)
(c,X,L)
(X,a,R)
q6
q11
(#,#,R)
(Y,Y,L)
HALT
(#,#,L)
(b,b,R)
q13
(X,X,L)
(Y,c,L)
(c,Y,R)
(b,b,R)
(a,A,R)
q14
(b,b,R)
(c,c,R)
(b,b,L)
(c,c,L)
(a,A,L)
(Y,Y,L)
q16
q15
(b,b,R)
(c,c,R)
(X,X,R)
(Y,Y,R)
q12
นายฉัตรชัย อัศวินโกวิท
545020154-5
i
i
d) { a xb | x is in {a, b}*, and i = (the number of a's in x) = (the number of b's in x) }
(a,a,R)
(b,b,R)
(A,A,R)
(B,B,R)
q0
(a,A,R)
q1
(a,a,L)
(b,b,L)
(A,A,L)
(B,B,L)
(a,a,L)
(A,A,L)
(B,B,L)
(#,#,L)
q2
(b,B,R)
(A,A,R)
(B,B,R)
(#,#,R)
q3
q4
(a,A,R)
e) { aibicjdj | i≠j }
(c,Y,R)
(c,Y,R)
(a,X,R)
(b,X,R)
q0
(#,#,L)
q1
(#,#,R)
(Y,#,R)
q2
(X,#,R)
(X,X,R)
(Y,Y,R)
(#,#,L)
q3
(#,#,L)
(Y,#,L)
(#,#,L)
q4
f) { aba2b2a3b3 …anbn | n ≥ 0 }
(B,B,R)
(A,A,R)
(B,B,R)
(a,a,R)
(b,B,L)
q0
q1
(a,A,R)
(#,#,R)
HALT
HALT
(#,#,L)
HALT
นายฉัตรชัย อัศวินโกวิท
545020154-5
6. Find a Turing machine that computes f(x)=2x +1.
กาหนดให้
𝛴 = {0,1,2,3,4,5,6,7,8,9} , 𝛤 = {0,1,2,3,4,5,6,7,8,9,#,$} โดย # คือ blank
ขันตอน
้
(กาหนดให้ Input คือ x และมีการใส่ Input ข้ างหลัง $)
1) Tape เมื่อเริ่ มต้ นทางาน
$
5
0
#
#
#
#
…
2) เลือ่ น Tape Head ไปขวาสุด
$
5
0
#
#
#
#
…
3) ทา x + x
1
0
0
#
#
#
#
…
4) เลือ่ น Tape Head ไปขวาสุด
1
0
0
#
#
#
#
…
5) ทา (x+x)+1
1
0
1
#
#
#
#
…
จากขันตอนสามารถเขี
้
ยน Turing Machine ได้ ดงั นี ้
(x,x+x,L)
x+x< 10
(any,any R)
q0
(#,#,L)
(any,any,R)
No
Carry
(x,x+x-10,L)
x+x≥ 10
($,#,R)
q1
(x,x+x+1,L)
x+x< 10
Owe
Carry
(x,x+x-10+1,L)
x+x≥ 10
($,1,R)
(#,#,L)
No
Carry
(x,0,L)
x+1=10
Owe
Carry
(x,0,L)
x+1= 10
ในที่นี ้ x คือตัวเลขหนึง่ หลักใน Tape
(x,x+1,L)
x+1< 10
HALT
(x,x+1,L)
x+1< 10
นายฉัตรชัย อัศวินโกวิท
545020154-5
7. Find the turing machines called minus that computes the subtraction operator. It
takes an input string of the form 0m10n on the TAPE and leaves 0m-n string on the
TAPE with ‘-’ placed in front of 0m-n as shown following:
a) Input format 0m10n
b) Output format +0m-n
, if m>n
all blank , if m=n
-0m-n
, otherwise
a)
(0,0,R)
(0,0,R)
(1,1,R)
q0
(1,1,L)
q1
HALT
b)
(0,0,L)
(1,-,R)
(0,0,R)
q10
HALT
q9
(#,#,L)
(1,#,L)
q8
(0,0,L)
(#,+,R)
q7
(0,0,L)
(1,1,L)
q6
q11
(1,1,R)
(0,0,R)
q0
(0,#,R)
(0,0,R)
q1
(0,0,R)
(0,0,R)
(1,1,R)
q2
(#,#,L)
(#,#,R)
q3
(0,0,R)
(0,#,L)
q4
(0,0,L)
q5
(1,1,L)
นายฉัตรชัย อัศวินโกวิท
545020154-5
Variation on Turing Machine
8. Find a Turing machine (TM1) that computes the function f(w)=w#w, where # is a
blank, w*,={0,1} for the following tapes.
a. One-way tape
b. Two-way tape
a)
(X,0,R)
(0,0,R)
(1,1,R)
(0,X,R)
q0
(#,#,R)
q1
(0,0,L)
(1,1,L)
(#,#,L)
(0,0,R)
(1,1,R)
(#,#,R)
q2
(#,0,L)
q3
HALT
(1,X,R)
q4
(#,#,R)
q5
(#,0,L)
(0,0,L)
(1,1,L)
(#,#,L)
(0,0,R)
(1,1,R)
(0,0,R)
(1,1,R)
q6
(X,1,R)
b)
(X,0,L)
(0,0,L)
(1,1,L)
(0,X,L)
(0,0,R)
(1,1,R)
q0
(#,#,L)
q1
(#,#,L)
q2
(0,0,R)
(1,1,R)
(#,#,R)
(0,0,L)
(1,1,L)
(#,#,L)
q3
(#,0,R)
q4
HALT
(1,X,L)
q5
(#,#,L)
q6
(0,0,L)
(1,1L)
(0,0,L)
(1,1,L)
(X,1,L)
(#,0,R)
q7
(0,0,R)
(1,1,R)
(#,#,R)
นายฉัตรชัย อัศวินโกวิท
545020154-5
9. Construct a 3-TM to add two n-bit binary numbers as the following example.
$
$
$
1
1
#
0
0
#
Input tape
1
1
0
1
#
#
#
#
#
#
#
#
$
$
$
Tape head
1
1
#
0
0
#
Output tape
1
1
0
1
#
#
#
#
#
#
#
#
Tape head
Note: # represent a blank.
No
Carry
Start
HALT
Owe
Carry
กาหนดให้
𝑥 คือ ข้ อมูลในแต่ละตัวที่อา่ นได้ จาก tape 1
𝑦 คือ ข้ อมูลในแต่ละตัวที่อา่ นได้ จาก tape 2
© Copyright 2026 Paperzz