Foundations of Computer Science Lecture 25 Context Free Grammars - - PowerPoint PPT Presentation

foundations of computer science lecture 25 context free
SMART_READER_LITE
LIVE PREVIEW

Foundations of Computer Science Lecture 25 Context Free Grammars - - PowerPoint PPT Presentation

Foundations of Computer Science Lecture 25 Context Free Grammars (CFGs) Solving a Problem by Listing Out the Language Rules for CFGs Parse Trees Pushdown Automata Last Time DFAs: State transitioning machines which can be implemented


slide-1
SLIDE 1

Foundations of Computer Science Lecture 25 Context Free Grammars (CFGs)

Solving a Problem by “Listing Out” the Language Rules for CFGs Parse Trees Pushdown Automata

slide-2
SLIDE 2

Last Time

DFAs: State transitioning machines which can be implemented using basic technology. Powerful: can solve any regular expression. (Finite sets, complement, union, intersection, concatenation, Kleene-star).

Computing Model Rules to:

  • 1. Construct machine;
  • 2. Solve problems.

Analyze Model

  • 1. Capabilities: what can be solved?
  • 2. Limitations: what can’t be solved?

Do we need a new model?

DFAs fail at so simple a problem as equality. That’s not acceptable. We need a more powerful machine.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 2 / 16 Adding Memory →

slide-3
SLIDE 3

Adding Memory

DFAs have no scratch paper. It’s hard to compute entirely in your head.

q0 q7 q6 q5 q4 q3 q2 q1

yes

  • r

no

1 1 1 1 1

Stack Memory. Think of a file-clerk with a stack of papers. The clerk’s capabilities: see the top sheet; remove the top sheet (pop) push something new onto the top of the stack. no access to inner sheets without removing top. DFA with a stack is a pushdown automaton (PDA)

How does the stack help to solve {0•n1•n | n ≥ 0}?

1: When you read in each 0, write it to the stack. 2: For each 1, pop the stack. At the end if the stack is empty, accept.

The memory allows the automaton to “remember” n.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 3 / 16 Today →

slide-4
SLIDE 4

Today: Context Free Grammars (CFGs)

1

Solving a problem by listing out the language.

2

Rules for Context Free Grammars (CFG).

3

Examples of Context Free Grammars.

English. Programming.

4

Proving a CFG solves a problem.

5

Parse Trees.

6

Pushdown Automata and non context free languages.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 4 / 16 Recursively Defined Language →

slide-5
SLIDE 5

Recursively Defined Language: Listing a Language.

L0n1n = {0•n1•n | n ≥ 0}

1 ε ∈ L0n1n.

[basis]

2 x ∈ L0n1n → 0 •x •1 ∈ L0n1n.

[constructor rule]

3 Nothing else is in L0n1n.

[minimality] To test if 0010 ∈ L0n1n: generate strings in order of length and test each for a match:

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

slide-6
SLIDE 6

Recursively Defined Language: Listing a Language.

L0n1n = {0•n1•n | n ≥ 0}

1 ε ∈ L0n1n.

[basis]

2 x ∈ L0n1n → 0 •x •1 ∈ L0n1n.

[constructor rule]

3 Nothing else is in L0n1n.

[minimality] To test if 0010 ∈ L0n1n: generate strings in order of length and test each for a match:

ε

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

slide-7
SLIDE 7

Recursively Defined Language: Listing a Language.

L0n1n = {0•n1•n | n ≥ 0}

1 ε ∈ L0n1n.

[basis]

2 x ∈ L0n1n → 0 •x •1 ∈ L0n1n.

[constructor rule]

3 Nothing else is in L0n1n.

[minimality] To test if 0010 ∈ L0n1n: generate strings in order of length and test each for a match:

ε → 01

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

slide-8
SLIDE 8

Recursively Defined Language: Listing a Language.

L0n1n = {0•n1•n | n ≥ 0}

1 ε ∈ L0n1n.

[basis]

2 x ∈ L0n1n → 0 •x •1 ∈ L0n1n.

[constructor rule]

3 Nothing else is in L0n1n.

[minimality] To test if 0010 ∈ L0n1n: generate strings in order of length and test each for a match:

ε → 01 → 0011

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

slide-9
SLIDE 9

Recursively Defined Language: Listing a Language.

L0n1n = {0•n1•n | n ≥ 0}

1 ε ∈ L0n1n.

[basis]

2 x ∈ L0n1n → 0 •x •1 ∈ L0n1n.

[constructor rule]

3 Nothing else is in L0n1n.

[minimality] To test if 0010 ∈ L0n1n: generate strings in order of length and test each for a match:

ε → 01 → 0011 → 000111

no

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

slide-10
SLIDE 10

Recursively Defined Language: Listing a Language.

L0n1n = {0•n1•n | n ≥ 0}

1 ε ∈ L0n1n.

[basis]

2 x ∈ L0n1n → 0 •x •1 ∈ L0n1n.

[constructor rule]

3 Nothing else is in L0n1n.

[minimality] To test if 0010 ∈ L0n1n: generate strings in order of length and test each for a match:

