0% found this document useful (0 votes)
199 views9 pages

Computation Theory: Lecture Two

This document discusses language grammar and computation theory. It provides examples of phrase structure grammars (PSG) involving non-terminal and terminal symbols. Grammars are defined as sets of rules to construct a language, containing non-terminal symbols, terminal symbols, production rules, and a starting symbol. Different types of grammars are discussed, including regular grammars, ambiguous grammars, and unambiguous grammars. Leftmost and rightmost derivations are demonstrated on sample strings to determine if they are accepted by a given grammar. Homework is assigned involving determining if strings are accepted and identifying if grammars are ambiguous.

Uploaded by

Colmain Nassiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
199 views9 pages

Computation Theory: Lecture Two

This document discusses language grammar and computation theory. It provides examples of phrase structure grammars (PSG) involving non-terminal and terminal symbols. Grammars are defined as sets of rules to construct a language, containing non-terminal symbols, terminal symbols, production rules, and a starting symbol. Different types of grammars are discussed, including regular grammars, ambiguous grammars, and unambiguous grammars. Leftmost and rightmost derivations are demonstrated on sample strings to determine if they are accepted by a given grammar. Homework is assigned involving determining if strings are accepted and identifying if grammars are ambiguous.

Uploaded by

Colmain Nassiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

COMPUTATION THEORY

Lecture Two

Asst.Lec. Amal Abdulbaqi Maryoosh

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

Example: Let the following grammar:


Sentence = Noun Phrase (NP) + Verb Phrase (VP) + Noun Phrase (NP)
NP = Article (Art) + Noun (N)
Art = a or an or the
Noun = Man, Car, House, Dog, …
VP = eat, kill, write, read, …
“The Man hits the dog”
NP VP NP

Sentence

NP VP NP

Art N hits Art N

The Man the dog

Phrase Tree

‫ لذلك فأن هذه الجملة صحيحة‬،‫ في المثال اعاله الجملة مكونة من مفردات وقواعد تنتمي الى نفس اللغة‬
.(Accept) ‫ووفق قواعد هذه اللغة هي مقبولة‬

7
Computation Theory Amal Abdulbaqi Maryoosh

Example: “The dog eats the house”


NP VP NP
‫ من هذا نستنتج ان الجملة‬.‫ الجملة اعاله وفق القواعد صحيحة لكن من ناحية المعنى ليس لها معنى‬
.(semantic) ‫( والمعنى‬syntax) ‫يجب ان تتكون من جزئين مترابطين هما القواعد‬
Terminal Symbol (T): The words that cannot be replaced by anything are called
terminals.
Non-terminal Symbol (N): The words that must be replaced by other things we
call non-terminals.

Grammars

A grammar is a set of rules which are used to construct a language (combine


words to generate sentences).
Grammars are containing four things:
1- A finite set of Nonterminal Symbols (N).
2- A finite set of Terminal Symbols (T).
3- A finite set of production rules (P) of the form u → v ; (u, v) ∈ (N ∪ T)*
4- Starting symbol (S).

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

2- Is the string “bbb” Accept or not?


S → bB
→ bbB
→ bbb
The string is Accept

8
Computation Theory Amal Abdulbaqi Maryoosh

3- Is the string “aaba” Accept or not?


S → aA
→ aaA The string is not Accept

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

2- Is the string “a2b2c2” Accept or not?


S → aSBC
→ aaBCBC
→ aaBBCC
→ aabBCC
→ aabbCC
→ aabbcC
→ aabbcc 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

Example: Derived a4 from the following grammar:


A ⟶ aA | a
A ⟶ aA
⟶ aaA
⟶ aaaA
⟶ aaaa
⟶ a4
،‫ نالحظ في هذه القواعد بأنه يمكن التكرار بأي عدد من الخطوات والتوقف في اي مرحلة من االشتقاق‬
:‫والصيغة العامة للكلمات الناجة من هذه القواعد‬
n
{a , n ≥ 1 by n steps}

Phrase Structure Grammar (PSG)


‫وهي القواعد التي ظهرت في الشكل الشجري بشكل العقد الوسطية التي تتحول في النهاية الى الرموز‬
PSG = (N, T, P, S) ‫ وتتكون ايضا ً من اربعة مجاميع‬،Terminal ‫النهائية‬

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

Left Most Derivation


Example: Let G(L) = ({S}, {a, b, c}, P, S), where P is:
S → SbS | ScS | a
Find the string “abaca”
S → SbS S → ScS
→ abS → SbScS
→ abScS → abScS
→ abacS → abacS
→ abaca Accept → abaca Accept

S S

S b S S c S

a S c S S b S a

a a a a

Right Most Derivation


S → SbS S → ScS
→ SbScS → Sca
→ SbSca → SbSca
→ Sbaca → Sbaca
→ abaca Accept → abaca Accept
S S

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‬‬

‫‪Ambiguous and Unambiguous Grammar‬‬


‫‪Ambiguous Grammar‬‬
‫)‪ ∀X: X ∈ L(G‬يوجد اشتقاقين متميزين من جهة اليمين او اشتقاقين متميزين من جهة اليسار عندها‬
‫تسمى القاعدة غامضة‪.‬‬

‫‪Unambiguous Grammar‬‬

‫)‪ ∀X: X ∈ L(G‬يعطي نفس االشتقاق الشجري سواء من اليمين او اليسار‪.‬‬

‫‪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‬‬

‫‪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

The grammar is ambiguous


b A

Example: Let the following grammar:

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

Example: Let the following grammar:


S
S → S +S | S ∗ S | digit
- Is the string “ 3 + 4 ∗ 5 ” Accept or not?
S + S
- Is the grammar ambiguous or not?

(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

Homework: Let the following grammar:


S → aSa | bSb | a | b | ∧
- Is the string “aabaa” Accept or not?
- Is the grammar ambiguous or not?

14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy