Computation Theory: Lecture Two
Computation Theory: Lecture Two
Lecture Two
Mustansiriyah University
Collage of Education
Computer Science Department
Second Class
2019 - 2020
Computation Theory Amal Abdulbaqi Maryoosh
Lecture Two
Language Grammar
Are the foundations and principles, through which we can link the vocabulary.
Vocabulary
Letters
Word Grammar Language
sentence
Sentence
NP VP NP
Phrase Tree
لذلك فأن هذه الجملة صحيحة، في المثال اعاله الجملة مكونة من مفردات وقواعد تنتمي الى نفس اللغة
.(Accept) ووفق قواعد هذه اللغة هي مقبولة
7
Computation Theory Amal Abdulbaqi Maryoosh
Grammars
G(L) = (N, T, P, S)
Grammar Starting symbol
Language Non terminal Terminal Production rule
Example: Let G(L) = ({S, A, B}, {a, b}, P, S) where P denoted as:
S → aA | bB | a | b
A → aA | a
B → bB |b
1- Is the string “aa” Accept or not?
S → aA (by using S → aA)
→ aa (by using A → a)
The string is Accept
8
Computation Theory Amal Abdulbaqi Maryoosh
Example: Let G(L) = ({S, B, C}, {a, b, c}, P, S) where P denoted as:
S → aSBC | aBC
CB → BC
aB → ab
bB → bb
bC → bc
cC → cc
1- Is the string “abc” Accept or not?
S → aBC
→ abC
→ abc The string is Accept
Homework: Let G(L) = ({S, B, C}, {a, b, c}, P, S) where P denoted as:
S → aSBC | aBC
CB → BC
aB → ab
bB → bb
bC → bc
cC → cc
1- Is the string “a3b3c3” Accept or not?
2- Is the string “a3b2” Accept or not?
9
Computation Theory Amal Abdulbaqi Maryoosh
Regular Grammar
The grammar that contain:
N ⟶ t | tN - Right regular
or
N ⟶ t | Nt - Left regular
Trees
We can represent the grammar of any language through tree that contain:
1- Root: it is a start symbol.
2- PSG: are a Nonterminal symbol.
3- Leaves: are a terminal symbol.
4- Links: collection of connections.
Tree is called syntax tree or parse tree or generation tree or production tree
or derivation tree.
10
Computation Theory Amal Abdulbaqi Maryoosh
S S
S b S S c S
a S c S S b S a
a a a a
S b S S c S
a S c S S b S a
a a a a
11
Computation Theory Amal Abdulbaqi Maryoosh
في المثال السابق نالحظ ان الحل الثاني هو نفس جهة الحل االول ،متكون من اشتقاق من جهة اليمين
واشتقاق من جهة اليسارولهُ نفس الشجرة.
نستنتج من ذلك عندما يوجد اكثر من حل من جهة اليسار او اكثر من حل من جهة اليمين تسمى القاعد
غامضة .ambiguous
Unambiguous Grammar
Example: Let G(L) = ({S, A}, {a, b}, P, S), where P is:
S → AA
A → AAA | bA | Ab | a
?- Is the string “bbaaaab” Accept or not
?- Is the grammar ambiguous or not S
(Left) S → AA
A A
→ bAA
→ bbAA
→ bbAAAA b
b A A
→ bbaAAA
→ bbaaAA
→ bbaaaA b A a
→ bbaaaAb
→ bbaaaab Accept
A A A
12
Computation Theory Amal Abdulbaqi Maryoosh
(Left) S → AA S
→ AAAA
→ bAAAA A A
→ bbAAAA
→ bbaAAA
→ bbaaAA A A A A b
→ bbaaaA
→ bbaaaAb
→ bbaaaab Accept b A a a a
S ⟶ (S) | S⊃S | ~S | p | q
- Is the string “ (~ ~P ⊃ (P ⊃ ~ ~ q)) ” Accept or not?
- Is the grammar ambiguous or not? H.W
(left) S ⟶ (S)
⟶ (S⊃S)
⟶ (~S⊃S)
⟶ (~ ~S⊃S)
⟶ (~ ~P⊃S)
⟶ (~ ~P⊃(S))
⟶ (~ ~P⊃(S⊃S))
⟶ (~ ~P⊃(P⊃S))
⟶ (~ ~P⊃(P⊃~ ~S))
⟶ (~ ~P ⊃(P ⊃ ~ ~ q)) Accept
13
Computation Theory Amal Abdulbaqi Maryoosh
(Left) S ⟶ S + S 3 S S
*
⟶3+S
⟶3+S∗S
⟶3+4∗S
4 5
⟶ 3 + 4 ∗ 5 Accept
a
(Left) S ⟶ S ∗ S S
⟶S+S∗S
⟶3+S∗S
S * S
⟶3+4∗S
⟶ 3 + 4 ∗ 5 Accept
S + S 5
The grammar is ambiguous
3 4
14