0% found this document useful (0 votes)
28 views38 pages

Theory of Computation: Madhav Institute of Technology and Science

Uploaded by

jyash1293
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)
28 views38 pages

Theory of Computation: Madhav Institute of Technology and Science

Uploaded by

jyash1293
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/ 38

MADHAV INSTITUTE OF TECHNOLOGY AND SCIENCE

Deemed to be university
NAAC Accredited with A++ grade

THEORY OF
COMPUTATION

Submitted by:
Mani Verma(0901MC221047)
Prachi Gupta(0901MC221053) Submitted to:
Pranshoo Patel(0901MC221054) Prof. Akanchha Tiwari
Sameeksha Kushwaha(0901MC221062)
Yash Jain(0901MC221078)
 Introduction
 Grammar
 Regular grammar
 Derivation & Ambiguity
 Context free grammar
 CNF & GNF
GRAMMAR
4

GRAMMAR
• It is a finite set of formal rules for generating syntactically correct sentences or
meaningful correct sentences.
Grammar can be represented by 4 tuples – <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 (Symbol from where we start producing our sentences or
strings).
5

CLASSIFICATION OF
GRAMMAR
• Type ‘0’ Grammar
• Type ‘1’ Grammar
• Type ‘2’ Grammar
• Type ‘3’ Grammar
6

TYPE ‘0’ GRAMMAR


• Type-0 grammars generate recursively enumerable.
• In type-0 the production has no restrictions.
• There may be any phase structure grammar which includes all formal
grammars
• They generate the language which is recognized by the Turing
machine.
• The productions can be in the form of a->b where, a is a string of
terminals and with at least one non-terminals and a cannot be null. b is
a string of terminal and non-terminal.
7

TYPE ‘1’ GRAMMAR


• Type 1 grammar is also known as context sensitive grammar
• The context sensitive grammar is used to represent context sensitive
language.
• The context sensitive grammar may have more than one symbol on the
left hand side of their production rules.
• The number of symbols on the left-hand side must not exceed the
number of symbols on the right-hand side.
8

TYPE ‘2’ GRAMMAR


• Type 2 grammars are generated by context free languages.
• The language that is generated by the grammar is recognized by Push
Down Automata.
• Type 2 must be in Type 1.
• Left-hand side of production can have only one variable.
9

TYPE ‘3’ GRAMMAR


• Type 3 grammars are generated by regular languages.
• These languages are exactly all those languages that can be accepted
by finite state automata.
• Type 3 is the most restricted grammar.
• It is in the form of

• V->VT*/T* Or V->T*V/T*
REGULAR GRAMMER
11
WHAT IS REGULAR GRAMMAR?
A regular grammar is a mathematical object, G, with four
components, G = (Ν, Σ, Ρ, S),
Where
N = nonempty, finite set of nonterminal symbols,
∑ = finite set of terminal symbols, or alphabet, symbols,
P = set of grammar rules, each of one having one of the forms
A->aB
A->a
A->E, for Α, Β € Ν, Α € Σ, and & the empty string, and S€N is the
start symbol.
12

• Regular Set: Any set represented by regular


expression is called Regular Set.

• Regular Language: The class of Regular sets over ∑


is Regular Language over the terminal set ∑.

• Regular Grammar: Grammar for Regular


Expression is Regular Grammar.
13

EXAMPLE:

Let's consider a simple example of a regular grammar that generates


strings over the alphabet {0, 1} with the following rules:
Start symbol: S
Production rules:
• S→0S
• S→1A
• A→0A
• A→1A
• A→ε
14
SOLUTION:
1.Starting with the start symbol �S:
• �⇒0�S⇒0S (using the rule �→0�S→0S)
• �⇒00�S⇒00S (applying �→0�S→0S again)
• �⇒001�S⇒001A (applying �→1�S→1A)

This string is generated: 001001

2. Starting again with the start symbol �S:


