Sunteți pe pagina 1din 252

Context-Free

Languages
(CFL)

Asai
Asaithambi

Context-Free Languages (CFL)

Asai Asaithambi

Fall 2016

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi
Context-Free Grammars (CFG)

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi
Context-Free Grammars (CFG)
Definition and Examples

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi
Context-Free Grammars (CFG)
Definition and Examples
Language of a CFG

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi
Context-Free Grammars (CFG)
Definition and Examples
Language of a CFG (Context-Free Language: CFL)

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi
Context-Free Grammars (CFG)
Definition and Examples
Language of a CFG (Context-Free Language: CFL)
Derivations and Parse Trees

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi
Context-Free Grammars (CFG)
Definition and Examples
Language of a CFG (Context-Free Language: CFL)
Derivations and Parse Trees
Ambiguity

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi
Context-Free Grammars (CFG)
Definition and Examples
Language of a CFG (Context-Free Language: CFL)
Derivations and Parse Trees
Ambiguity
Designing CFGs

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi
Context-Free Grammars (CFG)
Definition and Examples
Language of a CFG (Context-Free Language: CFL)
Derivations and Parse Trees
Ambiguity
Designing CFGs
Chomsky Normal Form

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi
Context-Free Grammars (CFG)
Definition and Examples
Language of a CFG (Context-Free Language: CFL)
Derivations and Parse Trees
Ambiguity
Designing CFGs
Chomsky Normal Form
Push Down Automata (PDA)

Asai Asaithambi Context-Free Languages (CFL)


Topic Overview

Context-Free
Languages
(CFL)

Asai What does this topic cover?


Asaithambi
Context-Free Grammars (CFG)
Definition and Examples
Language of a CFG (Context-Free Language: CFL)
Derivations and Parse Trees
Ambiguity
Designing CFGs
Chomsky Normal Form
Push Down Automata (PDA)
Examples of PDAs

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule:

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side:

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side: A string of variables and terminals

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side: A string of variables and terminals
Variables:

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side: A string of variables and terminals
Variables: uppercase letters

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side: A string of variables and terminals
Variables: uppercase letters
Terminals:

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side: A string of variables and terminals
Variables: uppercase letters
Terminals: lowercase letters,

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side: A string of variables and terminals
Variables: uppercase letters
Terminals: lowercase letters, numerals,

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side: A string of variables and terminals
Variables: uppercase letters
Terminals: lowercase letters, numerals, special symbols

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side: A string of variables and terminals
Variables: uppercase letters
Terminals: lowercase letters, numerals, special symbols
One variable is designated the start variable

Asai Asaithambi Context-Free Languages (CFL)


CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side: A string of variables and terminals
Variables: uppercase letters
Terminals: lowercase letters, numerals, special symbols
One variable is designated the start variable
Start variable:
Asai Asaithambi Context-Free Languages (CFL)
CFG: An Example

Context-Free
Languages
(CFL)

Asai G1 : A 0A1
Asaithambi
AB
B#
Substitution rules or Productions
Each rule appears as a line in the grammar
Left side symbol in a rule: variable
Right side: A string of variables and terminals
Variables: uppercase letters
Terminals: lowercase letters, numerals, special symbols
One variable is designated the start variable
Start variable: Occurs on left side of the topmost rule
Asai Asaithambi Context-Free Languages (CFL)
Generating Strings From A CFG

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi Find a rule that starts with a written down variable.

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi Find a rule that starts with a written down variable.
Replace written down variable with right side of that rule.

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi Find a rule that starts with a written down variable.
Replace written down variable with right side of that rule.
Repeat the above step until no variables remain.

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi Find a rule that starts with a written down variable.
Replace written down variable with right side of that rule.
Repeat the above step until no variables remain.

G1 : A 0A1
AB
B#

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi Find a rule that starts with a written down variable.
Replace written down variable with right side of that rule.
Repeat the above step until no variables remain.

G1 : A 0A1
AB
B#

For instance, G1 generates the string 000#111


Derivation:

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi Find a rule that starts with a written down variable.
Replace written down variable with right side of that rule.
Repeat the above step until no variables remain.

G1 : A 0A1
AB
B#

For instance, G1 generates the string 000#111


Derivation: Sequence of substitutions leading to a string

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi Find a rule that starts with a written down variable.
Replace written down variable with right side of that rule.
Repeat the above step until no variables remain.

G1 : A 0A1
AB
B#

For instance, G1 generates the string 000#111


Derivation: Sequence of substitutions leading to a string
A 0A1

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi Find a rule that starts with a written down variable.
Replace written down variable with right side of that rule.
Repeat the above step until no variables remain.

G1 : A 0A1
AB
B#

For instance, G1 generates the string 000#111


Derivation: Sequence of substitutions leading to a string
A 0A1 00A11

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi Find a rule that starts with a written down variable.
Replace written down variable with right side of that rule.
Repeat the above step until no variables remain.

G1 : A 0A1
AB
B#

For instance, G1 generates the string 000#111


Derivation: Sequence of substitutions leading to a string
A 0A1 00A11 000B111

Asai Asaithambi Context-Free Languages (CFL)


Generating Strings From A CFG

Context-Free
Languages
(CFL)
Write down the start variable.
Asai
Asaithambi Find a rule that starts with a written down variable.
Replace written down variable with right side of that rule.
Repeat the above step until no variables remain.