ε → 01 → 0011 → 000111

no

A Context Free Grammar is like a recursive definition.

1: S → ε

    ε ∈ L0n1n     Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

slide-11
SLIDE 11

Recursively Defined Language: Listing a Language.

L0n1n = {0•n1•n | n ≥ 0}

1 ε ∈ L0n1n.

[basis]

2 x ∈ L0n1n → 0 •x •1 ∈ L0n1n.

[constructor rule]

3 Nothing else is in L0n1n.

[minimality] To test if 0010 ∈ L0n1n: generate strings in order of length and test each for a match:

ε → 01 → 0011 → 000111

no

A Context Free Grammar is like a recursive definition.

1: S → ε 2: S → 0S1

    ε ∈ L0n1n

x ∈ L0n1n → 0 •x •1 ∈ L0n1n

    Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

slide-12
SLIDE 12

Rules for Context Free Grammars (CFGs)

Production rules of the CFG:

1: S → ε 2: S → 0S1

Each production rule has the form

variable

P, Q, R, S, T, . . . → expression

string of variables and terminals

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfgbal →

slide-13
SLIDE 13

Rules for Context Free Grammars (CFGs)

Production rules of the CFG:

1: S → ε 2: S → 0S1

Each production rule has the form

variable

P, Q, R, S, T, . . . → expression

string of variables and terminals

S

1: Write down the start variable (form the first production rule, typically S).

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfgbal →

slide-14
SLIDE 14

Rules for Context Free Grammars (CFGs)

Production rules of the CFG:

1: S → ε 2: S → 0S1

Each production rule has the form

variable

P, Q, R, S, T, . . . → expression

string of variables and terminals

S ε 0S1

1: 2:

1: Write down the start variable (form the first production rule, typically S). 2: Replace one variable in the current string with the expression from a pro-

duction rule that starts with that variable on the left. “Replace variable with expression, no matter where (independent of context)”

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfgbal →

slide-15
SLIDE 15

Rules for Context Free Grammars (CFGs)

Production rules of the CFG:

1: S → ε 2: S → 0S1

Each production rule has the form

variable

P, Q, R, S, T, . . . → expression

string of variables and terminals

S ε 0S1

1: 2:

1: Write down the start variable (form the first production rule, typically S). 2: Replace one variable in the current string with the expression from a pro-

duction rule that starts with that variable on the left.

3: Repeat step 2 until no variables remain in the string.

“Replace variable with expression, no matter where (independent of context)”

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfgbal →

slide-16
SLIDE 16

Rules for Context Free Grammars (CFGs)

Production rules of the CFG:

1: S → ε 2: S → 0S1

Each production rule has the form

variable

P, Q, R, S, T, . . . → expression

string of variables and terminals

S ε 0S1

1: 2:

00S11 01

1: 2:

1: Write down the start variable (form the first production rule, typically S). 2: Replace one variable in the current string with the expression from a pro-

duction rule that starts with that variable on the left.

3: Repeat step 2 until no variables remain in the string.

“Replace variable with expression, no matter where (independent of context)”

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfgbal →

slide-17
SLIDE 17

Rules for Context Free Grammars (CFGs)

Production rules of the CFG:

1: S → ε 2: S → 0S1

Each production rule has the form

variable

P, Q, R, S, T, . . . → expression

string of variables and terminals

S ε 0S1

1: 2:

00S11 01

1: 2:

000S111 0011

1: 2:

1: Write down the start variable (form the first production rule, typically S). 2: Replace one variable in the current string with the expression from a pro-

duction rule that starts with that variable on the left.

3: Repeat step 2 until no variables remain in the string.

“Replace variable with expression, no matter where (independent of context)”

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfgbal →

slide-18
SLIDE 18

Rules for Context Free Grammars (CFGs)

Production rules of the CFG:

1: S → ε 2: S → 0S1

Each production rule has the form

variable

P, Q, R, S, T, . . . → expression

string of variables and terminals

S ε 0S1

1: 2:

00S11 01

1: 2:

000S111 0011

1: 2:

0000S1111 000111

1: 2:

1: Write down the start variable (form the first production rule, typically S). 2: Replace one variable in the current string with the expression from a pro-

duction rule that starts with that variable on the left.

3: Repeat step 2 until no variables remain in the string.

“Replace variable with expression, no matter where (independent of context)”

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfgbal →

slide-19
SLIDE 19

Rules for Context Free Grammars (CFGs)

Production rules of the CFG:

1: S → ε 2: S → 0S1

Each production rule has the form

variable

P, Q, R, S, T, . . . → expression

string of variables and terminals

S ε 0S1

1: 2:

00S11 01

1: 2:

000S111 0011

1: 2:

0000S1111 000111

1: 2:

· · · 00001111

1: 2:

1: Write down the start variable (form the first production rule, typically S). 2: Replace one variable in the current string with the expression from a pro-

duction rule that starts with that variable on the left.

3: Repeat step 2 until no variables remain in the string.

“Replace variable with expression, no matter where (independent of context)” Shorthand:

1: S → ε | 0S1

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfgbal →

