0% found this document useful (0 votes)
24 views31 pages

Grammar

Uploaded by

ahmedch82281
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views31 pages

Grammar

Uploaded by

ahmedch82281
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Theory of

Automata
GRAMMER

Presented to
Dr. Saliah Zahoor
Group Members

Hafiz Khizar Usama Ijaz Maham Rani


20021519-003 20021519-031 20021519-125
01 Maham Rani
20021519-125
Basics of Grammar

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.

Grammar will be for both type of languages:


i. Regular
ii. Non-Regular
Example-1 :
Consider Grammar G1 = <V, T, P, S>

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

0 A Root vertex Must be labelled by the start Symbol


A
Vertex Labelled by Non- Terminal Symbols

Leaves Labelled by Terminal Symbols or λ


1 A A λ

λ λ
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

S E O C E T (Small equls to one , Capital equals to two)


S -> RB
Y -> b
R -> AX
A -> a
X -> m
Example
S -> XaX | bY |Y
X -> XaX | XbX |
Y -> ab

Step 1 remove null production:


S -> XaX | bY | Y | aX | Xa | a
X- > XaX | XbX | aX | Xa | a | bX | Xb | b
Y -> ab
Step 2: Remove unit production (captiol into small+add new rule)
S -> XaX | bY | y | aX | Xa | a
X- > XaX | XbX | aX | Xa | a | bX | Xb | b
Y -> ab
Y -> y
Step 3 : Convery all mix terms into Captials (solid non- terminals)
S -> XAX | BY | y | AX | XA | a
X- > XAX | XBX | AX | XA | a | BX | XB | b If small are more than one ,
Y -> AB convert it into capital

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

If all the productions are in the form of .


Where ‘V’ is the variables and ‘a’ is the terminal.

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.
Bb
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
Bb
C  AB | a
as we can see its not following the rules of GNF. GNF rules are:

So according to this rule:


S  AB | BC (Not in GNF )
A a (In GNF)
Bb (In GNF)
C  AB | a (‘AB’ is not in GNF but ‘a’ is in GNF)
We will apply some substitution here:
substitude A  a into S  AB | BC so it’ll become
S  aB | BC
again , we will subsitude B  b into this
S  aB | bc

So the new form is :


S  aB | bc
A a
B
C  AB | a
But C  AB is not in GNF so we will substitude A  a into this
C  aB | a
So now our CNF is completely converted into GNF.
S  aB | bc
A a
Bb
C  aB | a
CONVERSION
Example:
S -> aAB | λ
A -> b | aA
B -> BC | b
C -> c
Step 1: Eliminate ε-productions (productions that derive the empty string)
S -> aAB | AB | aA | aB | a | B | A | λ
A -> b | RA
R -> a
B -> BC | b
C -> c

Step 2: Eliminate unit productions (productions of the form A → B).

No unit productions exist in the updated grammar after handling ε-productions.


Step 3: Eliminate terminals from right-hand sides of productions with more
than one
S -> aAB | AB | aA | aB | a | B | A
A -> B | aA | aB | a | b
B -> BC | b
C -> c

Step 4: Convert remaining productions into Greibach Normal For


S -> aAB | AB | aA | aB | a | B | A
A -> B | aA | aB | a | b
B -> BC | b
C -> c
Example for u
S -> aSb | AB | λ Reminder!!
Non-Terminal (Terminal)(non-terminal) (non-terminal)

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

You might also like