L3 Grammar

You might also like

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

Grammar

1/7/2021 M.Prathilothamai 1
Formal Definition

• A Grammar is a 4-tuple representation G = (V , T , P , S)

• V = Finite non-empty set of non-terminal symbols


• T = Finite set of terminal symbols
• P = Finite non-empty set of production rules
• S = Start symbol

1/7/2021 M.Prathilothamai 2
Grammar Constituents

1. Terminal Symbols-

•Terminal symbols are those which are the constituents of the sentence
generated using a grammar.
•Terminal symbols are denoted by using small case letters such as a, b, c etc
except u,v,w.

2. Non-Terminal Symbols-

•Non-Terminal symbols are those which take part in the generation of the
sentence but are not part of it.
•Non-Terminal symbols are also called as auxiliary symbols or variables.
•Non-Terminal
1/7/2021
symbols are denoted by using capital letters such as A, B, C etc.
M.Prathilothamai 3
Example Grammar
G ({s},{a, b},S,P) Where P is given by Minimal String
S→aSb S=>λ
S→λ
Find language of the given grammar.
Derivation1:
S=>aSb Derivation3:
Derivation2:
=>ab S=>aSb
S=>aSb
=>aaSbb =>aaSbb
L={anbn|n>=0} =>aabb =>aaaSbbb
=>aaabbb
1/7/2021 M.Prathilothamai 4
Examples
• Consider a grammar G = (V , T , P , S) where-
V={S} // Set of Non-Terminal symbols
T={a,b} // Set of Terminal symbols
P = { S → aSbS , S → bSaS , S → ∈ } // Set of production rules
S={S} // Start symbol

• What are all the strings generated by the given grammar?

1/7/2021 M.Prathilothamai 5
Strings generated by the grammar
Derivation1: Derivation2: V → { S}
S => aSbS S => bSaS T → { a , b}
=>abS S→aSbS =>baS S→aSbS P → { S → aSbS ,
=>ab S→ ∈ =>ba S→ ∈ S → bSaS , S → ∈ }
S→{S} //start
S→ ∈ S→ ∈
symbol
Derivation3: Derivation4:
S => aSbS S => bSaS
=>abSaSbS =>baSbSaS
=>abaSbS =>babSaS
=>ababS =>babaS
=>abab =>babaaSbS
=>babaabS
=>babaab
L={∈,ab,ba,abab,baba,babaab,abaabb,….}
• This grammar generates the strings having equal number of a’s and b’s

1/7/2021 M.Prathilothamai 6
Example
• Consider a grammar G = (V , T , P , S) where-

V={S,A,B} // Set of Non-Terminal symbols


T={a,b} // Set of Terminal symbols
P = { S → ABa , A → BB , B → ab , AA → b } // Set of production rules
S={S} // Start symbol

Find the language generated by the grammar

1/7/2021 M.Prathilothamai 7
Grammar Construction
• Construct a grammar for the language L = { anbn+1 , n>=0 }

• First construct for anbn then add 1 b extra


S→Ab/ ∈
A→aAb/∈

1/7/2021 M.Prathilothamai 8
Multiple grammars are possible for a
language
• Construct a grammar for the language L = { anbn , n>=0 }

• Grammar G1-
• Consider a grammar G = (V , T , P , S) where-
V={S } // Set of Non-Terminal symbols
T = { a , b , ∈} // Set of Terminal symbols
P = {S → aSb / ∈} // Set of Production rules
S={S} // Start symbol

1/7/2021 M.Prathilothamai 9
Another equivalent grammar
• Grammar G2-
• Consider a grammar G = (V , T , P , S) where-
V = { S, A } // Set of Non-Terminal symbols
T = { a , b , ∈} // Set of Terminal symbols
P = {S → aAb / ∈,A → aAb / ∈} // Set of Production rules
S={S} // Start symbol

1/7/2021 M.Prathilothamai 10
Practice Problems
• Give a verbal description of the language generated by

• Find the grammar for the following Languages

1/7/2021 M.Prathilothamai 11
Cont….

1/7/2021 M.Prathilothamai 12

You might also like