• �⇒1�S⇒1A (using the rule �→1�S→1A)
• �⇒10�S⇒10A (applying �→0�A→0A)
• �⇒101�S⇒101A (applying �→1�A→1A)
• �⇒1010�S⇒1010A (applying �→0�A→0A)
• �⇒10100�S⇒10100A (applying �→0�A→0A)
• �⇒101001�S⇒101001A (applying �→1�A→1A)

This string is generated: 101001101001


PROPERTIES 15

1. Closure:
If r1 and r2 are regular expressions(RE), then
r1* is a RE
r1+r2 is a RE
r1.r2 is a RE
2. Associativity –
If r1, r2, r3 are RE, then
i.) r1+ (r2+r3) = (r1+r2) +r3
ii.) r1.(r2.r3) = (r1.r2).r3
3. Commutative property –
If r1, r2 are RE, then 16

• r1+r2 = r2+r1. For example, for r1 =a and r2 =b, then RE a+ b and b+ a


are equal.
• r1.r2 ≠ r2.r1. For example, for r1 = a and r2 = b, then RE a.b is not
equal to b.a.
4. Distributed property –
If r1, r2, r3 are regular expressions, then
• (r1+r2).r3 = r1.r3 + r2.r3 i.e. Right distribution
• r1.(r2+ r3) = r1.r2 + r1.r3 i.e. left distribution
• (r1.r2) +r3 ≠ (r1+r3)(r2+r3)
5. Idempotent law –
• r1 + r1 = r1 ⇒ r1 ∪ r1 = r1 , therefore the union operator satisfies
idempotent property.
• r.r ≠ r ⇒ concatenation operator does not satisfy idempotent property.
IDENTITIES FOR REGULAR EXPRESSION – 17

There are many identities for the regular expression. Let p, q and r are regular
expressions.
• ∅+r=r
• ∅.r= r.∅ = ∅
• ∈.r = r.∈ =r
• ∈* = ∈ and ∅* = ∈
• r+r=r
• r*.r* = r*
• r.r* = r*.r = r+.
• (r*)* = r*
• ∈ +r.r* = r* = ∈ + r.r*
• (p.q)*.p = p.(q.p)*
• (p + q)* = (p*.q*)* = (p* + q*)*
• (p+ q).r= p.r+ q.r and r.(p+q) = r.p + r.q
DERIVATION &
AMBIGUITY
19

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:
 We have to decide the non-terminal which is to be replaced.
 We have to decide the production rule by which the non-terminal will be replaced.

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.

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.
20
21

DERIVATION TREES:-

Derivation tree are a graphical representation for the derivation of the


given production rule.
Following are the properties of derivation tree:-
 The root node is always a node indicating start symbol.
 The derivation isread from left to right.
 The left node are always terminal nodes.
 The interior node are always the non-terminal nodes.
22
23
AMBIGUITY:-
The grammer can be derived in either left (LMD) or RMD the parse or
derivation tree has to be unique even though the derivation is in LM or
RM but if their exist more than one parse tree for a given grammer
that means their could be more than one left most and RMD possible
and then grammer is said to be ambiguous grammer.
CONTEXT FREE
GRAMMAR
Context Free Grammar
A Context-Free Grammar (CFG) is a formal system used to describe the
syntax or structure of languages in the field of formal language theory. It
consists of a set of rules that define how strings of symbols in a language can
be generated. These rules specify the relationships between different
components of the language, allowing for the creation of valid sentences or
expressions. CFG has 4 tuples.

 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 (Symbol from where we start producing our sentences or strings).
Example : For the grammar given below, find out the context free language. The grammar G = ({S}, 26
{a}, S, P) with the productions are;
S → SS (Rule: 1)
S→a (Rule: 2)