slide-20
SLIDE 20

Language of Equality, cfgbal

cfgbal

1: S → ε | 0S1S | 1S0S

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 7 / 16 A CFG for English →

slide-21
SLIDE 21

Language of Equality, cfgbal

cfgbal

1: S → ε | 0S1S | 1S0S

A derivation of 0110 in cfgbal (each step is called an inference).

S

1:

⇒ 0S1S

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 7 / 16 A CFG for English →

slide-22
SLIDE 22

Language of Equality, cfgbal

cfgbal

1: S → ε | 0S1S | 1S0S

A derivation of 0110 in cfgbal (each step is called an inference).

S

1:

⇒ 0S1S

1:

⇒ 0S11S0S

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 7 / 16 A CFG for English →

slide-23
SLIDE 23

Language of Equality, cfgbal

cfgbal

1: S → ε | 0S1S | 1S0S

A derivation of 0110 in cfgbal (each step is called an inference).

S

1:

⇒ 0S1S

1:

⇒ 0S11S0S

1:

⇒ 0ε11S0S

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 7 / 16 A CFG for English →

slide-24
SLIDE 24

Language of Equality, cfgbal

cfgbal

1: S → ε | 0S1S | 1S0S

A derivation of 0110 in cfgbal (each step is called an inference).

S

1:

⇒ 0S1S

1:

⇒ 0S11S0S

1:

⇒ 0ε11S0S

⇒ 0110

Notation:

S ∗ ⇒ 0110

( ∗ ⇒ means “A derivation starting from S yields 0110”)

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 7 / 16 A CFG for English →

slide-25
SLIDE 25

Language of Equality, cfgbal

cfgbal

1: S → ε | 0S1S | 1S0S

A derivation of 0110 in cfgbal (each step is called an inference).

S

1:

⇒ 0S1S

1:

⇒ 0S11S0S

1:

⇒ 0ε11S0S

⇒ 0110

Notation:

S ∗ ⇒ 0110

( ∗ ⇒ means “A derivation starting from S yields 0110”)

Distinguish S from a mathematical variable (e.g. x),

0S1S

versus

0x1x

Two S’s are replaced independently. Two x’s must be the same, e.g. x = 11.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 7 / 16 A CFG for English →

slide-26
SLIDE 26

Language of Equality, cfgbal

cfgbal

1: S → ε | 0S1S | 1S0S

A derivation of 0110 in cfgbal (each step is called an inference).

S

1:

⇒ 0S1S

1:

⇒ 0S11S0S

1:

⇒ 0ε11S0S

⇒ 0110

Notation:

S ∗ ⇒ 0110

( ∗ ⇒ means “A derivation starting from S yields 0110”)

Distinguish S from a mathematical variable (e.g. x),

0S1S

versus

0x1x

Two S’s are replaced independently. Two x’s must be the same, e.g. x = 11.

Pop Quiz. Determine if each string can be generated and if so, give a derivation.

(a)

0011

(b)

0110

(c)

00011

(d)

010101 Give an informal description for the CFL of this CFG.

1: S → ε | T0T1 | T0A 2: X → T0T1 | T0A 3: A → XT1 4: T0 → 0 5: T1 → 1

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 7 / 16 A CFG for English →

slide-27
SLIDE 27

A CFG for English

1:

S → <phrase><verb>

2:

<phrase> → <article><noun>

3: <article> → A␣ | The␣ 4:

<noun> → cat␣ | dog␣

5:

<verb> → walks. | runs. | walks.␣S | runs.␣S

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 8 / 16 A CFG for Programming →

slide-28
SLIDE 28

A CFG for English

1:

S → <phrase><verb>

2:

<phrase> → <article><noun>

3: <article> → A␣ | The␣ 4:

<noun> → cat␣ | dog␣

5:

<verb> → walks. | runs. | walks.␣S | runs.␣S

S

1:

⇒ <phrase><verb>

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 8 / 16 A CFG for Programming →

slide-29
SLIDE 29

A CFG for English

1:

S → <phrase><verb>

2:

<phrase> → <article><noun>

3: <article> → A␣ | The␣ 4:

<noun> → cat␣ | dog␣

5:

<verb> → walks. | runs. | walks.␣S | runs.␣S

S

1:

⇒ <phrase><verb>

5:

⇒ <phrase>walks.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 8 / 16 A CFG for Programming →

slide-30
SLIDE 30

A CFG for English

1:

S → <phrase><verb>

2:

<phrase> → <article><noun>

3: <article> → A␣ | The␣ 4:

<noun> → cat␣ | dog␣

5:

<verb> → walks. | runs. | walks.␣S | runs.␣S

S

1:

⇒ <phrase><verb>

5:

⇒ <phrase>walks.

2:

⇒ <article><noun>walks.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 8 / 16 A CFG for Programming →

slide-31
SLIDE 31

A CFG for English

1:

S → <phrase><verb>

2:

<phrase> → <article><noun>

3: <article> → A␣ | The␣ 4:

<noun> → cat␣ | dog␣

5:

<verb> → walks. | runs. | walks.␣S | runs.␣S

