0% found this document useful (0 votes)
14 views62 pages

Screenshot 2024-07-22 at 8.41.47 AM

Uploaded by

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

Screenshot 2024-07-22 at 8.41.47 AM

Uploaded by

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

Chapter 4

Context-Free Grammars
Part 1
Context-Free Grammars and Languages
Languages Hierarchy
Recursively Enumerable Languages

Context Sensitive Languages


TM
Context Free Languages
CFG / PDA
Regular Languages
RE / FA
Understand the Languages
Languages Automaton Example of languages Characteristics

In set notation
{ambn | m, n ≥ 0}
Finite No dependency in the
Regular
automata
In regular expression languages
a*b*
(a + b)*
{anbn | n ≥ 0}
{ambn | m = n and m, n ≥ 0} There is dependency
Pushdown (not more than two
Context-free {ambncm+n | m, n > 0}
automata parts of dependencies in
{ambmcndn | m ³ 0, n > 0} the language)
{am+1b*c2m | m >= 0}
Context- There is dependency
{anbncn | n ≥ 0}
sensitive / Turing (more than two parts of
Recursively machine
{am+1bmcn | m = n and m, n >= 0} dependencies in the
enumerable {ww | w ∊ {a, b}*} language)
3
What does dependency means?
• {anbn | n ≥ 0}
• {ambnc2m | m, n > 0}
• {ambmcndn | m ³ 0, n > 0}
• {ambncpdpenfm | m, n, p ³ 0}
• {anbncn | n ≥ 0}
• {anbncndn | n ≥ 0}
• {ambmcndnen | m ³ 0, n > 0}
Theory of Computer Science 4
How to recognize the languages
• Given a set of languages. How to recognize either
the language is a Regular or non-Regular language?

L1 = (a + b)* L2 = (aa*(ab + a)*)

L3 = {amb2mcn | m, n > 0} L4 = {ambn | m, n > 0}

S ® aA S ® aSbb | A
A ® aA | bA | b A ® cA | c
Theory of Computer Science 5
1. a*b*
2. {anbn | n ≥ 0}
3. {am+1bmc2n | m, n >= 0}
4. {amban |m, n ³ 1}
5. {am+1b*cm | m >= 0}
6. {am+1bmcm | m >= 0}
7. {ambn | m = n and m, n ≥ 0}
8. {am+1bnc* | m = n and m, n > 0}
9. {anbmcn+m | n, m > 0}
10. {ambncp | m, n, p ³ 0}
11. {am+1bmcn | m = n and m, n >= 0}
12. {ambncpdq | m = n + p + q; m, n, p, q ³ 0}
13. {am+nbncm | n, m ³ 0}
14. {anb2ncm | m = nTheory
andofn,Computer
m > 0} Science 7
Languages Hierarchy
Recursively Enumerable Languages

Context Sensitive Languages


TM
Context Free Languages
CFG / PDA
Regular Languages
RE / FA
Formal Definition of
Context-Free Grammars (CFG)
A CFG can be formally defined by a quadruple
of (V, S, P, S) where:
We can give a formal description to a CFG
– V is a finite set of variables (non-terminal)
by specifying each of its 4 components, e.g.:
– å (the alphabet) is a finite set of terminal
G = ({S, A}, {0, 1}, P, S)
symbols , where V Ç S = f
where P consists of three rules:
– P is a finite set of rules (production rules) written
as: A ® a for S A→Î0S1 V, a Î (v È S)*.
S→
– S is the start symbol, S ÎAV
A→l
9
Formal Definition of
Context-Free Grammars (CFG)
A CFG can be formally defined by a quadruple
of (V, S, P, S) where:
– V is a finite set of variables (non-terminal)
– å (the alphabet) is a finite set of terminal
symbols , where V Ç S = f
– P is a finite set of rules (production rules) written
as: A ® a for A Î V, a Î (v È S)*.
– S is the start symbol, S Î V

10
Formal Definition of CFG
• We can give a formal description to a
particular CFG by specifying each of its
four components, for example,
G = ({S, A}, {0, 1}, P, S) where P
consists of three rules:
S → 0S1 Can write as:
S → 0S1
S→A S → A OR
A→l S → 0S1 | A

Theory of Computer Science 11


Terminal or Variable ?

