0% found this document useful (0 votes)
15 views37 pages

Unit 3: by Kamini Sharma

Cfg notes automata

Uploaded by

jakisa9382
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)
15 views37 pages

Unit 3: by Kamini Sharma

Cfg notes automata

Uploaded by

jakisa9382
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/ 37

Unit 3

By kamini sharma
Classification of Context Free Grammars

• Context Free Grammars (CFG) can be classified on the basis of


following two properties:
• 1) Based on number of strings it generates.
• If CFG is generating finite number of strings, then CFG is Non-
Recursive (or the grammar is said to be Non-recursive grammar)
• If CFG can generate infinite number of strings then the grammar
is said to be Recursive grammar
• During Compilation, the parser uses the grammar of the language
to make a parse tree(or derivation tree) out of the source code.
The grammar used must be unambiguous. An ambiguous
grammar must not be used for parsing.
• 2) Based on number of derivation trees.
• If there is only 1 derivation tree then the CFG
is unambiguous.
• If there are more than 1 left most derivation
tree or right most derivation or parse tree ,
then the CFG is ambiguous.
Examples of Recursive and Non-Recursive Grammars

• Recursive Grammars
• 1) S->SaS
• S->b
• The language(set of strings) generated by the
above grammar is :{b, bab, babab,…}, which is
infinite.
• 2) S-> Aa
• A->Ab|c
• The language generated by the above
grammar is :{ca, cba, cbba …}, which is
infinite.
• Note: A recursive context-free grammar that
contains no useless rules necessarily produces
an infinite language.
• Non-Recursive Grammars
• S->Aa
• A->b|c
• The language generated by the above grammar is :{ba, ca},
which is finite.
• Types of Recursive Grammars
Based on the nature of the recursion in a recursive grammar, a
recursive CFG can be again divided into the following:
• Left Recursive Grammar (having left Recursion)
• Right Recursive Grammar (having right Recursion)
• General Recursive Grammar(having general Recursion)
Ambiguous Grammar :

A context-free grammar is called ambiguous
grammar if there exists more than one
derivation tree or parse tree.
• Example –
• S -> S + S / S * S / S / a
• G : S  AB
• A  aAa|b
• B bB|a
• W =abba
2. E  E + E|id
W: Id+Id+id
2. Unambiguous Grammar :

A context-free grammar is called unambiguous
grammar if there exists one and only one
derivation tree or parse tree.
• Example –
• X -> AB
• A -> Aa / a
• B -> b
Ambiguous Grammar Unambiguous Grammar

In ambiguous grammar, the leftmost and In unambiguous grammar, the leftmost


rightmost derivations are not same. and rightmost derivations are same.
Amount of non-terminals in ambiguous Amount of non-terminals in unambiguous
grammar is less than in unambiguous grammar is more than in ambiguous
grammar. grammar
Length of the parse tree in ambiguous Length of the parse tree in unambiguous
grammar is comparatively short. grammar is comparatively large.
Speed of derivation of a tree in Speed of derivation of a tree in
ambiguous grammar is faster than that of unambiguous grammar is slower than
unambiguous grammar. that of ambiguous grammar.
Ambiguous grammar generates more Unambiguous grammar generates only
than one parse tree. one parse tree.
• EE+E
• EE*E
• E  id
• Find String derivation?
Right Linear Regular Grammar

• In this type of regular grammar, all the non-


terminals on the right-hand side exist at the
rightmost place, i.e; right ends.
• Examples :
• A ⇢ a,
• A ⇢ aB,
• A⇢∈
• where, A and B are non-terminals, a is terminal,
and ∈ is empty string
• S ⇢ 00B | 11S
• B ⇢ 0B | 1B | 0 | 1
• where, S and B are non-terminals, and 0 and 1
are terminals
Left Linear Regular Grammar

• In this type of regular grammar, all the non-


terminals on the left-hand side exist at the
leftmost place, i.e; left ends.
• A ⇢ a, A ⇢ Ba, A ⇢ ∈ where, A and B are
non-terminals, a is terminal, and ∈ is empty
stringS ⇢ B00 | S11 B ⇢ B0 | B1 | 0 | 1 where
S and B are non-terminals, and 0 and 1 are
terminals
Left linear to Right Linear Regular Grammar

• In this type of conversion, we have to shift all the left-


handed non-terminals to right as shown in example
given below:
• Left linear A -> Ba , B -> ab
• Right linear
• A -> abaB ,B -> epsilon
• OR
• A -> abB B -> a
Right linear to Left Linear Regular Grammar

• In this type of conversion, we have to shift all


the right-handed non-terminals to left as
shown in example given below:
• Right linear A -> aB , B -> ab
• Left linear
• A -> Baba , B -> epsilon
• OR
• A -> Bab B -> a
Left most derivation
• Derivation means with help of grammar create
string. Or if we have language produce
grammar.
• S AB
• Aa aaA|null
• B  bB|null check weather
• W = aaaabb E L(G)
Left and right most derivation
Point-06:

Leftmost Derivation-
S → aS
→ aaS (Using S → aS)
→ aaaS (Using S → aS)
→ aaa∈
→ aaa

Rightmost Derivation-
S → aS
→ aaS (Using S → aS)
→ aaaS (Using S → aS)
→ aaa∈
→ aaa
Clearly,
Leftmost derivation = Rightmost derivation
Similar is the case for all other strings.
Pumping lemma for CFL Context free
language
• Lemma
• If L is a context-free language, there is a pumping
length p such that any string w ∈ L of length ≥ p can
be written as w = uvxyz, where
• vy ≠ ε,
• |vxy| ≤ p,
• and
• for all i ≥ 0, uvixyiz ∈ L.
• Show that L = {a^n b^n c^n| n>0} is not
context free.
Construction of reduced Grammar
Greibach Normal Form (GNF)

• GNF stands for Greibach normal form. A


CFG(context free grammar) is in GNF(Greibach
normal form) if all the production rules satisfy one
of the following conditions:
• A start symbol generating ε. For example, S → ε.
• A non-terminal generating a terminal. For example,
A → a.
• A non-terminal generating a terminal which is
followed by any number of non-terminals. For
example, S → aASB.
• G1 = {S → aAB | aB, A → aA| a, B → bB | b}
• G2 = {S → aAB | aB, A → aA | ε, B → bB | ε}
• The production rules of Grammar G1 satisfy the
rules specified for GNF, so the grammar G1 is in GNF.
However, the production rule of Grammar G2 does
not satisfy the rules specified for GNF as A → ε and
B → ε contains ε(only start symbol can generate ε).
So the grammar G2 is not in GNF.

You might also like