Professional Documents
Culture Documents
Chapter4 - New Context Free Grammar (CFG) (1)
Chapter4 - New Context Free Grammar (CFG) (1)
Chapter4 - New Context Free Grammar (CFG) (1)
Context-Free Grammars
3
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}, {a, b}, P, S) where P
consists of three rules:
S → aSb
S→A
A→
Suriati2022 Theory of Computer Science 4
Context-Free Grammars
• Terminal symbols – elements of the
alphabet
• Variables or non-terminals – additional
symbols used in production rules
• Variable S (start symbol) initiates the
process of generating acceptable
strings.
5
Terminal or Variable ?
• S → (S) | S + S | S × S | A
•A→a|b|c
11
Generating Strings with CFG
P: S aS S Bb
B cB B
• Generating a string:
S replace S with aS
aS replace S with Bb
aBb replace B with cB
acBb replace B with
acb Final String
Theory of Computer Science 12
Generating Strings with CFG
P: S aS S Bb
B cB B
• 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
aaccb Final String
Theory of Computer Science 13
Generating Strings with CFG
P: S aS S Bb
B cB B
• Regular expression equivalent to
this CFG: a c b
• Shortest string: S Bb b.
Theory of Computer Science 14
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 | aAB
B bB | aAbB
abB
abbB
abb
17
Derivation
• Let G be the grammar :
S aS | aA
A bA |
• The derivation of aabb is as shown:
S aS
aaA
aabA
aabbA
aabb aabb
18
Example
• Let G be the grammar :
S aSb | ab
• The derivation of aaaabbbb is as shown:
S aSb
aaSbb
aaaSbbb
aaaabbbb
Set notation equivalent to this CFG is
L = {anbn | n > 0}
19
CFG – Generating strings
• E.g. : Formal description of G = ({S}, {a, b}, P, S)
P: S aS | bS |
• The following strings can be derived:
S
S aS a a
S bS b b
S aS aaS aa aa
S aS abS ab ab
S bS baS ba ba
S aS abS abbS abb abb
20
CFG – Generating strings
• E.g. : G = ({S}, {a,b}, P, S)
P: S aS | bS |
• The language above can also be
defined using regular expression:
L(G) = (a + b)*
21
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]
22
CFG – Generating strings
G = (V, , P, S), V = {S, A}, = {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
23
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 is context-free
language if there is a context-free grammar
that generates it.
24
exercise
• G = ({S, A}, {a, b}, P, S)
P: S AA
A AAA | bA | Ab | a
• Show the derivation of baba (in
sentencial form)
27
Derivation Tree
35
Design example
L = {0n1n | n 0}
These strings have recursive structure:
000000111111
0000011111
00001111
000111
0011
S 0S1| λ
01
λ
Design examples
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
Design examples Allowed strings
011001
L = {0n1m0m1n | n 0, m 0} 0011
1100
00110011
• L(G) =
{a b a |
n m n n > 0, m > 0}
• Let G be the grammar given by
the production
S aSa | aBa
B bB | b
39
Example of CFG
41
Example
• Consider the grammar:
S abScB |
B bB | b
42
exercise
For each of the following context free grammar, use set
notation to define the language generated by the
grammar.
– S → aaSB | λ (aa)mbn
B → bB | b
– S → aSbb | A ancmb2n
A → cA | c
– S → abSdc | A (ab)n(cd)m(ba)m(dc)n
A → cdAba | λ
Theory of Computer Science 43
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.
44
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 , A, B is a non-
terminal and a can be one or more terminals
45
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λ
46
Regular Grammar
• The language L = a+b*
can also be defined as:
L = ( V, , P, S)
where: V = {S, A, B}
= {a, b}
P: S aS | aB
B bB |
47
Non-Regular Language
• The language L = a+b*
can also be defined as:
L = ( V, , P, S)
where: V = {S, A, B}
= {a, b}
P: S AB non-regular
A aA | a grammar
B bB |
48
Context Free Grammar (CFG) vs
Regular Grammar (RG) vs non Regular
Grammar (nRG),
CFG
nRG
RG
Step 2 a* b bb* S bA
A
If a* = , sbA
Step 3 A = b b* A bB
B
Step 4 B = b* BbB
Step 5 If b* = , B= B 55
exercise
• What is the RG of the following RE ?
• ab*a
S → aA;A → bA;A → a
• ab*ab
S → aA;A → bA;A → aB;B → b
63
Example of RG
• SaS|aB
• BbB|b