S ® AB
A ® aA | l
B ® bB | l
• The terminal symbols are {a, b, l}
• The variable symbols are S, A and
B
Theory of Computer Science 12
Context-Free Grammars
• Grammars are used to generate
strings of a language.
• An A rule can be applied to the
variable A whenever and wherever
it occurs.
• No limitation on applicability of a
rule – it is context free
13
Context-Free Grammars
• CFG have no restrictions on the right-hand side of
production rules. All of the following are valid CFG
production rules:
S ® aSbS S®c
S®λ S ® xyz
S ® ABcDEF S ® MZK
• Notice that there can be a λ, one or more terminals,
one or more non-terminals and any combination of
terminals and non-terminals on the right-hand side
of a production rule.
Theory of Computer Science 14
Generating Strings with CFG
• Generate a string by applying rules
– Start with the initial symbol
– Repeat:
• Pick any non-terminal in the string
• Replace that non-terminal with the right-hand side of
some rule that has that non-terminal as a left-hand
side
• Repeat until all elements in the string are terminals
E.g. : P: S ® AB
Note:
A ® aA | l
This → used in rule
B ® bB | l This ⇒ used in derivation
We can derived string ab as:
S ⇒ AB ⇒ aAB ⇒ alB ⇒ albB ⇒ albl ⇒ ab
15
Generating Strings with CFG
P: S ® aS S ® Bb
B ® cB B®l
• Generating a string:
S replace S with aS
aS replace S with Bb
aBb replace B with cB
acBb replace B with l
acb Final String
Theory of Computer Science 16
Generating Strings with CFG
P: S ® aS S ® Bb
B ® cB B®l
• Generating a string:
S replace S with aS
aS replace S with aS
aaS replace S with Bb
aaBb replace B with cB
aacBb replace B with cB
aaccBb replace B with l
aaccb Final String
Theory of Computer Science 17
Generating Strings with CFG

P: S ® aS S ® Bb
B ® cB B®l
• Regular expression equivalent to
this CFG: a∗c∗b
• Shortest string: S ⇒ Bb ⇒ lb.
Theory of Computer Science 18
How to convert RE to CFG

• General rules:
• a+b
• ab
• a*
Theory of Computer Science 19
How to convert RE to CFG

• General rules:
• a+b S®a|b
• ab S ® aA
A®b
• a* S ® aS | λ
Theory of Computer Science 20
Checkpoint 1 Question
• What is the language of this grammar ?
• S ® aA a(a + b)*b
A ® aA | bA | b
• S ® aA aa*bb*
A ® aA | bB
B ® bB | l
• S ® aS | bA a*bba*
A ® bB
B ® aB | l
21
Checkpoint 2 Question
• What is the grammar of this language ?
• b(a + b)*
• (a + b)*a
• (a + b)+

22
Derivation
• A derivation is a listing of how a string is generated –
showing what the string looks like after every
replacement.
S ® AB S ⇒ AB
A ® aA | l ⇒ aAB
B ® bB | l ⇒ aAbB
⇒ abB
⇒ abbB
⇒ abb

Theory of Computer Science 24


Derivation
• A terminal string is in the language of
the grammar if it can be derived from
the start symbol S using the rules of
the grammar:
• Example: S ® AASB | AAB
A®a
B ® bbb
25
Derivation
Derivation Rule Applied
S ⇒ AASB S ® AASB
⇒ AAAASBB S ® AASB
⇒ AAAAAASBBB S ® AASB
⇒ AAAAAAAABBBB S ® AAB
… …
… …
⇒ aaaaaaaaBBBB A®a
⇒ aaaaaaaabbbbbbbbbbbb B ® bbb
26
Derivation
• Let G be the grammar :
S ® aS | aA
A ® bA | l
• The derivation of aabb is as shown:
S Þ aS
Þ aaA
Þ aabA
Þ aabbA
Þ aabbl Þ aabb
27
Example
• Let G be the grammar :
S ® aSb | ab
• The derivation of aabb is as shown:
S Þ aSb
Þ aaSbb
Þ aaaSbbb
Þ aaaaSbbbb
Þ aaaabbbb
Þ Set notation equivalent to this CFG is
L = {anbn | n > 0}
28
CFG – Generating strings
• E.g. : Formal description of G = ({S}, {a, b}, P, S)
P: S ® aS | bS | l
• The following strings can be derived:
S⇒l
S ⇒ aS ⇒ al ⇒ a
S ⇒ bS ⇒ bl ⇒ b
S ⇒ aS ⇒ aaS ⇒ aal ⇒ aa
S ⇒ aS ⇒ abS ⇒ abl ⇒ ab
S ⇒ bS ⇒ baS ⇒ bal ⇒ ba
S ⇒ aS ⇒ abS ⇒ abbS ⇒ abbl ⇒ abb
29
CFG – Generating strings
• E.g. : G = ({S}, {a,b}, P, S)
P: S ® aS | bS | l
• The language above can also be
defined using regular expression:
L(G) = (a + b)*

