Chapter 2 Grammars
Chapter 2 Grammars
Chapter 2 : Grammars
Chapter 2 outline :
VN
VT
So we have : 𝑉𝑁 ∩ 𝑉𝑇 = ∅ 𝑎𝑛𝑑 𝑉𝑁 ∪ 𝑉𝑇 = 𝑉
- 𝑃 : a set of production or rewriting rules. Each rule is of the form 𝛼 → 𝛽 with
𝛼 ∈ (VN ∪ VT )+ and 𝛽 ∈ (VN ∪ VT )∗ ;
A production rule 𝛼 → 𝛽 specifies that the sequence of symbols 𝛼 can be replaced by the
sequence of symbols 𝛽 ;
Module : Language theory 2nd Year Bachelor of Computer Science – 4th Semester Module supervisor : Amiar Lotfi
Chapter 2 : Grammars page 17
Conventions :
a) For the elements of 𝑉𝑇 , we use lowercase letters, numbers, and other symbols.
b) For the elements of 𝑉𝑁 , we only use capital letters.
c) (𝛼 → 𝛽1 , 𝛼 → 𝛽2 , … , 𝛼 → 𝛽𝑛 ) ⇔ (𝛼 → 𝛽1 |𝛽2 | … |𝛽𝑛 ) .
Module : Language theory 2nd Year Bachelor of Computer Science – 4th Semester Module supervisor : Amiar Lotfi
Chapter 2 : Grammars page 18
- We apply the derivation rules to construct words. There are two ways to derive words
from the axiom :
• The leftmost derivation : we start the derivation with the leftmost non-terminal.
• The rightmost derivation : we start the derivation with the rightmost non-terminal.
Definition 2 (language generated by a grammar) : The language of all the words generated
∗
by the grammar 𝐺 is denoted 𝐿(𝐺) such that 𝐿(𝐺) = {𝑤 ∈ 𝑉𝑇∗ |𝑆 ⇒ 𝑤} and we say that the
Example 1 :
Let 𝐺1 (𝑉𝑇 , 𝑉𝑁 , 𝑆, 𝑃) be with : 𝑉𝑇 = {𝑎}, 𝑉𝑁 = {𝑆}, 𝑆, 𝑃 = {𝑆 → 𝑎𝑆|𝜀}
Give :
a) the words generated by this grammar,
b) the general form of this language generated by 𝐺1,
c) the derivation sequence of the word 𝑎𝑎𝑎𝑎𝑎.
Response example 1 :
a) Words generated by this grammar are :
The derivation sequence 𝑆 ⟹ 𝜀 generates the word 𝑤 = 𝜀
The derivation sequence 𝑆 ⟹ 𝑎𝑆 ⟹ 𝑎𝜀 ⟹ 𝑎 generates the word 𝑤 = 𝑎
The derivation sequence 𝑆 ⟹ 𝑎𝑆 ⟹ 𝑎𝑎𝑆 ⟹ 𝑎𝑎𝜀 ⟹ 𝑎𝑎 generates the word 𝑤 = 𝑎𝑎
The derivation sequence 𝑆 ⟹ 𝑎𝑆 ⟹ 𝑎𝑎𝑆 ⟹ 𝑎𝑎𝑎𝑆 ⟹ 𝑎𝑎𝑎𝜀 ⟹ 𝑎𝑎𝑎 generates the word
𝑤 = 𝑎𝑎𝑎
Module : Language theory 2nd Year Bachelor of Computer Science – 4th Semester Module supervisor : Amiar Lotfi
Chapter 2 : Grammars page 19
Example 2 :
Let 𝐺2 (𝑉𝑇 , 𝑉𝑁 , 𝑆, 𝑃) be with: 𝑉𝑇 = {𝑎, 𝑏}, 𝑉𝑁 = {𝑆, 𝐴}, 𝑆, 𝑃 = {𝑆 → 𝑎𝑆|𝑎𝐴 ; 𝐴 → 𝑏𝐴|𝑏}
Give:
a) the words generated by this grammar,
b) the general form of this language generated by 𝐺2,
c) the derivation sequence of the word 𝑎𝑎𝑏𝑏𝑏.
Response example 2 :
a) Words generated by this grammar are :
The derivation sequence 𝑆 ⟹ 𝑎𝐴 ⟹ 𝑎𝑏 generates the word 𝑤 = 𝑎𝑏
The derivation sequence 𝑆 ⟹ 𝑎𝑆 ⟹ 𝑎𝑎𝐴 ⟹ 𝑎𝑎𝑏 generates the word 𝑤 = 𝑎𝑎𝑏
The derivation sequence 𝑆 ⟹ 𝑎𝑆 ⟹ 𝑎𝑎𝑆 ⟹ 𝑎𝑎𝑎𝐴 ⟹ 𝑎𝑎𝑎𝑏 generates the word 𝑤 = 𝑎𝑎𝑎𝑏
The derivation sequence 𝑆 ⟹ 𝑎𝐴 ⟹ 𝑎𝑏𝐴 ⟹ 𝑎𝑏𝑏 generates the word 𝑤 = 𝑎𝑏𝑏
The derivation sequence 𝑆 ⟹ 𝑎𝐴 ⟹ 𝑎𝑏𝐴 ⟹ 𝑎𝑏𝑏𝐴 ⟹ 𝑎𝑏𝑏𝑏 generates the word 𝑤 = 𝑎𝑏𝑏𝑏
4. Derivation tree
Let 𝐺 (𝑉𝑇 , 𝑉𝑁 , 𝑆, 𝑃) be a grammar. The derivation tree is another representation of the
∗
derivation sequence 𝑆 ⇒ 𝑤 that leads from the axiom 𝑆 to the word 𝑤. This representation
abstracts from the order of application of the grammar rules and helps to understand the
syntax of the word considered.
The derivation tree of a word relative to a grammar 𝐺 is a tree such that :
a) the root is labeled by the axiom 𝑆,
Module : Language theory 2nd Year Bachelor of Computer Science – 4th Semester Module supervisor : Amiar Lotfi
Chapter 2 : Grammars page 20
𝒂 𝑆
𝒂 𝑆
𝒂 𝑆
𝒂 𝑆
𝒂 𝑆
𝜺
Fig.1. Derivation tree for word 𝑎𝑎𝑎𝑎𝑎
Example 4 :
The word 𝑤 = 𝑎𝑎𝑏𝑏𝑏 in Example 2 is represented by a derivation tree as follows :
Module : Language theory 2nd Year Bachelor of Computer Science – 4th Semester Module supervisor : Amiar Lotfi
Chapter 2 : Grammars page 21
𝒂 𝑆
𝒂 𝐴
𝒃 𝐴
𝒃 𝐴
In other words, the left-hand member of each rule consists of a single non-terminal symbol,
and the right-hand member consists of a terminal symbol and possibly a non-terminal symbol.
For right regular grammars, the non-terminal symbol must always be to the right of the
terminal symbol, whereas for left regular grammars it must be to the left.
Module : Language theory 2nd Year Bachelor of Computer Science – 4th Semester Module supervisor : Amiar Lotfi
Chapter 2 : Grammars page 22
In other words, the non-terminal symbol 𝐴 is replaced by 𝑤 if we have the contexts 𝛼 on the
left and 𝛽 on the right. Moreover, only the axiom can generate the empty word ((𝑆 → 𝜀) ∈ 𝑃)
and in this case 𝑆 does not appear on the right-hand member of another rule.
6. Classification of languages
The classification of grammars above will allow for the categorization of languages according
to the type of grammars necessary for their generation. A language that can be generated by a
type 𝑖 grammar but not by a grammar of a higher type in the hierarchy, will be called a type 𝑖
language.
Each type of grammar is associated with a type of language :
Module : Language theory 2nd Year Bachelor of Computer Science – 4th Semester Module supervisor : Amiar Lotfi
Chapter 2 : Grammars page 23
Remarks :
- A language can be generated by different grammars that can be of different types. A
language takes the smallest type in the sense of inclusion.
- There are "fundamental" languages for the most commonly used types of languages,
these are languages that illustrate an important property of their class :
• Regular languages (type 3) :
The language 𝑎𝑛 𝑏 𝑚 , with 𝑛, 𝑚 ≥ 0 (words of any length composed only of 𝑎 and/or
𝑏), can be generated by the following grammar :
〈{𝑎, 𝑏}, {𝑆, 𝑆1 }, 𝑆, {𝑆 → 𝑎𝑆 | 𝑆1 |𝜀 ; 𝑆1 → 𝑏𝑆1 |𝜀}〉.
• Context-free languages (type 2) :
The language 𝑎𝑛 𝑏 𝑛 , with 𝑛 ≥ 0 (the language of words composed of a certain
number of 𝑎 followed by the same number of 𝑏), which can be generated by the
following grammar :
〈{𝑎, 𝑏}, {𝑆}, 𝑆, {𝑆 → 𝑎𝑆𝑏 | 𝜀}〉.
• Context sensitive languages (type 1) :
The language 𝑎𝑛 𝑏 𝑛 𝑐 𝑛 with 𝑛 ≥ 1, which can be generated by the following grammar :
Module : Language theory 2nd Year Bachelor of Computer Science – 4th Semester Module supervisor : Amiar Lotfi