G1 : A 0A1
AB
B#

For instance, G1 generates the string 000#111


Derivation: Sequence of substitutions leading to a string
A 0A1 00A11 000B111 000#111

Asai Asaithambi Context-Free Languages (CFL)


Parse Tree

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Parse Tree

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Parse Tree

Context-Free
Languages
(CFL)

Asai
Asaithambi

All strings thus generated from the grammar constitute


the language of the grammar L(G1 ) = {0n #1n }.

Asai Asaithambi Context-Free Languages (CFL)


Parse Tree

Context-Free
Languages
(CFL)

Asai
Asaithambi

All strings thus generated from the grammar constitute


the language of the grammar L(G1 ) = {0n #1n }.
A 0A1 and A B can be written in one line as

Asai Asaithambi Context-Free Languages (CFL)


Parse Tree

Context-Free
Languages
(CFL)

Asai
Asaithambi

All strings thus generated from the grammar constitute


the language of the grammar L(G1 ) = {0n #1n }.
A 0A1 and A B can be written in one line as
A 0A1 | B

Asai Asaithambi Context-Free Languages (CFL)


Parse Tree

Context-Free
Languages
(CFL)

Asai
Asaithambi

All strings thus generated from the grammar constitute


the language of the grammar L(G1 ) = {0n #1n }.
A 0A1 and A B can be written in one line as
A 0A1 | B
The symbol | is read or

Asai Asaithambi Context-Free Languages (CFL)


Another CFG: G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Another CFG: G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Another CFG: G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

G2 has 10 variables;

Asai Asaithambi Context-Free Languages (CFL)


Another CFG: G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

G2 has 10 variables; 27 terminals;

Asai Asaithambi Context-Free Languages (CFL)


Another CFG: G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

G2 has 10 variables; 27 terminals; 18 rules

Asai Asaithambi Context-Free Languages (CFL)


Another CFG: G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

G2 has 10 variables; 27 terminals; 18 rules


Some strings in L(G2 ):

Asai Asaithambi Context-Free Languages (CFL)


Another CFG: G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

G2 has 10 variables; 27 terminals; 18 rules


Some strings in L(G2 ):
a boy sees

Asai Asaithambi Context-Free Languages (CFL)


Another CFG: G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

G2 has 10 variables; 27 terminals; 18 rules


Some strings in L(G2 ):
a boy sees
the boy sees a flower

Asai Asaithambi Context-Free Languages (CFL)


Another CFG: G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

G2 has 10 variables; 27 terminals; 18 rules


Some strings in L(G2 ):
a boy sees
the boy sees a flower
a girl with a flower likes the boy
Asai Asaithambi Context-Free Languages (CFL)
A Sample Derivation in G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


A Sample Derivation in G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi is a finite set called terminals, with V = ;

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi is a finite set called terminals, with V = ;
R is a finite set of rules; and

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi is a finite set called terminals, with V = ;
R is a finite set of rules; and
S V is the start variable.

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi is a finite set called terminals, with V = ;
R is a finite set of rules; and
S V is the start variable.
Let u, v , w be strings of variables and terminals

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi is a finite set called terminals, with V = ;
R is a finite set of rules; and
S V is the start variable.
Let u, v , w be strings of variables and terminals
Let A w be a rule in the grammar

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi is a finite set called terminals, with V = ;
R is a finite set of rules; and
S V is the start variable.
Let u, v , w be strings of variables and terminals
Let A w be a rule in the grammar
We say uAv yields uwv , and write uAv uwv

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi is a finite set called terminals, with V = ;
R is a finite set of rules; and
S V is the start variable.
Let u, v , w be strings of variables and terminals
Let A w be a rule in the grammar
We say uAv yields uwv , and write uAv uwv

We say u derives v and write u = v , if

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi is a finite set called terminals, with V = ;
R is a finite set of rules; and
S V is the start variable.
Let u, v , w be strings of variables and terminals
Let A w be a rule in the grammar
We say uAv yields uwv , and write uAv uwv

We say u derives v and write u = v , if
Either u = v , or, for k 0, a sequence
u0 , u1 , u2 , , uk exists such that

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi is a finite set called terminals, with V = ;
R is a finite set of rules; and
S V is the start variable.
Let u, v , w be strings of variables and terminals
Let A w be a rule in the grammar
We say uAv yields uwv , and write uAv uwv

We say u derives v and write u = v , if
Either u = v , or, for k 0, a sequence
u0 , u1 , u2 , , uk exists such that
u u0 u1 uk v .

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of a CFG

Context-Free
Languages
A context-free grammar is a 4-tuple (V , , R, S), where
(CFL)
V is a finite set called variables;
Asai
Asaithambi is a finite set called terminals, with V = ;
R is a finite set of rules; and
S V is the start variable.
Let u, v , w be strings of variables and terminals
Let A w be a rule in the grammar
We say uAv yields uwv , and write uAv uwv

We say u derives v and write u = v , if
Either u = v , or, for k 0, a sequence
u0 , u1 , u2 , , uk exists such that
u u0 u1 uk v .

The language of the grammar is {w | S =
w }.
Asai Asaithambi Context-Free Languages (CFL)
Formal Definition of G1

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
V = {A, B};
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
V = {A, B};
Asaithambi
= {0, 1, #};

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
V = {A, B};
Asaithambi
= {0, 1, #};
R : A 0A1 | B, B #; and

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
V = {A, B};
Asaithambi
= {0, 1, #};
R : A 0A1 | B, B #; and
S = A.

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
V = {A, B};
Asaithambi
= {0, 1, #};
R : A 0A1 | B, B #; and
S = A.

Writing only the rules to specify a grammar is sufficient.

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
V = {A, B};
Asaithambi
= {0, 1, #};
R : A 0A1 | B, B #; and
S = A.

Writing only the rules to specify a grammar is sufficient.


This is so because:

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
V = {A, B};
Asaithambi
= {0, 1, #};
R : A 0A1 | B, B #; and
S = A.

Writing only the rules to specify a grammar is sufficient.


This is so because:
The start symbol is the left side of the topmost rule.

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
V = {A, B};
Asaithambi
= {0, 1, #};
R : A 0A1 | B, B #; and
S = A.

Writing only the rules to specify a grammar is sufficient.


This is so because:
The start symbol is the left side of the topmost rule.
Variables: Uppercase letters.

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
V = {A, B};
Asaithambi
= {0, 1, #};
R : A 0A1 | B, B #; and
S = A.

Writing only the rules to specify a grammar is sufficient.


This is so because:
The start symbol is the left side of the topmost rule.
Variables: Uppercase letters.
Terminals: Lowercase letters, numerals, or symbols.

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G1

Context-Free
Languages G1 = (V , , R, S), where:
(CFL)

Asai
V = {A, B};
Asaithambi
= {0, 1, #};
R : A 0A1 | B, B #; and
S = A.

Writing only the rules to specify a grammar is sufficient.


This is so because:
The start symbol is the left side of the topmost rule.
Variables: Uppercase letters.
Terminals: Lowercase letters, numerals, or symbols.
These are implicit in the writing of the rules.

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G2

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G2

Context-Free
Languages
G2 = (V , , R, S), where:
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G2

Context-Free
Languages
G2 = (V , , R, S), where:
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G2

Context-Free
Languages
G2 = (V , , R, S), where:
(CFL)

Asai
Asaithambi

= {a, b, c, , z, };

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G2

Context-Free
Languages
G2 = (V , , R, S), where:
(CFL)

Asai
Asaithambi

= {a, b, c, , z, };
S = hSENTENCEi.

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of G2

Context-Free
Languages
G2 = (V , , R, S), where:
(CFL)

Asai
Asaithambi

= {a, b, c, , z, };
S = hSENTENCEi.

Asai Asaithambi Context-Free Languages (CFL)


Another Grammar G3

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Another Grammar G3

Context-Free
Languages
(CFL)

Asai
Asaithambi

G3 = ({S}, {a, b}, R, S),

Asai Asaithambi Context-Free Languages (CFL)


Another Grammar G3

Context-Free
Languages
(CFL)

Asai
Asaithambi

G3 = ({S}, {a, b}, R, S), with R given by S aSb | SS | .

Asai Asaithambi Context-Free Languages (CFL)


Another Grammar G3

Context-Free
Languages
(CFL)

Asai
Asaithambi

G3 = ({S}, {a, b}, R, S), with R given by S aSb | SS | .


Generates strings such as: abab,

Asai Asaithambi Context-Free Languages (CFL)


Another Grammar G3

Context-Free
Languages
(CFL)

Asai
Asaithambi

G3 = ({S}, {a, b}, R, S), with R given by S aSb | SS | .


Generates strings such as: abab, aaabbb,

Asai Asaithambi Context-Free Languages (CFL)


Another Grammar G3

Context-Free
Languages
(CFL)

Asai
Asaithambi

G3 = ({S}, {a, b}, R, S), with R given by S aSb | SS | .


Generates strings such as: abab, aaabbb, aababb

Asai Asaithambi Context-Free Languages (CFL)


Another Grammar G3

Context-Free
Languages
(CFL)

Asai
Asaithambi

G3 = ({S}, {a, b}, R, S), with R given by S aSb | SS | .


Generates strings such as: abab, aaabbb, aababb
Consider a as ( and

Asai Asaithambi Context-Free Languages (CFL)


Another Grammar G3

Context-Free
Languages
(CFL)

Asai
Asaithambi

G3 = ({S}, {a, b}, R, S), with R given by S aSb | SS | .


Generates strings such as: abab, aaabbb, aababb
Consider a as ( and b as )

Asai Asaithambi Context-Free Languages (CFL)


Another Grammar G3

Context-Free
Languages
(CFL)

Asai
Asaithambi

G3 = ({S}, {a, b}, R, S), with R given by S aSb | SS | .


Generates strings such as: abab, aaabbb, aababb
Consider a as ( and b as )
L(G3 ): all strings of properly nested parentheses

Asai Asaithambi Context-Free Languages (CFL)


Another Grammar G3

Context-Free
Languages
(CFL)

Asai
Asaithambi

G3 = ({S}, {a, b}, R, S), with R given by S aSb | SS | .


Generates strings such as: abab, aaabbb, aababb
Consider a as ( and b as )
L(G3 ): all strings of properly nested parentheses
The right side of a rule may be the empty string

Asai Asaithambi Context-Free Languages (CFL)


G4 : A Grammar for Arithmetic Expressions

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


G4 : A Grammar for Arithmetic Expressions

Context-Free
Languages
(CFL) G4 = (V , , R, S), where:
Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


G4 : A Grammar for Arithmetic Expressions

Context-Free
Languages
(CFL) G4 = (V , , R, S), where:
Asai
Asaithambi
V = {hEXPRi, hTERMi, hFACTORi}
= {a, +, , (, )}
S = hEXPRi

Asai Asaithambi Context-Free Languages (CFL)


G4 : A Grammar for Arithmetic Expressions

Context-Free
Languages
(CFL) G4 = (V , , R, S), where:
Asai
Asaithambi
V = {hEXPRi, hTERMi, hFACTORi}
= {a, +, , (, )}
S = hEXPRi

R contains the rules as shown below:

Asai Asaithambi Context-Free Languages (CFL)


G4 : A Grammar for Arithmetic Expressions

Context-Free
Languages
(CFL) G4 = (V , , R, S), where:
Asai
Asaithambi
V = {hEXPRi, hTERMi, hFACTORi}
= {a, +, , (, )}
S = hEXPRi

R contains the rules as shown below:

Asai Asaithambi Context-Free Languages (CFL)


Parse Trees for Two Expressions

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Parse Trees for Two Expressions

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Parse Trees for Two Expressions

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)

Asai
Asaithambi

If a derivation replaces the leftmost variable at every step,


then

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)

Asai
Asaithambi

If a derivation replaces the leftmost variable at every step,


then such a derivation is called a leftmost derivation.

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)

Asai
Asaithambi

If a derivation replaces the leftmost variable at every step,


then such a derivation is called a leftmost derivation.
A string is derived ambiguously in a CFG

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)

Asai
Asaithambi

If a derivation replaces the leftmost variable at every step,


then such a derivation is called a leftmost derivation.
A string is derived ambiguously in a CFG if it has two or
more leftmost derivations.

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)

Asai
Asaithambi

If a derivation replaces the leftmost variable at every step,


then such a derivation is called a leftmost derivation.
A string is derived ambiguously in a CFG if it has two or
more leftmost derivations.
A grammar G is ambiguous

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)

Asai
Asaithambi

If a derivation replaces the leftmost variable at every step,


then such a derivation is called a leftmost derivation.
A string is derived ambiguously in a CFG if it has two or
more leftmost derivations.
A grammar G is ambiguous if it generates some string am-
biguously.

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)
Consider the grammar G5 described below:
Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)
Consider the grammar G5 described below:
Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)
Consider the grammar G5 described below:
Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Ambiguity

Context-Free
Languages
(CFL)
Consider the grammar G5 described below:
Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Designing CFGs

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Designing CFGs

Context-Free
Languages
(CFL)

Asai
Asaithambi A CFL is often a union of simpler CFLs

Asai Asaithambi Context-Free Languages (CFL)


Designing CFGs

Context-Free
Languages
(CFL)

Asai
Asaithambi A CFL is often a union of simpler CFLs
Break into simpler pieces

Asai Asaithambi Context-Free Languages (CFL)


Designing CFGs

Context-Free
Languages
(CFL)

Asai
Asaithambi A CFL is often a union of simpler CFLs
Break into simpler pieces
A CFL may also be a regular language

Asai Asaithambi Context-Free Languages (CFL)


Designing CFGs

Context-Free
Languages
(CFL)

Asai
Asaithambi A CFL is often a union of simpler CFLs
Break into simpler pieces
A CFL may also be a regular language
Construct a DFA

Asai Asaithambi Context-Free Languages (CFL)


Designing CFGs

Context-Free
Languages
(CFL)

Asai
Asaithambi A CFL is often a union of simpler CFLs
Break into simpler pieces
A CFL may also be a regular language
Construct a DFA
A CFL may contain two linked substrings

Asai Asaithambi Context-Free Languages (CFL)


Designing CFGs

Context-Free
Languages
(CFL)

Asai
Asaithambi A CFL is often a union of simpler CFLs
Break into simpler pieces
A CFL may also be a regular language
Construct a DFA
A CFL may contain two linked substrings
Use a rule of the form R uRv

Asai Asaithambi Context-Free Languages (CFL)


Designing CFGs

Context-Free
Languages
(CFL)

Asai
Asaithambi A CFL is often a union of simpler CFLs
Break into simpler pieces
A CFL may also be a regular language
Construct a DFA
A CFL may contain two linked substrings
Use a rule of the form R uRv
A CFL may contain structures appearing recursively

Asai Asaithambi Context-Free Languages (CFL)


Designing CFGs

Context-Free
Languages
(CFL)

Asai
Asaithambi A CFL is often a union of simpler CFLs
Break into simpler pieces
A CFL may also be a regular language
Construct a DFA
A CFL may contain two linked substrings
Use a rule of the form R uRv
A CFL may contain structures appearing recursively
Use the rule for the structure recursively

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2
Add the rule S S1 | S2

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2
Add the rule S S1 | S2
L = {0n 1n | n 0} {1n 0n | n 0}

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2
Add the rule S S1 | S2
L = {0n 1n | n 0} {1n 0n | n 0}
G1 :

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2
Add the rule S S1 | S2
L = {0n 1n | n 0} {1n 0n | n 0}
G1 : S1 0S1 1 |

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2
Add the rule S S1 | S2
L = {0n 1n | n 0} {1n 0n | n 0}
G1 : S1 0S1 1 |
G2 :

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2
Add the rule S S1 | S2
L = {0n 1n | n 0} {1n 0n | n 0}
G1 : S1 0S1 1 |
G2 : S2 1S2 0 |

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2
Add the rule S S1 | S2
L = {0n 1n | n 0} {1n 0n | n 0}
G1 : S1 0S1 1 | G:
G2 : S2 1S2 0 |

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2
Add the rule S S1 | S2
L = {0n 1n | n 0} {1n 0n | n 0}
G1 : S1 0S1 1 | G: S S1 | S2
G2 : S2 1S2 0 |

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2
Add the rule S S1 | S2
L = {0n 1n | n 0} {1n 0n | n 0}
G1 : S1 0S1 1 | G: S S1 | S2
G2 : S2 1S2 0 | S1 0S1 1 |

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Union of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi Suppose L = L1 L2 .
Design CFGs separately for L1 and L2
Let S1 , S2 be the start variables for the CFGs L1 , L2
Add the rule S S1 | S2
L = {0n 1n | n 0} {1n 0n | n 0}
G1 : S1 0S1 1 | G: S S1 | S2
G2 : S2 1S2 0 | S1 0S1 1 |
S2 1S2 0 |

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Regular Language

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Regular Language

Context-Free
Languages Construct a DFA for the language.
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Regular Language

Context-Free
Languages Construct a DFA for the language.
(CFL)
Make a variable Ri for each state qi .
Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Regular Language

Context-Free
Languages Construct a DFA for the language.
(CFL)
Make a variable Ri for each state qi .
Asai
Asaithambi Make R0 the start variable

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Regular Language

Context-Free
Languages Construct a DFA for the language.
(CFL)
Make a variable Ri for each state qi .
Asai
Asaithambi Make R0 the start variable (start state q0 )

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Regular Language

Context-Free
Languages Construct a DFA for the language.
(CFL)
Make a variable Ri for each state qi .
Asai
Asaithambi Make R0 the start variable (start state q0 )
Make a rule for each edge

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Regular Language

Context-Free
Languages Construct a DFA for the language.
(CFL)
Make a variable Ri for each state qi .
Asai
Asaithambi Make R0 the start variable (start state q0 )
Make a rule for each edge
(qi , a) = qj translates to Ri a Rj

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Regular Language

Context-Free
Languages Construct a DFA for the language.
(CFL)
Make a variable Ri for each state qi .
Asai
Asaithambi Make R0 the start variable (start state q0 )
Make a rule for each edge
(qi , a) = qj translates to Ri a Rj
Add the rule Ri if qi is an accept state

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Regular Language

Context-Free
Languages Construct a DFA for the language.
(CFL)
Make a variable Ri for each state qi .
Asai
Asaithambi Make R0 the start variable (start state q0 )
Make a rule for each edge
(qi , a) = qj translates to Ri a Rj
Add the rule Ri if qi is an accept state

Asai Asaithambi Context-Free Languages (CFL)


CFG for a Regular Language

Context-Free
Languages Construct a DFA for the language.
(CFL)
Make a variable Ri for each state qi .
Asai
Asaithambi Make R0 the start variable (start state q0 )
Make a rule for each edge
(qi , a) = qj translates to Ri a Rj
Add the rule Ri if qi is an accept state

Asai Asaithambi Context-Free Languages (CFL)


Chomsky Normal Form (CNF)

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Chomsky Normal Form (CNF)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Every rule of the grammar must be of the form:

Asai Asaithambi Context-Free Languages (CFL)


Chomsky Normal Form (CNF)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Every rule of the grammar must be of the form:
Either A BC , or A a

Asai Asaithambi Context-Free Languages (CFL)


Chomsky Normal Form (CNF)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Every rule of the grammar must be of the form:
Either A BC , or A a
Neither B nor C can be the start variable

Asai Asaithambi Context-Free Languages (CFL)


Chomsky Normal Form (CNF)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Every rule of the grammar must be of the form:
Either A BC , or A a
Neither B nor C can be the start variable
a is a terminal symbol

Asai Asaithambi Context-Free Languages (CFL)


Chomsky Normal Form (CNF)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Every rule of the grammar must be of the form:
Either A BC , or A a
Neither B nor C can be the start variable
a is a terminal symbol
Can have S , where S is the start variable

Asai Asaithambi Context-Free Languages (CFL)


Chomsky Normal Form (CNF)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Every rule of the grammar must be of the form:
Either A BC , or A a
Neither B nor C can be the start variable
a is a terminal symbol
Can have S , where S is the start variable
Two grammars are said to be equivalent:

Asai Asaithambi Context-Free Languages (CFL)


Chomsky Normal Form (CNF)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Every rule of the grammar must be of the form:
Either A BC , or A a
Neither B nor C can be the start variable
a is a terminal symbol
Can have S , where S is the start variable
Two grammars are said to be equivalent:
if they generate the same language.

Asai Asaithambi Context-Free Languages (CFL)


Chomsky Normal Form (CNF)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Every rule of the grammar must be of the form:
Either A BC , or A a
Neither B nor C can be the start variable
a is a terminal symbol
Can have S , where S is the start variable
Two grammars are said to be equivalent:
if they generate the same language.
Every CFL can be generated by a grammar in CNF

Asai Asaithambi Context-Free Languages (CFL)


Chomsky Normal Form (CNF)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Every rule of the grammar must be of the form:
Either A BC , or A a
Neither B nor C can be the start variable
a is a terminal symbol
Can have S , where S is the start variable
Two grammars are said to be equivalent:
if they generate the same language.
Every CFL can be generated by a grammar in CNF
For every CFG there is an equivalent grammar in CNF

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF

Context-Free
Languages
(CFL)

Asai
Asaithambi

Start symbol cannot appear on the right side of any rule.

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF

Context-Free
Languages
(CFL)

Asai
Asaithambi

Start symbol cannot appear on the right side of any rule.


Remove rules like A , where A is not the start symbol.

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF

Context-Free
Languages
(CFL)

Asai
Asaithambi

Start symbol cannot appear on the right side of any rule.


Remove rules like A , where A is not the start symbol.
Remove unit rules like A B

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF

Context-Free
Languages
(CFL)

Asai
Asaithambi

Start symbol cannot appear on the right side of any rule.


Remove rules like A , where A is not the start symbol.
Remove unit rules like A B
Eliminate rules with more than two symbols on the right

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF

Context-Free
Languages
(CFL)

Asai
Asaithambi

Start symbol cannot appear on the right side of any rule.


Remove rules like A , where A is not the start symbol.
Remove unit rules like A B
Eliminate rules with more than two symbols on the right
The two symbols must be variables.

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF

Context-Free
Languages
(CFL)

Asai
Asaithambi

Start symbol cannot appear on the right side of any rule.


Remove rules like A , where A is not the start symbol.
Remove unit rules like A B
Eliminate rules with more than two symbols on the right
The two symbols must be variables.
Make sure all rules are in the CNF (A BC , A )

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)

Asai
Asaithambi S ASA | aB
A B |S
B b|

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB
A B |S
B b|

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S
B b|

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S S ASA | aB
B b| A B |S
B b|

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S S ASA | aB
B b| A B |S
B b|
Remove B rule.

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S S ASA | aB
B b| A B |S
B b|
Remove B rule.

Bb

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S S ASA | aB
B b| A B |S
B b|
Remove B rule.

A B |S |
Bb

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S S ASA | aB
B b| A B |S
B b|
Remove B rule.

S ASA | aB | a
A B |S |
Bb

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S S ASA | aB
B b| A B |S
B b|
Remove B rule.
S0 S
S ASA | aB | a
A B |S |
Bb

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S S ASA | aB
B b| A B |S
B b|
Remove B rule.
S0 S
S ASA | aB | a
A B |S |
Bb
Remove A rule.

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S S ASA | aB
B b| A B |S
B b|
Remove B rule.
S0 S
S ASA | aB | a
A B |S | A B |S
Bb
Remove A rule.

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S S ASA | aB
B b| A B |S
B b|
Remove B rule.
S0 S
S ASA | aB | a S ASA | aB | a | SA | AS | S
A B |S | A B |S
Bb
Remove A rule.

Asai Asaithambi Context-Free Languages (CFL)


Converting a CFG to CNF: An Example

Context-Free
Languages
(CFL)
Add a new start symbol S0 .
Asai
Asaithambi S ASA | aB S0 S
A B |S S ASA | aB
B b| A B |S
B b|
Remove B rule.
S0 S S0 S
S ASA | aB | a S ASA | aB | a | SA | AS | S
A B |S | A B |S
Bb Bb
Remove A rule.

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi
S0 S
S ASA | aB | a | SA | AS | S
A B |S
Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Remove S S and S0 S unit rules
Asaithambi
S0 S
S ASA | aB | a | SA | AS | S
A B |S
Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Remove S S and S0 S unit rules
Asaithambi
S0 S
S ASA | aB | a | SA | AS | S S ASA | aB | a | SA | AS
A B |S
Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Remove S S and S0 S unit rules
Asaithambi
S0 S S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS | S S ASA | aB | a | SA | AS
A B |S
Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Remove S S and S0 S unit rules
Asaithambi
S0 S S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS | S S ASA | aB | a | SA | AS
A B |S A B |S
Bb Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Remove S S and S0 S unit rules
Asaithambi
S0 S S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS | S S ASA | aB | a | SA | AS
A B |S A B |S
Bb Bb
Remove A B and A S unit rules

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Remove S S and S0 S unit rules
Asaithambi
S0 S S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS | S S ASA | aB | a | SA | AS
A B |S A B |S
Bb Bb
Remove A B and A S unit rules

A b|S

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Remove S S and S0 S unit rules
Asaithambi
S0 S S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS | S S ASA | aB | a | SA | AS
A B |S A B |S
Bb Bb
Remove A B and A S unit rules
S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b|S
Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Remove S S and S0 S unit rules
Asaithambi
S0 S S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS | S S ASA | aB | a | SA | AS
A B |S A B |S
Bb Bb
Remove A B and A S unit rules
S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b|S A b | ASA | aB | a | SA | AS
Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Remove S S and S0 S unit rules
Asaithambi
S0 S S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS | S S ASA | aB | a | SA | AS
A B |S A B |S
Bb Bb
Remove A B and A S unit rules
S0 ASA | aB | a | SA | AS S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS S ASA | aB | a | SA | AS
A b|S A b | ASA | aB | a | SA | AS
Bb Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi

S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b | ASA | aB | a | SA | AS
Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Some rules have ASA and aB on the right.

S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b | ASA | aB | a | SA | AS
Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Some rules have ASA and aB on the right.
We add rules A1 SA and U a.
S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b | ASA | aB | a | SA | AS
Bb

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Some rules have ASA and aB on the right.
We add rules A1 SA and U a.
S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b | ASA | aB | a | SA | AS
Bb
A1 SA

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Some rules have ASA and aB on the right.
We add rules A1 SA and U a.
S0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b | ASA | aB | a | SA | AS
Bb
A1 SA
Ua

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Some rules have ASA and aB on the right.
We add rules A1 SA and U a.
S0 ASA | aB | a | SA | AS S0 AA1 | UB | a | SA | AS
S ASA | aB | a | SA | AS
A b | ASA | aB | a | SA | AS
Bb
A1 SA
Ua

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Some rules have ASA and aB on the right.
We add rules A1 SA and U a.
S0 ASA | aB | a | SA | AS S0 AA1 | UB | a | SA | AS
S ASA | aB | a | SA | AS S AA1 | UB | a | SA | AS
A b | ASA | aB | a | SA | AS
Bb
A1 SA
Ua

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Some rules have ASA and aB on the right.
We add rules A1 SA and U a.
S0 ASA | aB | a | SA | AS S0 AA1 | UB | a | SA | AS
S ASA | aB | a | SA | AS S AA1 | UB | a | SA | AS
A b | ASA | aB | a | SA | AS A b | AA1 | UB | a | SA | AS
Bb
A1 SA
Ua

Asai Asaithambi Context-Free Languages (CFL)


CFG to CNF Conversion Example (cont.)

Context-Free
Languages
(CFL)

Asai
Asaithambi
Some rules have ASA and aB on the right.
We add rules A1 SA and U a.
S0 ASA | aB | a | SA | AS S0 AA1 | UB | a | SA | AS
S ASA | aB | a | SA | AS S AA1 | UB | a | SA | AS
A b | ASA | aB | a | SA | AS A b | AA1 | UB | a | SA | AS
Bb Bb
A1 SA
Ua

Asai Asaithambi Context-Free Languages (CFL)


Pushdown Automata (PDA)

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Pushdown Automata (PDA)

Context-Free
Languages
Schematic for FA
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Pushdown Automata (PDA)

Context-Free
Languages
Schematic for FA
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Pushdown Automata (PDA)

Context-Free
Languages
Schematic for FA
(CFL)

Asai
Asaithambi

PDA is another computational model

Asai Asaithambi Context-Free Languages (CFL)


Pushdown Automata (PDA)

Context-Free
Languages
Schematic for FA
(CFL)

Asai
Asaithambi

PDA is another computational model


but has an additional component:

Asai Asaithambi Context-Free Languages (CFL)


Pushdown Automata (PDA)

Context-Free
Languages
Schematic for FA
(CFL)

Asai
Asaithambi

PDA is another computational model


but has an additional component: stack

Asai Asaithambi Context-Free Languages (CFL)


Pushdown Automata (PDA)

Context-Free
Languages
Schematic for FA
(CFL)

Asai
Asaithambi

PDA is another computational model


but has an additional component: stack

Asai Asaithambi Context-Free Languages (CFL)


PDA Characteristics

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


PDA Characteristics

Context-Free
Languages
(CFL)

Asai
Asaithambi

Input string scanned left to right, no backing up.

Asai Asaithambi Context-Free Languages (CFL)


PDA Characteristics

Context-Free
Languages
(CFL)

Asai
Asaithambi

Input string scanned left to right, no backing up.


Stack operations:

Asai Asaithambi Context-Free Languages (CFL)


PDA Characteristics

Context-Free
Languages
(CFL)

Asai
Asaithambi

Input string scanned left to right, no backing up.


Stack operations: Push/Ignore,

Asai Asaithambi Context-Free Languages (CFL)


PDA Characteristics

Context-Free
Languages
(CFL)

Asai
Asaithambi

Input string scanned left to right, no backing up.


Stack operations: Push/Ignore, Read/Pop/Ignore

Asai Asaithambi Context-Free Languages (CFL)


PDA Characteristics

Context-Free
Languages
(CFL)

Asai
Asaithambi

Input string scanned left to right, no backing up.


Stack operations: Push/Ignore, Read/Pop/Ignore
Stack alphabet may be different from input alphabet.

Asai Asaithambi Context-Free Languages (CFL)


PDA Characteristics

Context-Free
Languages
(CFL)

Asai
Asaithambi

Input string scanned left to right, no backing up.


Stack operations: Push/Ignore, Read/Pop/Ignore
Stack alphabet may be different from input alphabet.
State transitions may depend on the top of stack.

Asai Asaithambi Context-Free Languages (CFL)


PDA Characteristics

Context-Free
Languages
(CFL)

Asai
Asaithambi

Input string scanned left to right, no backing up.


Stack operations: Push/Ignore, Read/Pop/Ignore
Stack alphabet may be different from input alphabet.
State transitions may depend on the top of stack.
State transitions may push symbols onto the stack.

Asai Asaithambi Context-Free Languages (CFL)


PDA Characteristics

Context-Free
Languages
(CFL)

Asai
Asaithambi

Input string scanned left to right, no backing up.


Stack operations: Push/Ignore, Read/Pop/Ignore
Stack alphabet may be different from input alphabet.
State transitions may depend on the top of stack.
State transitions may push symbols onto the stack.
PDA is nondeterministic.

Asai Asaithambi Context-Free Languages (CFL)


PDA State Transitions

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


PDA State Transitions

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


PDA State Transitions

Context-Free
Languages
(CFL)

Asai
Asaithambi

a = means the input is .

Asai Asaithambi Context-Free Languages (CFL)


PDA State Transitions

Context-Free
Languages
(CFL)

Asai
Asaithambi

a = means the input is .


b = means the stack is neither read nor popped.

Asai Asaithambi Context-Free Languages (CFL)


PDA State Transitions

Context-Free
Languages
(CFL)

Asai
Asaithambi

a = means the input is .


b = means the stack is neither read nor popped.
c = means nothing is pushed.

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of PDA

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Formal Definition of PDA

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


PDA for {0n 1n | n 0}

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


PDA for {0n 1n | n 0}

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


PDA for {0n 1n | n 0}

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {0n 1n | n 0}

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.


Pushes every 0 onto the stack.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {0n 1n | n 0}

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.


Pushes every 0 onto the stack.
On reading a 1, pops the stack.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {0n 1n | n 0}

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.


Pushes every 0 onto the stack.
On reading a 1, pops the stack.
When the stack becomes empty, accepts the string.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {0n 1n | n 0}

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.


Pushes every 0 onto the stack.
On reading a 1, pops the stack.
When the stack becomes empty, accepts the string.
The bottom of the stack is represented by a $ symbol.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {0n 1n | n 0}

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.


Pushes every 0 onto the stack.
On reading a 1, pops the stack.
When the stack becomes empty, accepts the string.
The bottom of the stack is represented by a $ symbol.
Input alphabet = {0, 1}.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {0n 1n | n 0}

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.


Pushes every 0 onto the stack.
On reading a 1, pops the stack.
When the stack becomes empty, accepts the string.
The bottom of the stack is represented by a $ symbol.
Input alphabet = {0, 1}.
Tape alphhabet = {0, $}.
Asai Asaithambi Context-Free Languages (CFL)
PDA for {ai b j c k | i, j 0 and i = j or i = k}

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ai b j c k | i, j 0 and i = j or i = k}

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ai b j c k | i, j 0 and i = j or i = k}

Context-Free
Languages
(CFL)

Asai
Asaithambi

Pushes every a onto the stack.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ai b j c k | i, j 0 and i = j or i = k}

Context-Free
Languages
(CFL)

Asai
Asaithambi

Pushes every a onto the stack.


Guesses whether i = j or i = k.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ai b j c k | i, j 0 and i = j or i = k}

Context-Free
Languages
(CFL)

Asai
Asaithambi

Pushes every a onto the stack.


Guesses whether i = j or i = k.
If i = j is guessed, pops the stack when a b is read.
Flushes all the cs.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ai b j c k | i, j 0 and i = j or i = k}

Context-Free
Languages
(CFL)

Asai
Asaithambi

Pushes every a onto the stack.


Guesses whether i = j or i = k.
If i = j is guessed, pops the stack when a b is read.
Flushes all the cs.
If i = k is guessed, flushes all the bs
Pops the stack when a c is read.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ai b j c k | i, j 0 and i = j or i = k}

Context-Free
Languages
(CFL)

Asai
Asaithambi

Pushes every a onto the stack.


Guesses whether i = j or i = k.
If i = j is guessed, pops the stack when a b is read.
Flushes all the cs.
If i = k is guessed, flushes all the bs
Pops the stack when a c is read.
When the stack becomes empty, accepts the string.
Asai Asaithambi Context-Free Languages (CFL)
PDA for {ww R | w {0, 1} }

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ww R | w {0, 1} }

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ww R | w {0, 1} }

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ww R | w {0, 1} }

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.


Pushes every symbol (0 or 1) onto the stack.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ww R | w {0, 1} }

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.


Pushes every symbol (0 or 1) onto the stack.
Guesses whether the middle of the string is reached.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ww R | w {0, 1} }

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.


Pushes every symbol (0 or 1) onto the stack.
Guesses whether the middle of the string is reached.
From this point, pops the stack if it matches the input.

Asai Asaithambi Context-Free Languages (CFL)


PDA for {ww R | w {0, 1} }

Context-Free
Languages
(CFL)

Asai
Asaithambi

PDA scans input from left to right.


Pushes every symbol (0 or 1) onto the stack.
Guesses whether the middle of the string is reached.
From this point, pops the stack if it matches the input.
When the stack becomes empty, accepts the string.

Asai Asaithambi Context-Free Languages (CFL)


Classes of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)


Classes of Languages

Context-Free
Languages
(CFL)

Asai
Asaithambi

Asai Asaithambi Context-Free Languages (CFL)

S-ar putea să vă placă și