30
CFG – Generating strings
• E.g. : G = ({S, A}, {a, b}, P, S)
P: S ® AA
A ® AAA | bA | Ab | a
• The following strings can be derived:
S ⇒ AA
S ⇒ aA rule [A ® a] Shortest
S ⇒ aAAA rule [A ® AAA] string ?
S ⇒ abAAA rule [A ® bA]
S ⇒ abaAA rule [A ® a]
S ⇒ abaAbA rule [A ® Ab]
S ⇒ abaabA rule [A ® a]
S ⇒ abaaba rule [A ® a]
31
CFG – Generating strings
G = (V, S, P, S), V = {S, A}, S = {a, b},
P: S ® AA
A ® AAA | bA | Ab | a
• Four distinct derivations of ababaa in G:
(a) S ⇒ AA (b) S ⇒ AA (c) S ⇒ AA (d) S ⇒ AA
⇒ aA ⇒ AAAA ⇒ Aa ⇒ aA
⇒ aAAA ⇒ aAAA ⇒ AAAa ⇒ aAAA
⇒ abAAA ⇒ abAAA ⇒ AAbAa ⇒ aAAa
⇒ abaAA ⇒ abaAA ⇒ AAbaa ⇒ abAAa
⇒ ababAA ⇒ ababAA ⇒ AbAbaa ⇒ abAbAa
⇒ ababaA ⇒ ababaA ⇒ Ababaa ⇒ ababAa
⇒ ababaa ⇒ ababaa ⇒ ababaa ⇒ ababaa
(a) & (b) leftmost, (c) rightmost, (d) arbitrary
32
Context-Free Grammars
S ⇒ aS ⇒ aSa ⇒ aba
• Sentencial forms are the strings derivable
from start symbol of the grammar.
• Sentences are forms that contain only
terminal symbols.
• A set of strings over S is context-free
language if there is a context-free grammar
that generates it.
33
Derivation Tree, DT
G = ({S, A}, {a, b}, P, S)
P: S ® AA
A ® AAA | bA | Ab | a

The derivation tree for


S ⇒ AA ⇒ aA ⇒ abA ⇒ abAb ⇒ abab
34
Derivation Tree (DT)
• A CFG G is ambiguous if there exist more than
ONE derivation tree (DT) for n, where n Î L(G).
• Example: G = ({S}, {a, b}, P, S)
P: S ® aS | Sa | b
The string aba can be derived as:
S ⇒ aS ⇒ aSa ⇒ aba OR
S ⇒ Sa ⇒ aSa ⇒ aba
35
Derivation Tree
S ⇒ aS ⇒ aSa ⇒ aba OR S ⇒ Sa ⇒ aSa ⇒ aba

36
End of Part 1

Chapter 4
Context-Free Grammars
Chapter 4
Context-Free Grammars
Part 2
Structures of the Language
Regular Grammars
Structures of the Language
Three structures of languages
1. Recursive Structure
2. Two-parts Structure
3. Nested Structure

Theory of Computer Science 39


Recursive Structure
L = {0n1n | n ³ 0}
These strings have recursive structure:
00000011111
0000011111
00001111
000111
0011
S ® 0S1| λ
01
λ
Two-parts Structure (1)
Allowed strings
L= {0n1n0m1m | n ³ 0, m ³ 0} 010011
00110011
These strings have two parts: 000111
L = L1L2
L1 = {0n1n | n ³ 0}
L2 = {0m1m | m ³ 0}
S ® AA
rules for L1: S1 ® 0S11| λ A ® 0A1 | λ
L2 is the same as L1
Two-parts Structure (2)
• L = {0n1n1m0m | n > 0, m ³ 0} Allowed strings
010011
00110011
These strings have two parts: 000111
L = L1L2
L1 = {0n1n | n > 0}
L2 = {1m0m | m ³ 0}

rules for L1 : S1 ® 0S11| λ S ® AB


rules for L2 : S2 ® 1S20| λ A ® 0A1 | 01
B ® 1B0 | λ
Nested structure Allowed strings
011001
L = {0n1m0m1n | n ³ 0, m ³ 0} 0011
1100
00110011

These strings have nested structure:

outer part: 0n1n S ® 0S1|A


inner part: 1m0m A ® 1A0 | λ
Example

• Let G be the grammar given by