Solution: First compute some strings generated by the production rules of the grammar G in the above;
(i) S⇒a (Rule: 2)
(ii) S ⇒ SS (Rule: 1)
⇒ aS, (Rule: 2)
⇒ aa, (Rule: 2)
i.e. ⇒ aa ⇒ a2
(iii) S ⇒ SS (Rule: 1)
⇒ aS (Rule: 2)
⇒ aSS (Rule: 1)
⇒ aaS (Rule: 2)
⇒ aaa (Rule: 2)
i.e. ⇒ aaa ⇒ a3

Hence; Language generated by the above grammar L(G) = {a, a2, a3, a4, a5, .. .. .. .. }
27
Simplification of Context Free Grammar
 Removing Useless Symbols:
o Identify non-terminals and terminals that cannot generate any terminal strings.
o Remove these useless symbols along with any production rules involving them.

 Eliminating ε-Productions:
o An ε-production is a production rule where a non-terminal can derive the empty string (ε).
o Replace each ε-production with equivalent productions that do not include ε.
o Update other production rules to account for the possibility of deriving ε.

 Eliminating Unit Productions:


o A unit production is a production rule where a non-terminal directly produces another non
terminal.
o Replace each unit production A→B with the set of production rules A→α where α is any string of
terminals and non-terminals derivable from B.
Applications of Context Free Grammar
 Programming Languages: CFGs are widely used in the design and implementation of programming
languages. They serve as the basis for defining the syntax of programming languages, specifying how
programs should be structured and written. Compilers and interpreters for programming languages use
CFGs to analyze and parse source code, ensuring that it conforms to the language's syntax rules.

 Compiler Design: In the process of compiling source code into machine code or intermediate
representations, compilers employ CFGs to perform syntax analysis or parsing. This involves
breaking down the source code into its constituent parts according to the rules defined by the CFG.
Syntax analysis is a crucial phase of compilation, as it checks the syntactic correctness of the code
before further processing.

 Natural Language Processing (NLP): CFGs play a significant role in NLP for modeling the syntax
of natural languages. They are used to develop grammars for formalizing the structure of sentences in
a language, enabling tasks such as parsing, syntactic analysis, and generation of natural language text.
CFG-based parsers can identify the syntactic structure of sentences and extract meaningful
information from them.
NORMAL FORM:
CNF AND GNF
NORMAL FORM
NORMAL FORM/Simplification of grammar

The grammar can be simply by minimizing the ε production, removing


useless symbols, unit production. There is also a need to have a
grammar in some specific form that mean there should be fixed number
of terminal and non-terminal in the context free grammar(CFG ).

There are two import ant normal forms


a. Chomsky Normal form (CNF)
b. Greibach Normal form (GNF)
CHOMSKY NORMAL FORM (CNF)

Chomsky’s Normal Form Stands as CNF.


A context free grammar is in CNF, if the production rules satisfy one of the following
conditions
• If there is start Symbol generating ε. Example − A-> ε
• If a non-terminal generates two non-terminals. Example− S->AB
• If a non-terminal generates a terminal. Example − S->a
Example
Let's take G1 Production rules, as given below −
G1={ S->AB,
S->c,
A->a,
B->b}
G1 satisfies the rule specified for CNF. So, it is in CNF.
CONVERSION OF CFG TO CNF
 Step 1. Eliminate start symbol from RHS.
If start symbol S is at the RHS of any production in the grammar, create a new
production as:
S0->S
where S0 is the new start symbol.
 Step 2. Eliminate null, unit and useless productions.
If CFG contains null, unit or useless production rules, eliminate them.
 Step 3. Eliminate terminals from RHS if they exist with other terminals or non-
terminals. e.g,; production rule X->xY can be decomposed as:
X->ZY
Z->x
 Step 4. Eliminate RHS with more than two non-terminals.
