0% found this document useful (0 votes)
5 views20 pages

Lec 6 P2

The document provides an overview of context-free languages and context-free grammars. It defines what a context-free language and context-free grammar are. It also gives examples of context-free grammars that generate specific languages and shows how strings can be derived from a grammar through multiple derivation steps.

Uploaded by

ahmed.teka446
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)
5 views20 pages

Lec 6 P2

The document provides an overview of context-free languages and context-free grammars. It defines what a context-free language and context-free grammar are. It also gives examples of context-free grammars that generate specific languages and shows how strings can be derived from a grammar through multiple derivation steps.

Uploaded by

ahmed.teka446
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/ 20

Context-Free Languages

Lec 3 1
Context-Free Languages

{a b : n  0}
n n {wwR}

Regular Languages
a *b * (a  b)*

Lec 3 2
• Informally, a Context-Free Language (CFL) is a language
generated by a Context-Free Grammar (CFG).

• What is a CFG?

• Informally, a CFG is a set of rules for deriving (or


generating) strings (or sentences) in a language.

• Note: A grammar generates a string, whereas a machine


accepts a string
3
Context-Free Languages

Context-Free Pushdown
Grammars Automata

stack

automaton

Lec 3 4
Context-Free Grammars

Informally, a CFG consists of:


• A set of replacement rules, each having a Left-Hand
Side (LHS) and a Right-Hand Side (RHS).
• Two types of symbols; variables and terminals.
• LHS of each rule is a single variable (no terminals).
• RHS of each rule is a string of zero or more variables
and terminals.
A string consists of only terminals.
S  aSb S 

Lec 3 5
Example

Sequence of
terminals and variables

Grammar: S  aSb
S 

Variable The right side


may be 

Lec 3 6
Grammar: S  aSb
S 

Derivation of string ab :
S  aSb  ab

S  aSb S
Lec 3 7
Grammar: S  aSb
S 

Derivation of string aabb :


S  aSb  aaSbb  aabb

S  aSb S
Lec 3 8
Grammar: S  aSb
S 

Other derivations:

S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb
Lec 3 9
Grammar: S  aSb
S 

Language of the grammar:

L  {anbn : n  0}

Lec 3 10
• Example CFG for {0k1k | k≥0}:

G = ({S}, {0, 1}, P, S) // Remember: G = (V, T, P, S)


P:
(1) S –> 0S1 or just simply S –> 0S1 | ε
(2) S –> ε
• Example Derivations:
S => 0S1 (1)
=> 01 (2)

S => 0S1 (1)


=> 00S11 (1)
=> 000S111 (1)
=> 000111 (2) 11
A Convenient Notation
*
We write: S  aaabbb
for zero or more derivation steps

Instead of:

S  aSb  aaSbb  aaaSbbb  aaabbb

Lec 3 12
*
In general we write: w1  wn

If: w1  w2  w3  wn
in zero or more derivation steps

*
Trivially: w  w
Lec 3 13
Example Grammar Possible Derivations
S  aSb *
S 
S 
*
S  ab
*
S  aaabbb

 
S aaSbb aaaaaSbbbb b

Lec 3 14
Formal Definitions

Grammar: G  V ,T , S, P

Set of
variables
Set of Start Set of
terminal variable productions
symbols

Lec 3 15
Example of Context-Free Grammar
S  aSb | 
productions
P {S  aSb, S  }

G  V ,T , S, P

V {S}
T  {a,b} start variable
variables
terminals
Lec 3 16
Construct the CFG for the language that generates:
L = {anbn+1 : n  0}
So the strings of this language will be:
b (0 a's and 1 b)
abb (1 a and 2 b's)
aabbb (2 a's and 3 b's) . . .

S  aSb
Sb
Construct the CFG for the language having any number of
a's over the set ∑= {a}.
L = {an : n  0}
L = {ε a , aa , aaa , …}

S → aS
S→ε

L = {an : n > 0}
L = { a , aa , aaa , …}

S → aS
S→a

L = {anb2n where n>=1}


{abb, aabbbb, aaabbbbbb....}
S → aS bb
S → abb
Construct a CFG for the regular expression (0+1)*
L = {(0 +1)m : m  0}
L = {ε 0 , 00 ,1,11,011 ,100 …}

S  0S
S  1S
Sε
L = {wcwR | where w € (a, b)*}.
{aacaa, bcb, abcba, bacab, abbcbba, ....}

S → aSa
S → bSb
S→c
Construct the CFG for the language
L = {anbmcm+n : n,m ≥0}
{ε , ac,aacc, abbccc, abbb,bbcc, ....}

S → aSc
S → bAc
A → bAc
S→ ε
A→ ε

You might also like