Lecture Four Language Grammar

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Computation Theory Amal Abdulbaqi Maryoosh

Lecture Four
Language Grammar
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, play, write, read, …
“The boy hits the dog”
NP VP NP

Sentence

NP VP NP

Art N hits Art N

The boy the dog

Phrase Tree

21
Computation Theory Amal Abdulbaqi Maryoosh

‫ لذلك فأن هذه الجملة صحيحة‬،‫ في المثال اعاله الجملة مكونة من مفردات وقواعد تنتمي الى نفس اللغة‬
.(Accept) ‫ووفق قواعد هذه اللغة هي مقبولة‬
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 Nonterminal Terminal Production rule

Example: Let G(L) = ({S, A, B}, {a, b}, P, S) where P denoted as:
S → aA | bB | a | b rule 1
A → aA | a rule 2
B → bB |b rule 3
1- Is the string “aa” Accept or not?
S → aA using rule 1 (S → aA)
→ aa using rule 2 (A → a)
The string is Accept

22
Computation Theory Amal Abdulbaqi Maryoosh

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


S → bB using rule 1 (S → bB)
→ bbB using rule 3 (B → bB)
→ bbb using rule 3 (B → b)
The string is Accept

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


S → aA using rule 1 (S → aA)
→ aaA using rule 2 (A → aA)
The string is not Accept

Example: Let G(L) = ({S, B, C}, {a, b, c}, P, S) where P denoted as:
S → aSBC | aBC rule 1
CB → BC rule 2
aB → ab rule 3
bB → bb rule 4
bC → bc rule 5
cC → cc rule 6
1- Is the string “abc” Accept or not?
S → aBC using rule 1 (S → aBC)
→ abC using rule 3 (aB → ab)
→ abc using rule 5 (bC → bc)
The string is Accept

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


S → aSBC using rule 1 (S → aSBC)
→ aaBCBC using rule 1 (S → aBC)
→ aaBBCC using rule 2 (CB → BC)
→ aabBCC using rule 3 (aB → ab)
→ aabbCC using rule 4 (bB → bb)
→ aabbcC using rule 5 (bC → bc)
→ aabbcc using rule 6 (cC → cc)
The string is Accept

23
Computation Theory Amal Abdulbaqi Maryoosh

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?

Context-Free Grammar (CFG)

CFG stands for context-free grammar. It is a formal grammar which is used to


generate all possible patterns of strings in a given formal language. Context-free
grammar G can be defined by four tuples as:

G = (N, T, P, S)
Where,
1- G is the grammar, which consists of a set of the production rule. It is used to
generate the string of a language.
2- T is the final set of a terminal symbol. It is denoted by lower case letters.
3- N is the final set of a non-terminal symbol. It is denoted by capital letters.
4- P is a set of production rules, which is used for replacing non-terminals
symbols (on the left side of the production) in a string with other terminal or
non-terminal symbols (on the right side of the production).
5- S is the start symbol which is used to derive the string. We can derive the
string by repeatedly replacing a non-terminal by the right-hand side of the
production until all non-terminal have been replaced by terminal symbols.

Example: Let G(L) = ({S}, {a}, P, S), where P is:


S → aS rule 1
S →∧ rule 2
If we apply production (S → aS) four times and then apply production (S →∧) we
generate the following string: a4

24
Computation Theory Amal Abdulbaqi Maryoosh

S → aS using rule 1
→ aaS using rule 1
→ aaaS using rule 1
→ aaaaS using rule 1
→ aaaa∧ using rule 2
The RE = a* can generate a set of string {∧, a, aa, aaa,.....}. We can have a null
string because S is a start symbol and rule 2 gives S →∧ .

،‫ نالحظ في هذه القواعد بأنه يمكن التكرار بأي عدد من الخطوات والتوقف في اي مرحلة من االشتقاق‬
:‫والصيغة العامة للكلمات الناتجة من هذه القواعد‬
n
{a , n ≥ 0 by n steps}

Example: Construct a CFG for the regular expression (0+1)*


Solution:
The CFG can be given by,
Production rule (P):
S → 0S | 1S
S→∧
The rules are in the combination of 0's and 1's with the start symbol. Since (0+1)*
indicates {∧, 0, 1, 01, 10, 00, 11, …}.

Example: Construct a CFG for a language L = {wcwR: w ∈ (a, b)*}.


Solution:
The string that can be generated for a given language is {aacaa, bcb, abcba, bacab,
abbcbba, ....}
The grammar could be:
S → aSa rule 1
S → bSb rule 2
S→c rule 3

Now if we want to derive a string "abbcbba", we can start with start symbols.
S → aSa
→ abSba using rule 2
→ abbSbba using rule 2
→ abbcbba using rule 3