S

1:

⇒ <phrase><verb>

5:

⇒ <phrase>walks.

2:

⇒ <article><noun>walks.

3:

⇒ A␣<noun>walks.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 8 / 16 A CFG for Programming →

slide-32
SLIDE 32

A CFG for English

1:

S → <phrase><verb>

2:

<phrase> → <article><noun>

3: <article> → A␣ | The␣ 4:

<noun> → cat␣ | dog␣

5:

<verb> → walks. | runs. | walks.␣S | runs.␣S

S

1:

⇒ <phrase><verb>

5:

⇒ <phrase>walks.

2:

⇒ <article><noun>walks.

3:

⇒ A␣<noun>walks.

4:

⇒ A␣cat␣walks.

Pop Quiz. Give a derivation for: A␣cat␣runs.␣The␣dog␣walks.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 8 / 16 A CFG for Programming →

slide-33
SLIDE 33

A CFG for Programming

1:

S → <stmt>;S | <stmt>;

2:

<stmt> → <assign> | <declare>

3:

<declare> → int␣<variable>

4:

<assign> → <variable>=<integer>

5:

<integer> → <integer><digit> | <digit>

6:

<digit> → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

7: <variable> → x | x<variable>

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 9 / 16 Constructing a CFG →

slide-34
SLIDE 34

A CFG for Programming

1:

S → <stmt>;S | <stmt>;

2:

<stmt> → <assign> | <declare>

3:

<declare> → int␣<variable>

4:

<assign> → <variable>=<integer>

5:

<integer> → <integer><digit> | <digit>

6:

<digit> → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

7: <variable> → x | x<variable>

Pop Quiz. Give derivations for these snippets of code.

(a)

int␣x;int␣xx;x=22;xx=8;

(b)

x=8;int␣x;

(c)

int␣x;xx=8;

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 9 / 16 Constructing a CFG →

slide-35
SLIDE 35

Constructing a CFG to Solve a Problem

Lbal = {strings with an equal number of 1’s and 0’s}. 001011010110

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 10 / 16 Proving a CFG Solves a Problem →

slide-36
SLIDE 36

Constructing a CFG to Solve a Problem

Lbal = {strings with an equal number of 1’s and 0’s}. 001011010110 = 0 1

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 10 / 16 Proving a CFG Solves a Problem →

slide-37
SLIDE 37

Constructing a CFG to Solve a Problem

Lbal = {strings with an equal number of 1’s and 0’s}. 001011010110 = 0 • 0101

in Lbal

  • 1 • 010110

in Lbal

= 0S1S

(S represents “a string in Lbal”)

Every large string in Lbal can be obtained (recursively) from smaller ones.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 10 / 16 Proving a CFG Solves a Problem →

slide-38
SLIDE 38

Constructing a CFG to Solve a Problem

Lbal = {strings with an equal number of 1’s and 0’s}. 001011010110 = 0 • 0101

in Lbal

  • 1 • 010110

in Lbal

= 0S1S

(S represents “a string in Lbal”)

Every large string in Lbal can be obtained (recursively) from smaller ones.

S → ε | 0S1S | 1S0S.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 10 / 16 Proving a CFG Solves a Problem →

slide-39
SLIDE 39

Constructing a CFG to Solve a Problem

Lbal = {strings with an equal number of 1’s and 0’s}. 001011010110 = 0 • 0101

in Lbal

  • 1 • 010110

in Lbal

= 0S1S

(S represents “a string in Lbal”)

Every large string in Lbal can be obtained (recursively) from smaller ones.

S → ε | 0S1S | 1S0S.

We must prove that:

(i)

Every string generated by this CFG is in Lbal?

(ii)

Every string in Lbal can be derived by this grammar?

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 10 / 16 Proving a CFG Solves a Problem →

slide-40
SLIDE 40

Proving a CFG Solves a Problem

Lbal = {strings with an equal number of 1’s and 0’s} S → ε | 0S1S | 1S0S

(i) Every derivation in cfgbal generates a string in Lbal. (ii) Every string in Lbal can be derived within cfgbal.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 11 / 16 Union, Concatenation, Kleene-star →

slide-41
SLIDE 41

Proving a CFG Solves a Problem

Lbal = {strings with an equal number of 1’s and 0’s} S → ε | 0S1S | 1S0S

(i) Every derivation in cfgbal generates a string in Lbal.

Strong induction on the length of the derivation (number of production rules invoked). Base Case. length-1 derivation gives ε.

(ii) Every string in Lbal can be derived within cfgbal.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 11 / 16 Union, Concatenation, Kleene-star →

slide-42
SLIDE 42

Proving a CFG Solves a Problem

Lbal = {strings with an equal number of 1’s and 0’s} S → ε | 0S1S | 1S0S

(i) Every derivation in cfgbal generates a string in Lbal.

Strong induction on the length of the derivation (number of production rules invoked). Base Case. length-1 derivation gives ε.

  • Induction. The derivation starts in one of two ways:

S → 0 S

⇓∗ x

1 S

⇓∗ y

→ · · ·

  • r

