Lecture Four Language Grammar
Lecture Four Language Grammar
Lecture Four Language Grammar
Lecture Four
Language Grammar
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
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
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
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?
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.
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}
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
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.
وهي القواعد التي ظهرت في الشكككككش الشككككجري بشكككككش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
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
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
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
(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