the production
S ® aSa | aBa
B ® bB | b
• Then L(G) = {anbman | n > 0, m > 0}
45
Example
• Let L(G) = {anbmcmd2n | n≥0, m>0}
• Then the production rules for this
grammar is:
S ® aSdd | A
A ® bAc | bc
46
Example
• Let L(G) = {ambncm+n | n ³ 0, m > 0}
• Step1: Expand to {ambncmcn | n ³ 0, m > 0}.
• Step2: Formalize the structure (re-arrange the
same input alphabets {ambncncm | n ³ 0, m >
0}.
• The production rules for this grammar is:
S ® aSc | aAc
A ® bAc | λ

47
Example

• A string w is a palindrome if w = wR
• The set of palindrome over {a, b}
can be derived using rules:
S®a|b|l
S ® aSa | bSb
48
Example of CFG

• G = ({S, A, B, C}, {a, b}, P, S)


P: S ® aSbb | abb
• L(G) = {anb2n |n ³ 1}

Sept2011 Theory of Computer Science 49


Regular Grammars
• Regular grammars are an important
subclass of context-free grammars that
play an important role in the lexical
analysis and parsing of programming
languages.
• Regular grammars is a subset of CFG.
• Regular grammars are obtained by
placing restrictions on the form of the
right hand side of the rules.
51
Definition
• A regular grammar is a CFG in which each rule
has one of the following form:
1. A ® a
2. A ® aB
3. A ® λ
where A, B Î V, and a Î S, A, B is a non-
terminal and a can be one or more terminals
52
Regular Grammars
• There is at most ONE variable in a
sentential form – the rightmost
symbol in the string.
• Each rule application adds ONE
terminal to the derived string.
Derivation is terminated by rules:
A ® a OR A®λ
53
Example
• Consider the grammar:
G: S ® abSA | l
A ® Aa | l
• The equivalent regular grammar:
Gr : S ® aB | l
B ® bS | bA
A ® aA | l
54
Example of RG

• G = ({S, A, B, C}, {a, b}, P, S)


P: S ® aS | aB
B ® bC
C ® aC | a
• L(G) = {amban |m, n ³ 1}
Theory of Computer Science 55
How to design CFG for
A. {ambncndm | m ³ 0, n > 0}.
B. {ambmcndn | m ³ 0, n > 0}.
C. {ambnc2m | m ³ 0, n > 0}.
D. {ambnc2n | m ³ 0, n > 0}.
E. {am+nbncm | n, m > 0}.
F. {ambncm+n | n, m ³ 0}.
G. {anbmc2n+m | m ³ 0, n > 0}.
H. {ambncn+2m | m ³ 0, n > 0}.
How to construct RG from a RE
• Use a new non-terminal for every new
character
• Each loop state turns into a recursive
definition on a non-terminal
• Example: R.E. ab*ab
RG S ® aA
A ® bA
A ® aB
B®b
Theory of Computer Science 57
Reg Expression to Reg Grammar
• RE a(a + b)*b RG S ® aA
A ® aA
A ® bA
A®b

• RE ab*a RG S ® aA
A ® bA
A®a
Theory of Computer Science 58
RE ⇒ RG

• Consider the regular expression:


a+b*
• The regular grammar is:
S ® aS ç aR
R ® bR ç l

59
How to construct RG from an FA
• Use a new non-terminal for every new
character
• Each loop state turns into a recursive
definition on a non-terminal
• Example: F.A. =
Regular Grammar = S -> aA
A -> bA
A -> a
Sept2011 Theory of Computer Science 60
How to convert FA to RG
A ® aB | l
B ® aB | bC
C ® aB | bC | l.

• We could also add the productions A ® bD; D ® aD


| bD, but they are not necessary, because no string
of terminals can be obtained from a sequence of
steps in which the variable D appears.

Sept2011 Theory of Computer Science 61


RE vs CFG
• Every RE can be represented by CFG
but not every CFG can be
represented as RE.
• RL is a subset of CFL, thus every RL is
automatically a CFL but not every
CFL is a RL.
Equivalent RG and RE for each CFG
CFG RG RE
S ® aBa S ® aB
ab*a
B ® bB | l B ® bB | a
S ® AB
S ® aS | A
A ® aA | l a*b*
A ® bA | l
B ® bB | l
S ® abS | l OR
S ® aA
S ® AS | l (ab)*
A ® bS | l
A ® ab
S ® Aa
S ® aS | bS | a (a + b)*a
A ® aA | bA | λ
S ® aS | bS | aA
S ® AaaA
A ® aB (a + b)*bb(a + b)*
A ® aA | bA | l
B ® aB | bB | λ
Theory of Computer Science 63
Short Questions
• Consider the following regular grammar:
A ® a | b | aB | bA
B ® b | bA
• Which one of the following strings is NOT an
element of the language of this grammar?
a) abab c) baab
b) baba d) babb
Theory of Computer Science 64
Short Questions
• Consider the following grammar
G = (V, S, P, S), V = {S}, S = {a, b},
P : S → aS | aA;
A → bA | b;
• Which of the following RE denotes L(G)?
a) (a + b)* c) aa*bb*
b) a(ab)*b d) a*b*
Theory of Computer Science 65
End of Part 2

Chapter 4
Context-Free Grammars

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