S → 1 S

⇓∗ x

0 S

⇓∗ y

→ · · · The derivations of x and y are shorter.

(ii) Every string in Lbal can be derived within cfgbal.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 11 / 16 Union, Concatenation, Kleene-star →

slide-43
SLIDE 43

Proving a CFG Solves a Problem

Lbal = {strings with an equal number of 1’s and 0’s} S → ε | 0S1S | 1S0S

(i) Every derivation in cfgbal generates a string in Lbal.

Strong induction on the length of the derivation (number of production rules invoked). Base Case. length-1 derivation gives ε.

  • Induction. The derivation starts in one of two ways:

S → 0 S

⇓∗ x

1 S

⇓∗ y

→ · · ·

  • r

S → 1 S

⇓∗ x

0 S

⇓∗ y

→ · · · The derivations of x and y are shorter. By the induction hypothesis, x, y ∈ Lbal, so the final strings are in Lbal.

(ii) Every string in Lbal can be derived within cfgbal.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 11 / 16 Union, Concatenation, Kleene-star →

slide-44
SLIDE 44

Proving a CFG Solves a Problem

Lbal = {strings with an equal number of 1’s and 0’s} S → ε | 0S1S | 1S0S

(i) Every derivation in cfgbal generates a string in Lbal.

Strong induction on the length of the derivation (number of production rules invoked). Base Case. length-1 derivation gives ε.

  • Induction. The derivation starts in one of two ways:

S → 0 S

⇓∗ x

1 S

⇓∗ y

→ · · ·

  • r

S → 1 S

⇓∗ x

0 S

⇓∗ y

→ · · · The derivations of x and y are shorter. By the induction hypothesis, x, y ∈ Lbal, so the final strings are in Lbal.

(ii) Every string in Lbal can be derived within cfgbal.

Strong induction on the length of the string. Base case: length-1 string, ε.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 11 / 16 Union, Concatenation, Kleene-star →

slide-45
SLIDE 45

Proving a CFG Solves a Problem

Lbal = {strings with an equal number of 1’s and 0’s} S → ε | 0S1S | 1S0S

(i) Every derivation in cfgbal generates a string in Lbal.

Strong induction on the length of the derivation (number of production rules invoked). Base Case. length-1 derivation gives ε.

  • Induction. The derivation starts in one of two ways:

S → 0 S

⇓∗ x

1 S

⇓∗ y

→ · · ·

  • r

S → 1 S

⇓∗ x

0 S

⇓∗ y

→ · · · The derivations of x and y are shorter. By the induction hypothesis, x, y ∈ Lbal, so the final strings are in Lbal.

(ii) Every string in Lbal can be derived within cfgbal.

Strong induction on the length of the string. Base case: length-1 string, ε.

  • Induction. Any string w in Lbal has one of two forms:

w = 0w11w2

  • r

w = 1w10w2, where w1, w2 ∈ Lbal and have smaller length.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 11 / 16 Union, Concatenation, Kleene-star →

slide-46
SLIDE 46

Proving a CFG Solves a Problem

Lbal = {strings with an equal number of 1’s and 0’s} S → ε | 0S1S | 1S0S

(i) Every derivation in cfgbal generates a string in Lbal.

Strong induction on the length of the derivation (number of production rules invoked). Base Case. length-1 derivation gives ε.

  • Induction. The derivation starts in one of two ways:

S → 0 S

⇓∗ x

1 S

⇓∗ y

→ · · ·

  • r

S → 1 S

⇓∗ x

0 S

⇓∗ y

→ · · · The derivations of x and y are shorter. By the induction hypothesis, x, y ∈ Lbal, so the final strings are in Lbal.

(ii) Every string in Lbal can be derived within cfgbal.

Strong induction on the length of the string. Base case: length-1 string, ε.

  • Induction. Any string w in Lbal has one of two forms:

w = 0w11w2

  • r

w = 1w10w2, where w1, w2 ∈ Lbal and have smaller length. By the induction hypothesis, S ∗ ⇒ w1 and S ∗ ⇒ w2, so S ∗ ⇒ w.

  • Practice. Exercise 25.5.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 11 / 16 Union, Concatenation, Kleene-star →

slide-47
SLIDE 47

Union, Concatenation, Kleene-star

L1 = {0•n1•n | n ≥ 0} L2 = {1•n0•n | n ≥ 0} A → ε | 0A1 B → ε | 1B0

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 12 / 16 Parse Trees →

slide-48
SLIDE 48

Union, Concatenation, Kleene-star

L1 = {0•n1•n | n ≥ 0} L2 = {1•n0•n | n ≥ 0} A → ε | 0A1 B → ε | 1B0 L1 ∪ L2 :

1: S → A | B

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 12 / 16 Parse Trees →

slide-49
SLIDE 49

Union, Concatenation, Kleene-star

L1 = {0•n1•n | n ≥ 0} L2 = {1•n0•n | n ≥ 0} A → ε | 0A1 B → ε | 1B0 L1 ∪ L2 :

1: S → A | B 2: A → ε | 0A1 3: B → ε | 1B0

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 12 / 16 Parse Trees →

