Grammar
Grammar
Automata
GRAMMER
Presented to
Dr. Saliah Zahoor
Group Members
History:
Noam Chomsky gave a Mathematical model of Grammar which is effective for writing computer
languages.
Grammar :
It is a finite set of formal rules for generating syntactically correct sentences or meaningful correct
sentences.
Constitute Of Grammar :
Basically composed of two elements:
i. Terminal Symbols → represented by small letters(a,b,c)
ii. Non-Terminal Symbols/Auxiliary Symbols/ Variables → represented by Capital letters (A,B,C)
Components
Formal Definition of Grammar :
Any Grammar can be represented by 4 tuples → <V, T, P, S>
∑ → Input Symbol
V → Finite Non-Empty Set of Non-Terminal Symbols. (In Capital letters) (A,S,B)
T → Finite Set of Terminal Symbols. (In small letters) (a to z, 0 to 9, λ)
P → Finite Non-Empty Set of Production Rules.
S → Start Symbol (Symbol from where we start producing our sentences or strings).
Production Rules :
A production or production rule in computer science is a rewrite rule specifying a symbol substitution
that can be recursively performed to generate new symbol sequences.
V = {S,A,B}
T = {a,b}
S=S
Production Rule:
S → AB
A →a
B→b
Solution:
S ⇒ AB ⇒aB ⇒ab
Thus, String “ab” can be drive using the above grammar.
Derivation Order Regular Grammar
Regular Grammar can be divided into two
types:
Right Linear Grammar Left Linear Grammar
A Grammar is said to be Right Linear if all productions are of the A Grammar is said to be Left Linear if all productions are of the form
form
A →Bx
A →xB A →x
A →x Where A,B ∈ V and x ∈ T
Where A,B ∈ V and x ∈ T
Example: S →abS │b Example: S →Sbb │b
Example: S →AB Example: S →AB
A→aaA │λ A→aaA │λ
B→Bb │λ B→Bb │λ
S ⇒ AB [by S → AB] S ⇒ AB [by S → AB]
S ⇒ ABb [by B→Bb ] S ⇒ aaAB [by A→aaA ]
S ⇒ Aλb ⇒ Ab [by B→λ] S ⇒ aaλB ⇒ aaB [by A→λ]
S ⇒ aaAb [by A→aaA ] S ⇒ aaBb [by B→Bb ]
S ⇒ aaλb ⇒ aab [by A→λ] S ⇒ aaλb ⇒ aab [by B→λ]
Derivation of Grammar
The set of all strings that can be derived from a Grammar is said to be the Language generated from the grammar.
Example:
Consider the grammar G1
V = {S,A}
T = {a,b}
S=S
Production Rule:
S → aAb
aA →aaAb
A→λ
Solution:
S ⇒ aAb [by S → aAb]
S ⇒ aaAbb [by aA →aaAb]
S ⇒ aaaAbbb [by aA →aaAb]
S ⇒ aaa λ bbb ⇒ aaabbb [by A → λ]
Context Free Languages (CFL)
In Formal language theory, a context free language is a language generated by some Context Free
Grammar.
The set of all CFL is identical to the set of languages accepted by Pushdown Automata.
CFL is represented by 4 tuples G → <V, ∑, P, S>
∑ → Input Symbol
V → Finite Non-Empty Set of Non-Terminal Symbols. (In Capital letters) (A,S,B)
T → Finite Set of Terminal Symbols. (In small letters) (a to z, 0 to 9, λ)
P → Finite Non-Empty Set of Production Rules.
S → Start Symbol (Symbol from where we start producing our sentences or strings).
CFL has production Rule of the form
A→a
Where, a {V ∪ ∑}* and A ∈ V
Example:
Equal No. of a’s and equal no. of b’s
CFG will be defined as
V = {S,A}
T = {a,b}
Production Rule:
S → aAb
A →aAb│ λ
Solution:
S ⇒ aAb [by S → aAb]
S ⇒ aaAbb [by A →aAb]
S ⇒ aaaAbbb [by A →aAb]
S ⇒ aaa λ bbb ⇒ aaabbb [by A → λ]
Note
One Grammar is of one to more languages
One Language can have more than one grammar
02 Hafiz Khizar
20021519-003
Derivation Tree/ Parse Tree
A Derivation or Parse Tree is an ordered rooted tree that graphically represents the semantic
information of strings derived from a Context Free Grammar.
Example:
For the Grammar G = {V,T,P,S}, where S →0B, A →1AA│ λ , B → 0AA
S
0 B
λ λ
Chomsky Normal Form (CNF)
A Grammer is said to be in CNF:
If each terminal produce exactly one non terminal
If each terminal produce exactly two terrminals
Y -> y
A -> a
B -> b
Step 4: Reduce the length of each non terminals utp 2 and add new
rules
S -> XR | BY | y | AX | XA | a
R -> AX
X- > XR | XT | AX | XA | a | BX | XB | b
T -> BX
Y -> AB
Y -> y
A -> a
B -> b
Example for u:
S -> AB | λ Reminder!
A -> a | λ SEOCET
(Small equls to one , Capital equals to two)
B -> BC | b
C -> c
S → AB | B | A | λ
A→a|λ
B → BD | b
D→C
C→c
03 Usama Ijaz
20021519-031
Greibach Normal Form (GNF).
As we know, we represent Context-Free-Grammar in two ways.
i) CNF
ii) GNF
Examples:
1) A a
2) A aB
3) A aABCb
CFG TO GNF
Let say we have a CFG
S abSb | aa
As, we know GNF must be in the form of Now, we will convert it into a GNF.
STEP 1.
we will introduce a variable corresponding to all terminals.
Bb
A a
Now, we will subtitude these variables into the CFG.
STEP 2.
S aBSB || aA
CNF TO GNF
Let say we have a CNF Grammar:
S AB | BC
A a
Bb
C AB | a
as we can see its not following the rules of GNF. GNF rules are:
A -> aA | a Or
Non-Terminal (Terminal)
B -> bBc | BC | d
Answer:
S → aSb | Sb | aS | S | AB | aA | ABb | Ab | A | aB | Bb | B | λ
A → aA | a
B → Bc | BC | bBc | b | d
C→c
Thank
you