Thus any of this kind of string can be derived from the given production rules.

25
Computation Theory Amal Abdulbaqi Maryoosh

Example: Construct a CFG for the language {anb2n where n>=1}.


Solution:
The string that can be generated for a given language is {abb, aabbbb, aaabbbbbb,
....}.
The grammar could be: there is another grammar (H.W)
S → aSbb | abb
Now if we want to derive a string "aabbbb", we can start with start symbols.
S → aSbb
→ aabbbb

Homework: Construct a CFG for the language {am bn | m >= n}

Derivation Trees
Derivation tree is a graphical representation for the derivation of the given
production rules for a given CFG. It is the simple way to show how the derivation
can be done to obtain some string from a given set of production rules.
 The derivation tree is also called parse tree or syntax tree or parse tree or
generation tree or production tree or derivation tree.

The properties of parse tree are:


1- Root: The root node is always a node indicating start symbols.
2- PSG: The interior nodes are always the non-terminal nodes.
3- Leaves: The leaf node is always terminal nodes.
4- Links: collection of connections.

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

Derivation
Derivation is a sequence of production rules. It is used to get the input string
through these production rules. During parsing, we have to take two decisions.
These are as follows:

26
Computation Theory Amal Abdulbaqi Maryoosh

- We have to decide the non-terminal which is to be replaced.


- We have to decide the production rule by which the non-terminal will be
replaced.
We have two options to decide which non-terminal to be placed with production
rule.

1. Leftmost Derivation:
In the leftmost derivation, the input is scanned and replaced with the production
rule from left to right. So in leftmost derivation, we read the input string from left
to right.
N ⟶ t | Nt - Leftmost 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

2. Rightmost Derivation:
In rightmost derivation, the input is scanned and replaced with the production rule
from right to left. So in rightmost derivation, we read the input string from right
to left.

27
Computation Theory Amal Abdulbaqi Maryoosh

N ⟶ t | tN - Rightmost 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

‫ متكون من اشكككككككتقاق من جهة‬،‫ في المثال السكككككككابق نالحظ ان الحش الثاني هو نفس جهة الحش االول‬
.‫اليمين واشتقاق من جهة اليسارولهُ نفس الشجرة‬
‫نسككككتنتج من علك عندما يوجد امثر من حش من جهة اليسككككار او امثر من حش من جهة اليمين تسككككمى القاعد‬
.ambiguous grammar ‫غامضة‬
Examples of Derivation:
Example 1:
Derive the string "abb" for leftmost derivation and rightmost derivation using a
CFG given by,
S
S → AB | ∧
A → aB
B → Sb A B
Solution:
Leftmost derivation: a B S b
S → AB
→ aBB
→ aSbB S b ∧ b
→ a∧bB
→ abSb

→ ab∧b
→ abb

28
Computation Theory Amal Abdulbaqi Maryoosh

Rightmost derivation: S
S → AB
→ ASb
A B
→ A∧b
→ aBb
→ aSbb a B S b
→ a∧bb
→ abb ∧ b
S b

Example 2:
Derive the string "aabbabba" for leftmost derivation and rightmost derivation
using a CFG given by,
S → aB | bA S
A → a | aS | bAA
B → b | aS | aBB a B
Solution:
Leftmost derivation: a B B
S → aB
→ aaBB
b b S
→ aabB
→ aabbS
→ aabbaB a B
→ aabbabS
→ aabbabbA b S
→ aabbabba
b A

29
Computation Theory Amal Abdulbaqi Maryoosh

Rightmost derivation: S
S → aB
→ aaBB
a B
→ aaBbS
→ aaBbbA
→ aaBbba a B B
→ aabSbba
→ aabbAbba S
b b S
→ aabbabba
b A b A

a a

Ambiguity in Grammar
A grammar is said to be ambiguous if there exists more than one leftmost
derivation or more than one rightmost derivation or more than one parse tree for
the given input string.
If it gives the same tree derivative from the leftmost derivation or from rightmost
derivation the grammar is called unambiguous.

If the grammar has ambiguity, then it is not good for compiler construction. No
method can automatically detect and remove the ambiguity, but we can remove
ambiguity by re-writing the whole grammar without ambiguity.

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?
(Left) S → AA
→ bAA
→ bbAA
→ bbAAAA
→ bbaAAA
→ bbaaAA

30
Computation Theory Amal Abdulbaqi Maryoosh

→ bbaaaA S
→ bbaaaAb
→ bbaaaab Accept A A

b A A b

b A a

A A A

a a a

(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

31
Computation Theory Amal Abdulbaqi Maryoosh

(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

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?

32

You might also like