slide-50
SLIDE 50

Union, Concatenation, Kleene-star

L1 = {0•n1•n | n ≥ 0} L2 = {1•n0•n | n ≥ 0} A → ε | 0A1 B → ε | 1B0 L1 ∪ L2 :

1: S → A | B 2: A → ε | 0A1 3: B → ε | 1B0

L1 •L2 :

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 12 / 16 Parse Trees →

slide-51
SLIDE 51

Union, Concatenation, Kleene-star

L1 = {0•n1•n | n ≥ 0} L2 = {1•n0•n | n ≥ 0} A → ε | 0A1 B → ε | 1B0 L1 ∪ L2 :

1: S → A | B 2: A → ε | 0A1 3: B → ε | 1B0

L1 •L2 :

2: A → ε | 0A1 3: B → ε | 1B0

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 12 / 16 Parse Trees →

slide-52
SLIDE 52

Union, Concatenation, Kleene-star

L1 = {0•n1•n | n ≥ 0} L2 = {1•n0•n | n ≥ 0} A → ε | 0A1 B → ε | 1B0 L1 ∪ L2 :

1: S → A | B 2: A → ε | 0A1 3: B → ε | 1B0

L1 •L2 :

2: A → ε | 0A1 3: B → ε | 1B0

Kleene-star. L∗

1 is generated by the CFG 1:S → ε | SA

← generates A•i

2:A → ε | 0A1

← each A becomes a 0•n1•n

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 12 / 16 Parse Trees →

slide-53
SLIDE 53

Union, Concatenation, Kleene-star

L1 = {0•n1•n | n ≥ 0} L2 = {1•n0•n | n ≥ 0} A → ε | 0A1 B → ε | 1B0 L1 ∪ L2 :

1: S → A | B 2: A → ε | 0A1 3: B → ε | 1B0

L1 •L2 :

2: A → ε | 0A1 3: B → ε | 1B0

Kleene-star. L∗

1 is generated by the CFG 1:S → ε | SA

← generates A•i

2:A → ε | 0A1

← each A becomes a 0•n1•n

Example 25.2. CFGs can implement DFAs, and so are strictly more powerful.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 12 / 16 Parse Trees →

slide-54
SLIDE 54

Union, Concatenation, Kleene-star

L1 = {0•n1•n | n ≥ 0} L2 = {1•n0•n | n ≥ 0} A → ε | 0A1 B → ε | 1B0 L1 ∪ L2 :

1: S → A | B 2: A → ε | 0A1 3: B → ε | 1B0

L1 •L2 :

2: A → ε | 0A1 3: B → ε | 1B0

Kleene-star. L∗

1 is generated by the CFG 1:S → ε | SA

← generates A•i

2:A → ε | 0A1

← each A becomes a 0•n1•n

Example 25.2. CFGs can implement DFAs, and so are strictly more powerful.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 12 / 16 Parse Trees →

slide-55
SLIDE 55

Union, Concatenation, Kleene-star

L1 = {0•n1•n | n ≥ 0} L2 = {1•n0•n | n ≥ 0} A → ε | 0A1 B → ε | 1B0 L1 ∪ L2 :

1: S → A | B 2: A → ε | 0A1 3: B → ε | 1B0

L1 •L2 :

1: S → AB 2: A → ε | 0A1 3: B → ε | 1B0

Kleene-star. L∗

1 is generated by the CFG 1:S → ε | SA

← generates A•i

2:A → ε | 0A1

← each A becomes a 0•n1•n

Example 25.2. CFGs can implement DFAs, and so are strictly more powerful.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 12 / 16 Parse Trees →

slide-56
SLIDE 56

Parse Trees

S → # | 0S1

Here is a derivation of 000#111,

S ⇒ 0S1 ⇒ 00S11 ⇒ 000S111 ⇒ 000#111

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 13 / 16 CFG for Arithmetic →

slide-57
SLIDE 57

Parse Trees

S → # | 0S1

Here is a derivation of 000#111,

S ⇒ 0S1 ⇒ 00S11 ⇒ 000S111 ⇒ 000#111

The parse tree gives more information than a derivation

S

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 13 / 16 CFG for Arithmetic →

slide-58
SLIDE 58

Parse Trees

S → # | 0S1

Here is a derivation of 000#111,

S ⇒ 0S1 ⇒ 00S11 ⇒ 000S111 ⇒ 000#111

The parse tree gives more information than a derivation

S

S

S 1

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 13 / 16 CFG for Arithmetic →

slide-59
SLIDE 59

Parse Trees

S → # | 0S1

Here is a derivation of 000#111,

S ⇒ 0S1 ⇒ 00S11 ⇒ 000S111 ⇒ 000#111

The parse tree gives more information than a derivation

S

S

S 1 S

S 1 S 1

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 13 / 16 CFG for Arithmetic →

slide-60
SLIDE 60

Parse Trees

S → # | 0S1

Here is a derivation of 000#111,

S ⇒ 0S1 ⇒ 00S11 ⇒ 000S111 ⇒ 000#111

The parse tree gives more information than a derivation

S

S

S 1 S

S 1 S 1 S

S 1 S 1 S 1

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 13 / 16 CFG for Arithmetic →

slide-61
SLIDE 61

Parse Trees

S → # | 0S1

Here is a derivation of 000#111,

S ⇒ 0S1 ⇒ 00S11 ⇒ 000S111 ⇒ 000#111

The parse tree gives more information than a derivation

S

S

S 1 S

S 1 S 1 S

S 1 S 1 S 1 S S 1 S 1 S 1 #

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 13 / 16 CFG for Arithmetic →

slide-62
SLIDE 62

Parse Trees

S → # | 0S1

Here is a derivation of 000#111,

S ⇒ 0S1 ⇒ 00S11 ⇒ 000S111 ⇒ 000#111

The parse tree gives more information than a derivation

S

S

S 1 S

S 1 S 1 S

S 1 S 1 S 1 S S 1 S 1 S 1 # S S 1 S 1 S 1 #

Clearly shows how a substring belongs to the language of its parent variable.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 13 / 16 CFG for Arithmetic →

slide-63
SLIDE 63

CFG for Arithmetic

S → S + S | S × S | (S) | 2

(the terminals are +, ×, (, ) and 2)

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 14 / 16 Pushdown Automata →

slide-64
SLIDE 64

CFG for Arithmetic

S → S + S | S × S | (S) | 2

(the terminals are +, ×, (, ) and 2) Two derivations of 2 + 2 × 2 along with parse trees,

S S

S S

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 14 / 16 Pushdown Automata →

slide-65
SLIDE 65

CFG for Arithmetic

S → S + S | S × S | (S) | 2

(the terminals are +, ×, (, ) and 2) Two derivations of 2 + 2 × 2 along with parse trees,

S ⇒ S + S S ⇒ S × S

S S + S S S × S

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 14 / 16 Pushdown Automata →

slide-66
SLIDE 66

CFG for Arithmetic

S → S + S | S × S | (S) | 2

(the terminals are +, ×, (, ) and 2) Two derivations of 2 + 2 × 2 along with parse trees,

S ⇒ S + S ⇒ S + S × S S ⇒ S × S ⇒ S + S × S

S S + S S × S S S × S S + S

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 14 / 16 Pushdown Automata →

slide-67
SLIDE 67

CFG for Arithmetic

S → S + S | S × S | (S) | 2

(the terminals are +, ×, (, ) and 2) Two derivations of 2 + 2 × 2 along with parse trees,

S ⇒ S + S ⇒ S + S × S

⇒ 2 + 2 × 2 S ⇒ S × S ⇒ S + S × S

⇒ 2 + 2 × 2

S S + S S × S 2 2 2 S S × S S + S 2 2 2

(multiply 2 × 2 and add to 2) (add 2 + 2 and multiply by 2)

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 14 / 16 Pushdown Automata →

slide-68
SLIDE 68

CFG for Arithmetic

S → S + S | S × S | (S) | 2

(the terminals are +, ×, (, ) and 2) Two derivations of 2 + 2 × 2 along with parse trees,

S ⇒ S + S ⇒ S + S × S

⇒ 2 + 2 × 2 S ⇒ S × S ⇒ S + S × S

⇒ 2 + 2 × 2

S S + S S × S 2 2 2 S S × S S + S 2 2 2

(multiply 2 × 2 and add to 2) (add 2 + 2 and multiply by 2)

Parse tree ↔ How you interpret the string. Different parse trees ↔ different meanings. BAD! We want unambiguous meaning

programs, html-code, math, English, . . .

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 14 / 16 Pushdown Automata →

slide-69
SLIDE 69

CFG for Arithmetic

S → S + S | S × S | (S) | 2

(the terminals are +, ×, (, ) and 2) Two derivations of 2 + 2 × 2 along with parse trees,

S ⇒ S + S ⇒ S + S × S

⇒ 2 + 2 × 2 S ⇒ S × S ⇒ S + S × S

⇒ 2 + 2 × 2

S S + S S × S 2 2 2 S S × S S + S 2 2 2

(multiply 2 × 2 and add to 2) (add 2 + 2 and multiply by 2)

Parse tree ↔ How you interpret the string. Different parse trees ↔ different meanings. BAD! We want unambiguous meaning

programs, html-code, math, English, . . .

Unambiguous grammer

1: S

→ P | S + P

2: P

→ T | P × T

3: T

→ 2 | (S)

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 14 / 16 Pushdown Automata →

slide-70
SLIDE 70

Pushdown Automata: DFAs with Stack Memory

L = {w#wr | w ∈ {0, 1}∗} S → # | 0S0 | 1S1

q0 q7 q6 q5 q4 q3 q2 q1

yes

  • r

no

1 1 0 # 0 1 1

DFA with stack memory (push, pop, read).

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 15 / 16 Non Context Free →

slide-71
SLIDE 71

Pushdown Automata: DFAs with Stack Memory

L = {w#wr | w ∈ {0, 1}∗} S → # | 0S0 | 1S1

q0 q7 q6 q5 q4 q3 q2 q1

yes

  • r

no

1 1 0 # 0 1 1

DFA with stack memory (push, pop, read). Push the first half of the string (before #). For each bit in the second half, pop the stack and compare.

DFAs with stack memory closely related to CFGs.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 15 / 16 Non Context Free →

slide-72
SLIDE 72

Non Context Free

{w#w}

repetition

{0•n1•n0•n}

multiple-equality

{0•n2}, {0•n1•n2}

squaring

{0•2n}, {0•n1•2n}

exponentiation

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 16 / 16

slide-73
SLIDE 73

Non Context Free

{w#w}

repetition

{0•n1•n0•n}

multiple-equality

{0•n2}, {0•n1•n2}

squaring

{0•2n}, {0•n1•2n}

exponentiation

w#wr w#w 0•n1•n0•n

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 16 / 16

slide-74
SLIDE 74

Non Context Free

{w#w}

repetition

{0•n1•n0•n}

multiple-equality

{0•n2}, {0•n1•n2}

squaring

{0•2n}, {0•n1•2n}

exponentiation

w#wr w#w 0•n1•n0•n

1 1

0 0 1 1

0011 is pushed.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 16 / 16

slide-75
SLIDE 75

Non Context Free

{w#w}

repetition

{0•n1•n0•n}

multiple-equality

{0•n2}, {0•n1•n2}

squaring

{0•2n}, {0•n1•2n}

exponentiation

w#wr w#w 0•n1•n0•n

1 1

0 0 1 1 0 0 1 1#1 1 0 0

m a t c h ; p

  • p

0011 is pushed. DFA matches 1100 by popping.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 16 / 16

slide-76
SLIDE 76

Non Context Free

{w#w}

repetition

{0•n1•n0•n}

multiple-equality

{0•n2}, {0•n1•n2}

squaring

{0•2n}, {0•n1•2n}

exponentiation

w#wr w#w 0•n1•n0•n

1 1

0 0 1 1 0 0 1 1#1 1 0 0

m a t c h ; p

  • p

1 1

0 0 1 1

0011 is pushed. DFA matches 1100 by popping. 0011 is pushed.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 16 / 16

slide-77
SLIDE 77

Non Context Free

{w#w}

repetition

{0•n1•n0•n}

multiple-equality

{0•n2}, {0•n1•n2}

squaring

{0•2n}, {0•n1•2n}

exponentiation

w#wr w#w 0•n1•n0•n

1 1

0 0 1 1 0 0 1 1#1 1 0 0

m a t c h ; p

  • p

1 1

0 0 1 1 0 0 1 1#0 0 1 1

match; pop

0011 is pushed. DFA matches 1100 by popping. 0011 is pushed. DFA needs bottom-access to match.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 16 / 16

slide-78
SLIDE 78

Non Context Free

{w#w}

repetition

{0•n1•n0•n}

multiple-equality

{0•n2}, {0•n1•n2}

squaring

{0•2n}, {0•n1•2n}

exponentiation

w#wr w#w 0•n1•n0•n

1 1

0 0 1 1 0 0 1 1#1 1 0 0

m a t c h ; p

  • p

1 1

0 0 1 1 0 0 1 1#0 0 1 1

match; pop 1 1 1

0 0 0 1 1 1

0011 is pushed. DFA matches 1100 by popping. 0011 is pushed. DFA needs bottom-access to match. 000111 is pushed.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 16 / 16

slide-79
SLIDE 79

Non Context Free

{w#w}

repetition

{0•n1•n0•n}

multiple-equality

{0•n2}, {0•n1•n2}

squaring

{0•2n}, {0•n1•2n}

exponentiation

w#wr w#w 0•n1•n0•n

1 1

0 0 1 1 0 0 1 1#1 1 0 0

m a t c h ; p

  • p

1 1

0 0 1 1 0 0 1 1#0 0 1 1

match; pop 1 1 1

0 0 0 1 1 1 0 0 0 1 1 1 0 0 0

match; pop

0011 is pushed. DFA matches 1100 by popping. 0011 is pushed. DFA needs bottom-access to match. 000111 is pushed. DFA needs random access to match.

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 16 / 16

slide-80
SLIDE 80

Non Context Free

{w#w}

repetition

{0•n1•n0•n}

multiple-equality

{0•n2}, {0•n1•n2}

squaring

{0•2n}, {0•n1•2n}

exponentiation

w#wr w#w 0•n1•n0•n

1 1

0 0 1 1 0 0 1 1#1 1 0 0

m a t c h ; p

  • p

1 1

0 0 1 1 0 0 1 1#0 0 1 1

match; pop 1 1 1

0 0 0 1 1 1 0 0 0 1 1 1 0 0 0

match; pop

0011 is pushed. DFA matches 1100 by popping. 0011 is pushed. DFA needs bottom-access to match. 000111 is pushed. DFA needs random access to match.

The file clerk who only has access to the top of his stack of papers has fundamentally less power than the file clerk who has a filing cabinet with access to all his papers.

We need a new model, one with Random Access Memory (RAM).

Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 16 / 16