Theory of Computation: Madhav Institute of Technology and Science
Theory of Computation: 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>
CLASSIFICATION OF
GRAMMAR
• Type ‘0’ Grammar
• Type ‘1’ Grammar
• Type ‘2’ Grammar
• Type ‘3’ Grammar
6
• 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
EXAMPLE:
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
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.
DERIVATION TREES:-
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 ε.
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