e.g,; production rule X->XYZ can be decomposed as:
X->PZ
P->XY
EXAMPLE:
CONVERT THE GIVEN CFG TO CNF. CONSIDER THE GIVEN GRAMMAR
G1: S → a | aA | B , A → aBB | ε , B → Aa | b
Solution:
• Step 1: We will create a new production S1 → S, as the start symbol S appears on the RHS. The grammar will
be:
S1 → S , S → a | aA | B , A → aBB | ε , B → Aa | b
• Step 2: As grammar G1 contains A → ε null production, its removal from the grammar yields:
S1 → S , S → a | aA | B , A → aBB , B → Aa | b | a
• Now, as grammar G1 contains Unit production S → B, its removal yield:
S1 → S , S → a | aA | Aa | b , A → aBB , B → Aa | b | a
• Also remove the unit production S1 → S, its removal from the grammar yields:
S0 → a | aA | Aa | b , S → a | aA | Aa | b , A → aBB , B → Aa | b | a
• Step 3: In the production rule S0 → aA | Aa, S → aA | Aa, A → aBB and B → Aa, terminal a exists on RHS
with non-terminals. So we will replace terminal a with X:
S0 → a | XA | AX | b , S → a | XA | AX | b , A → XBB , B → AX | b | a , X→a
• Step 4: In the production rule A → XBB, RHS has more than two symbols, removing it from grammar yield:
S0 → a | XA | AX | b S → a | XA | AX | b A → RB B → AX | b | a X → a R → XB
Hence, for the given grammar, this is the required CNF.
GREIBACH NORMAL FORM (GNF)

A CFG is in Greibach Normal Form if the Productions are in


the following forms −
• If there is start Symbol generating ε. Example − S-> ε
• If a non-terminal generates a terminal. Example − S->a
• If a non-terminal generates…...A → bD1…Dn
where A, D1,....,Dn are non-terminals and b is a terminal.
• Example
Let's take G1 Production rules, as given below −
G1={ S→ aAB | aB,
A → aA| a,
B → bB | b}
G1 satisfies the rule specified for CNF. So, it is in CNF.
CONVERSION OF CFG TO GNF
• Step 1: Convert the grammar into CNF.If the given grammar is not
in CNF, convert it into CNF. You can refer the following topic to
convert the CFG into CNF: Chomsky normal form
• Step 2: If the grammar exists left recursion, eliminate it. If the
context free grammar contains left recursion, eliminate it. You can
refer the following topic to eliminate left recursion: Left Recursion
• Step 3: In the grammar, convert the given production rule into GNF
form.If any production rule in the grammar is not in GNF form,
convert it.
EXAMPLE:
S → XB | AA , A → a | SA , B→b , X→a
Solution:
• As the given grammar G is already in CNF and there is no left recursion, so we can skip step 1 and step 2 and
directly go to step 3.
• The production rule A → SA is not in GNF, so we substitute S → XB | AA in the production rule A → SA as:
S → XB | AA , A → a | XBA | AAA , B → b , X → a
• The production rule S → XB and B → XBA is not in GNF, so we substitute X → a in the production rule S → XB
and B → XBA as:
S → aB | AA ,A → a | aBA | AAA ,B → b ,X → a
• Now we will remove left recursion (A → AAA), we get:
S → aB | AA ,A → aC | aBAC ,C → AAC | ε ,B → b ,X → a
• Now we will remove null production C → ε, we get:
S → aB | AA ,A → aC | aBAC | a | aBA ,C → AAC | AA ,B → b ,X → a
The production rule S → AA is not in GNF, so we substitute
A → aC | aBAC | a | aBA in production rule S → AA as:
S → aB | aCA | aBACA | aA | aBAA ,A → aC | aBAC | a | aBA ,
C → AAC ,C → aCA | aBACA | aA | aBAA ,B → b ,X → a
The production rule C → AAC is not in GNF, so we substitute A
→ aC | aBAC | a | aBA in production rule C → AAC as:
S → aB | aCA | aBACA | aA | aBAA ,A → aC | aBAC | a | aBA ,
C → aCAC | aBACAC | aAC | aBAAC ,
C → aCA | aBACA | aA | aBAA ,B → b ,X → a
Hence, this is the GNF form for the grammar G.
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy