lecture 4 – outline - tau › ~bchor › cm › compute4.pdf · lecture 4 – outline so far we...

99
Lecture 4 – Outline So far we saw Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Upload: others

Post on 23-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Lecture 4 – OutlineSo far we saw

finite automata,regular languages,regular expressions,pumping lemma for regular languages.

Today, we introduce stronger machines andlanguages with more expressive power:

pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 2: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Lecture 4 – OutlineSo far we saw

finite automata,

regular languages,regular expressions,pumping lemma for regular languages.

Today, we introduce stronger machines andlanguages with more expressive power:

pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 3: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Lecture 4 – OutlineSo far we saw

finite automata,regular languages,

regular expressions,pumping lemma for regular languages.

Today, we introduce stronger machines andlanguages with more expressive power:

pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 4: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Lecture 4 – OutlineSo far we saw

finite automata,regular languages,regular expressions,

pumping lemma for regular languages.

Today, we introduce stronger machines andlanguages with more expressive power:

pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 5: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Lecture 4 – OutlineSo far we saw

finite automata,regular languages,regular expressions,pumping lemma for regular languages.

Today, we introduce stronger machines andlanguages with more expressive power:

pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 6: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Lecture 4 – OutlineSo far we saw

finite automata,regular languages,regular expressions,pumping lemma for regular languages.

Today, we introduce stronger machines andlanguages with more expressive power:

pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 7: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Lecture 4 – OutlineSo far we saw

finite automata,regular languages,regular expressions,pumping lemma for regular languages.

Today, we introduce stronger machines andlanguages with more expressive power:

pushdown automata,

context-free languages,context-free grammars,pumping lemma for context-free languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 8: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Lecture 4 – OutlineSo far we saw

finite automata,regular languages,regular expressions,pumping lemma for regular languages.

Today, we introduce stronger machines andlanguages with more expressive power:

pushdown automata,context-free languages,

context-free grammars,pumping lemma for context-free languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 9: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Lecture 4 – OutlineSo far we saw

finite automata,regular languages,regular expressions,pumping lemma for regular languages.

Today, we introduce stronger machines andlanguages with more expressive power:

pushdown automata,context-free languages,context-free grammars,

pumping lemma for context-free languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 10: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Lecture 4 – OutlineSo far we saw

finite automata,regular languages,regular expressions,pumping lemma for regular languages.

Today, we introduce stronger machines andlanguages with more expressive power:

pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 11: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Context-Free GrammarsThis is an example of a context free grammer, G1:

A → 0A1

A → B

B → #

Terminology:

Each line is a substitution rule or production.

Each rule has the form: symbol → string.The left-hand symbol is a variable(usually upper-case).

A string consists of variables and terminals.

One variable is the start variable.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.2

Page 12: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Context-Free GrammarsThis is an example of a context free grammer, G1:

A → 0A1

A → B

B → #

Terminology:

Each line is a substitution rule or production.

Each rule has the form: symbol → string.The left-hand symbol is a variable(usually upper-case).

A string consists of variables and terminals.

One variable is the start variable.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.2

Page 13: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Context-Free GrammarsThis is an example of a context free grammer, G1:

A → 0A1

A → B

B → #

Terminology:

Each line is a substitution rule or production.

Each rule has the form: symbol → string.The left-hand symbol is a variable(usually upper-case).

A string consists of variables and terminals.

One variable is the start variable.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.2

Page 14: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Context-Free GrammarsThis is an example of a context free grammer, G1:

A → 0A1

A → B

B → #

Terminology:

Each line is a substitution rule or production.

Each rule has the form: symbol → string.The left-hand symbol is a variable(usually upper-case).

A string consists of variables and terminals.

One variable is the start variable.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.2

Page 15: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Rules for Generating Strings

Write down the start variable (lhs of top rule).

Pick a variable written down in current string anda derivation that starts with that variable.

Replace that variable with right-hand side of thatderivation.

Repeat until no variables remain.

Return final string (concatenation of terminals).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3

Page 16: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Rules for Generating Strings

Write down the start variable (lhs of top rule).

Pick a variable written down in current string anda derivation that starts with that variable.

Replace that variable with right-hand side of thatderivation.

Repeat until no variables remain.

Return final string (concatenation of terminals).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3

Page 17: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Rules for Generating Strings

Write down the start variable (lhs of top rule).

Pick a variable written down in current string anda derivation that starts with that variable.

Replace that variable with right-hand side of thatderivation.

Repeat until no variables remain.

Return final string (concatenation of terminals).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3

Page 18: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Rules for Generating Strings

Write down the start variable (lhs of top rule).

Pick a variable written down in current string anda derivation that starts with that variable.

Replace that variable with right-hand side of thatderivation.

Repeat until no variables remain.

Return final string (concatenation of terminals).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3

Page 19: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Rules for Generating Strings

Write down the start variable (lhs of top rule).

Pick a variable written down in current string anda derivation that starts with that variable.

Replace that variable with right-hand side of thatderivation.

Repeat until no variables remain.

Return final string (concatenation of terminals).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3

Page 20: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Example

Grammar G1:

A → 0A1

A → B

B → #

Derivation with G1:

A ⇒ 0A1

⇒ 00A11

⇒ 000A111

⇒ 000B111

⇒ 000#111

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.4

Page 21: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Example

Grammar G1:

A → 0A1

A → B

B → #

Derivation with G1:

A ⇒ 0A1

⇒ 00A11

⇒ 000A111

⇒ 000B111

⇒ 000#111

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.4

Page 22: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

A Parse TreeA

A

A

B

#0 10 0 1 1

Question: What strings can be generated in this wayfrom the grammar G1?

Answer: Exactly those of the form 0n#1n (n ≥ 0).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.5

Page 23: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

A Parse TreeA

A

A

B

#0 10 0 1 1

Question: What strings can be generated in this wayfrom the grammar G1?

Answer: Exactly those of the form 0n#1n (n ≥ 0).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.5

Page 24: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

A Parse TreeA

A

A

B

#0 10 0 1 1

Question: What strings can be generated in this wayfrom the grammar G1?

Answer: Exactly those of the form 0n#1n (n ≥ 0).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.5

Page 25: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Context-Free Languages

The language generated in this way is the language ofthe grammar.

For example, L(G1) is {0n#1n|n ≥ 0}.

Any language generated by a context-free grammar is

called a context-free language.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.6

Page 26: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Context-Free Languages

The language generated in this way is the language ofthe grammar.

For example, L(G1) is {0n#1n|n ≥ 0}.

Any language generated by a context-free grammar is

called a context-free language.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.6

Page 27: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Context-Free Languages

The language generated in this way is the language ofthe grammar.

For example, L(G1) is {0n#1n|n ≥ 0}.

Any language generated by a context-free grammar is

called a context-free language.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.6

Page 28: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

A Useful AbbreviationRules with same variable on left hand side

A → 0A1

A → B

are written as:

A → 0A1 | B

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.7

Page 29: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

A Useful AbbreviationRules with same variable on left hand side

A → 0A1

A → B

are written as:

A → 0A1 | B

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.7

Page 30: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

English-like Sentences

A grammar G2 to describe a few English sentences:

< SENTENCE > → < NOUN-PHRASE >< VERB >

< NOUN-PHRASE > → < ARTICLE >< NOUN >

< NOUN > → boy | girl | flower

< ARTICLE > → a | the

< VERB > → touches | likes | sees

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.8

Page 31: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Deriving English-like Sentences

A specific derivation in G2:

< SENTENCE > ⇒ < NOUN-PHRASE >< VERB >

⇒ < ARTICLE >< NOUN >< VERB >

⇒ a < NOUN >< VERB >

⇒ a boy < VERB >

⇒ a boy sees

More strings in G2:

a flower sees

the girl touches

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.9

Page 32: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Deriving English-like Sentences

A specific derivation in G2:

< SENTENCE > ⇒ < NOUN-PHRASE >< VERB >

⇒ < ARTICLE >< NOUN >< VERB >

⇒ a < NOUN >< VERB >

⇒ a boy < VERB >

⇒ a boy sees

More strings in G2:

a flower sees

the girl touches

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.9

Page 33: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Derivation and Parse Tree

< SENTENCE > ⇒ < NOUN-PHRASE >< VERB >

⇒ < ARTICLE >< NOUN >< VERB >

⇒ a < NOUN >< VERB >

⇒ a boy < VERB >

⇒ a boy sees

SENTENCE

NOUN-PHRASE VERB

ARTICLE NOUN

a boy seesSlides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.10

Page 34: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Formal DefinitionsA context-free grammar is a 4-tuple (V, Σ, R, S)where

V is a finite set of variables,

Σ is a finite set of terminals,

R is a finite set of rules: each rule is a variableand a finite string of variables and terminals.

S is the start symbol.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11

Page 35: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Formal DefinitionsA context-free grammar is a 4-tuple (V, Σ, R, S)where

V is a finite set of variables,

Σ is a finite set of terminals,

R is a finite set of rules: each rule is a variableand a finite string of variables and terminals.

S is the start symbol.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11

Page 36: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Formal DefinitionsA context-free grammar is a 4-tuple (V, Σ, R, S)where

V is a finite set of variables,

Σ is a finite set of terminals,

R is a finite set of rules: each rule is a variableand a finite string of variables and terminals.

S is the start symbol.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11

Page 37: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Formal DefinitionsA context-free grammar is a 4-tuple (V, Σ, R, S)where

V is a finite set of variables,

Σ is a finite set of terminals,

R is a finite set of rules: each rule is a variableand a finite string of variables and terminals.

S is the start symbol.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11

Page 38: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Formal Definitions

If u and v are strings of variables and terminals,

and A → w is a rule of the grammar, then

we say uAv yields uwv, written uAv ⇒ uwv.

We write u∗⇒ v if u = v or

u ⇒ u1 ⇒ . . . ⇒ uk ⇒ v.

for some sequence u1, u2, . . . , uk.

Definition: The language of the grammar is{

w | S ∗⇒ w}

.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12

Page 39: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Formal Definitions

If u and v are strings of variables and terminals,

and A → w is a rule of the grammar, then

we say uAv yields uwv, written uAv ⇒ uwv.

We write u∗⇒ v if u = v or

u ⇒ u1 ⇒ . . . ⇒ uk ⇒ v.

for some sequence u1, u2, . . . , uk.

Definition: The language of the grammar is{

w | S ∗⇒ w}

.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12

Page 40: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Formal Definitions

If u and v are strings of variables and terminals,

and A → w is a rule of the grammar, then

we say uAv yields uwv, written uAv ⇒ uwv.

We write u∗⇒ v if u = v or

u ⇒ u1 ⇒ . . . ⇒ uk ⇒ v.

for some sequence u1, u2, . . . , uk.

Definition: The language of the grammar is{

w | S ∗⇒ w}

.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12

Page 41: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Formal Definitions

If u and v are strings of variables and terminals,

and A → w is a rule of the grammar, then

we say uAv yields uwv, written uAv ⇒ uwv.

We write u∗⇒ v if u = v or

u ⇒ u1 ⇒ . . . ⇒ uk ⇒ v.

for some sequence u1, u2, . . . , uk.

Definition: The language of the grammar is{

w | S ∗⇒ w}

.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12

Page 42: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Formal Definitions

If u and v are strings of variables and terminals,

and A → w is a rule of the grammar, then

we say uAv yields uwv, written uAv ⇒ uwv.

We write u∗⇒ v if u = v or

u ⇒ u1 ⇒ . . . ⇒ uk ⇒ v.

for some sequence u1, u2, . . . , uk.

Definition: The language of the grammar is{

w | S ∗⇒ w}

.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12

Page 43: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Example

Consider G4 = (V, {a, b} , R, S).

R (Rules): S → aSb | SS | ε .

Some words in the language: aabb, aababb.

Q.: But what is this language?

Hint: Think of parentheses.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.13

Page 44: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Example

Consider G4 = (V, {a, b} , R, S).

R (Rules): S → aSb | SS | ε .

Some words in the language: aabb, aababb.

Q.: But what is this language?

Hint: Think of parentheses.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.13

Page 45: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Example

Consider G4 = (V, {a, b} , R, S).

R (Rules): S → aSb | SS | ε .

Some words in the language: aabb, aababb.

Q.: But what is this language?

Hint: Think of parentheses.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.13

Page 46: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Example

Consider G4 = (V, {a, b} , R, S).

R (Rules): S → aSb | SS | ε .

Some words in the language: aabb, aababb.

Q.: But what is this language?

Hint: Think of parentheses.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.13

Page 47: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Arythmetic Example

Consider (V, Σ, R,E) where

V = {E, T, F}Σ = {a, +,×, (, )}

Rules:E → E + T | TT → T × F | FF → (E) | a

Strings generated by the grammer:a + a× a and (a + a)× a.

What is the language of this grammer?Hint: arithmetic expressions.

E = expression, T = term, F = factor.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.14

Page 48: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Arythmetic Example

Consider (V, Σ, R,E) where

V = {E, T, F}Σ = {a, +,×, (, )}

Rules:E → E + T | TT → T × F | FF → (E) | a

Strings generated by the grammer:a + a× a and (a + a)× a.

What is the language of this grammer?Hint: arithmetic expressions.

E = expression, T = term, F = factor.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.14

Page 49: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Arythmetic Example

Consider (V, Σ, R,E) where

V = {E, T, F}Σ = {a, +,×, (, )}

Rules:E → E + T | TT → T × F | FF → (E) | a

Strings generated by the grammer:a + a× a and (a + a)× a.

What is the language of this grammer?Hint: arithmetic expressions.

E = expression, T = term, F = factor.Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.14

Page 50: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Parse Tree for a + a× a

E → E + T |TT → T × F | FF → (E) | a

aXa+a

FFF

T T

TE

E

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.15

Page 51: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Parse Tree for a + a× a

E → E + T |TT → T × F | FF → (E) | a

aXa+a

FFF

T T

TE

E

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.15

Page 52: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Parse Tree for (a + a)× a

E → E + T | TT → T × F | FF → (E) | a

( a + aX)a

F F F

T T

E

E

F

T

T

E

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.16

Page 53: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Parse Tree for (a + a)× a

E → E + T | TT → T × F | FF → (E) | a

( a + aX)a

F F F

T T

E

E

F

T

T

E

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.16

Page 54: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Designing Context-Free Grammars

No recipe in general, but few rules-of-thumb

If CFG is the union of several CFGs, renamevariables (not terminals) so they are disjoint, andadd new rule S → S1 | S2 | . . . | Si.

To construct CFG for a regular language,“follow” a DFA for the language. For initial stateq0, make R0 the start variable. For state transitionδ(qi, a) = qj add rule Ri → aRj to grammer. Foreach final state qf , add rule Rf → ε to grammer.

For languages with linked substrings (like{0n#1n|n ≥ 0} ), a rule of form R → uRv maybe helpful, forcing desired relation betweensubstrings.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.17

Page 55: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Designing Context-Free Grammars

No recipe in general, but few rules-of-thumb

If CFG is the union of several CFGs, renamevariables (not terminals) so they are disjoint, andadd new rule S → S1 | S2 | . . . | Si.

To construct CFG for a regular language,“follow” a DFA for the language. For initial stateq0, make R0 the start variable. For state transitionδ(qi, a) = qj add rule Ri → aRj to grammer. Foreach final state qf , add rule Rf → ε to grammer.

For languages with linked substrings (like{0n#1n|n ≥ 0} ), a rule of form R → uRv maybe helpful, forcing desired relation betweensubstrings.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.17

Page 56: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Designing Context-Free Grammars

No recipe in general, but few rules-of-thumb

If CFG is the union of several CFGs, renamevariables (not terminals) so they are disjoint, andadd new rule S → S1 | S2 | . . . | Si.

To construct CFG for a regular language,“follow” a DFA for the language. For initial stateq0, make R0 the start variable. For state transitionδ(qi, a) = qj add rule Ri → aRj to grammer. Foreach final state qf , add rule Rf → ε to grammer.

For languages with linked substrings (like{0n#1n|n ≥ 0} ), a rule of form R → uRv maybe helpful, forcing desired relation betweensubstrings.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.17

Page 57: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Closure Properties

Regular languages are closed under

unionconcatenationstar

Context-Free Languages are closed underunion : S → S1 | S2

concatenation S → S1S2

star S → ε | SS

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 58: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Closure Properties

Regular languages are closed underunion

concatenationstar

Context-Free Languages are closed underunion : S → S1 | S2

concatenation S → S1S2

star S → ε | SS

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 59: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Closure Properties

Regular languages are closed underunionconcatenation

star

Context-Free Languages are closed underunion : S → S1 | S2

concatenation S → S1S2

star S → ε | SS

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 60: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Closure Properties

Regular languages are closed underunionconcatenationstar

Context-Free Languages are closed underunion : S → S1 | S2

concatenation S → S1S2

star S → ε | SS

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 61: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Closure Properties

Regular languages are closed underunionconcatenationstar

Context-Free Languages are closed under

union : S → S1 | S2

concatenation S → S1S2

star S → ε | SS

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 62: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Closure Properties

Regular languages are closed underunionconcatenationstar

Context-Free Languages are closed underunion : S → S1 | S2

concatenation S → S1S2

star S → ε | SS

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 63: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Closure Properties

Regular languages are closed underunionconcatenationstar

Context-Free Languages are closed underunion : S → S1 | S2

concatenation S → S1S2

star S → ε | SS

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 64: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Closure Properties

Regular languages are closed underunionconcatenationstar

Context-Free Languages are closed underunion : S → S1 | S2

concatenation S → S1S2

star S → ε | SS

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 65: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

More Closure Properties

Regular languages are also closed under

complement (reverse accept/non-accept statesof DFA)

intersection(

L1 ∩ L2 = L1 ∪ L2

)

.

What about complement and intersection ofcontext-free languages?

Not clear . . .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19

Page 66: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

More Closure Properties

Regular languages are also closed undercomplement (reverse accept/non-accept statesof DFA)

intersection(

L1 ∩ L2 = L1 ∪ L2

)

.

What about complement and intersection ofcontext-free languages?

Not clear . . .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19

Page 67: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

More Closure Properties

Regular languages are also closed undercomplement (reverse accept/non-accept statesof DFA)

intersection(

L1 ∩ L2 = L1 ∪ L2

)

.

What about complement and intersection ofcontext-free languages?

Not clear . . .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19

Page 68: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

More Closure Properties

Regular languages are also closed undercomplement (reverse accept/non-accept statesof DFA)

intersection(

L1 ∩ L2 = L1 ∪ L2

)

.

What about complement and intersection ofcontext-free languages?

Not clear . . .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19

Page 69: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

More Closure Properties

Regular languages are also closed undercomplement (reverse accept/non-accept statesof DFA)

intersection(

L1 ∩ L2 = L1 ∪ L2

)

.

What about complement and intersection ofcontext-free languages?

Not clear . . .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19

Page 70: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Ambiguity

Grammar: E → E+E | E×E | (E) | a

aXa+a

EEE

E

E

aXa+a

EEE

E

E

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.20

Page 71: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Ambiguity

We say that a string w is derived ambiguously fromgrammer G if w has two or more parse trees thatgenerate it from G.

Ambiguity is usually not only a syntactic notion butalso a semantic one, implying multiple meanings forthe same string.

It is sometime possible to eliminate ambiguity byfinding a different context free grammer generatingthe same language. This is true for the grammerabove, which can be replaced by unambiguousgrammer from slide (14).

Some languages (e.g. {1i2j3k | i = j or j = k} are in-

herentrly ambigous.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.21

Page 72: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Ambiguity

We say that a string w is derived ambiguously fromgrammer G if w has two or more parse trees thatgenerate it from G.

Ambiguity is usually not only a syntactic notion butalso a semantic one, implying multiple meanings forthe same string.

It is sometime possible to eliminate ambiguity byfinding a different context free grammer generatingthe same language. This is true for the grammerabove, which can be replaced by unambiguousgrammer from slide (14).

Some languages (e.g. {1i2j3k | i = j or j = k} are in-

herentrly ambigous.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.21

Page 73: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Ambiguity

We say that a string w is derived ambiguously fromgrammer G if w has two or more parse trees thatgenerate it from G.

Ambiguity is usually not only a syntactic notion butalso a semantic one, implying multiple meanings forthe same string.

It is sometime possible to eliminate ambiguity byfinding a different context free grammer generatingthe same language. This is true for the grammerabove, which can be replaced by unambiguousgrammer from slide (14).

Some languages (e.g. {1i2j3k | i = j or j = k} are in-

herentrly ambigous.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.21

Page 74: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Ambiguity

We say that a string w is derived ambiguously fromgrammer G if w has two or more parse trees thatgenerate it from G.

Ambiguity is usually not only a syntactic notion butalso a semantic one, implying multiple meanings forthe same string.

It is sometime possible to eliminate ambiguity byfinding a different context free grammer generatingthe same language. This is true for the grammerabove, which can be replaced by unambiguousgrammer from slide (14).

Some languages (e.g. {1i2j3k | i = j or j = k} are in-

herentrly ambigous.Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.21

Page 75: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Chomsky Normal Form

A simplified, canonical form of context freegrammers.Every rule has the form

A → BC

A → a

S → ε

where S is the start symbol, A, B and C are any vari-

able, except B and C not the start symbol.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.22

Page 76: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

TheoremTheorem: Any context-free language is generated bya context-free grammar in Chomsky normal form.Basic idea:

Add new start symbol S0.

Eliminate all ε rules of the form A → ε.

Eliminate all “unit” rules of the form A → B.

Patch up rules so that grammar generates thesame language.

Convert remaining long rules to proper form.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23

Page 77: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

TheoremTheorem: Any context-free language is generated bya context-free grammar in Chomsky normal form.Basic idea:

Add new start symbol S0.

Eliminate all ε rules of the form A → ε.

Eliminate all “unit” rules of the form A → B.

Patch up rules so that grammar generates thesame language.

Convert remaining long rules to proper form.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23

Page 78: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

TheoremTheorem: Any context-free language is generated bya context-free grammar in Chomsky normal form.Basic idea:

Add new start symbol S0.

Eliminate all ε rules of the form A → ε.

Eliminate all “unit” rules of the form A → B.

Patch up rules so that grammar generates thesame language.

Convert remaining long rules to proper form.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23

Page 79: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

TheoremTheorem: Any context-free language is generated bya context-free grammar in Chomsky normal form.Basic idea:

Add new start symbol S0.

Eliminate all ε rules of the form A → ε.

Eliminate all “unit” rules of the form A → B.

Patch up rules so that grammar generates thesame language.

Convert remaining long rules to proper form.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23

Page 80: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

TheoremTheorem: Any context-free language is generated bya context-free grammar in Chomsky normal form.Basic idea:

Add new start symbol S0.

Eliminate all ε rules of the form A → ε.

Eliminate all “unit” rules of the form A → B.

Patch up rules so that grammar generates thesame language.

Convert remaining long rules to proper form.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23

Page 81: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofAdd new start symbol S0 and rule S0 → S.

Guarantees that new start symbol does not appear on

right-hand-side of a rule.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.24

Page 82: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofEliminating ε rules.

Repeat:

remove some A → ε.

for each R → uAv, add rule R → uv.

and so on: for R → uAvAw add R → uvAw,R → uAvw, and R → uvw.

for R → A add R → ε, except if R → ε hasalready been removed.

until all ε-rules not involving the start variable have

been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25

Page 83: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofEliminating ε rules.

Repeat:

remove some A → ε.

for each R → uAv, add rule R → uv.

and so on: for R → uAvAw add R → uvAw,R → uAvw, and R → uvw.

for R → A add R → ε, except if R → ε hasalready been removed.

until all ε-rules not involving the start variable have

been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25

Page 84: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofEliminating ε rules.

Repeat:

remove some A → ε.

for each R → uAv, add rule R → uv.

and so on: for R → uAvAw add R → uvAw,R → uAvw, and R → uvw.

for R → A add R → ε, except if R → ε hasalready been removed.

until all ε-rules not involving the start variable have

been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25

Page 85: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofEliminating ε rules.

Repeat:

remove some A → ε.

for each R → uAv, add rule R → uv.

and so on: for R → uAvAw add R → uvAw,R → uAvw, and R → uvw.

for R → A add R → ε, except if R → ε hasalready been removed.

until all ε-rules not involving the start variable have

been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25

Page 86: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofEliminating ε rules.

Repeat:

remove some A → ε.

for each R → uAv, add rule R → uv.

and so on: for R → uAvAw add R → uvAw,R → uAvw, and R → uvw.

for R → A add R → ε, except if R → ε hasalready been removed.

until all ε-rules not involving the start variable have

been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25

Page 87: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofEliminate unit rules.

Repeat:

remove some A → B.

for each B → u, add rule A → u, unless this ispreviously removed unit rule. (u is a string ofvariables and terminals.)

until all unit rules have been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.26

Page 88: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofEliminate unit rules.

Repeat:

remove some A → B.

for each B → u, add rule A → u, unless this ispreviously removed unit rule. (u is a string ofvariables and terminals.)

until all unit rules have been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.26

Page 89: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofEliminate unit rules.

Repeat:

remove some A → B.

for each B → u, add rule A → u, unless this ispreviously removed unit rule. (u is a string ofvariables and terminals.)

until all unit rules have been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.26

Page 90: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofFinally, convert long rules.To replace each A → u1u2 . . . uk (for k ≥ 3),introduce new non-terminals

N1, N2, . . . , Nk−1

and rules

A → u1N1

N1 → u2N2

...Nk−3 → uk−2Nk−2

Nk−2 → uk−1uk ♠

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.27

Page 91: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

ProofFinally, convert long rules.To replace each A → u1u2 . . . uk (for k ≥ 3),introduce new non-terminals

N1, N2, . . . , Nk−1

and rules

A → u1N1

N1 → u2N2

...Nk−3 → uk−2Nk−2

Nk−2 → uk−1uk ♠

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.27

Page 92: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Conversion Example

Initial Grammar:

S → ASA | aB

A → B | SB → b | ε

(1) Add new start state:

S0 → S

S → ASA | aB

A → B | SB → b | ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.28

Page 93: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Conversion Example (2)

S0 → S

S → ASA | aB

A → B | SB → b | ε

(2) Remove ε-rule B → ε:

S0 → S

S → ASA | aB | aA → B | S | εB → b | ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.29

Page 94: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Conversion Example (3)

S0 → S

S → ASA | aB | aA → B | S | εB → b

(3) Remove ε-rule A → ε:

S0 → S

S → ASA | aB | a | AS | SA | SA → B | S | εB → b

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.30

Page 95: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Conversion Example (4)

S0 → S

S → ASA | aB | a | AS | SA | SA → B | SB → b

(4) Remove unit rule S → S

S0 → S

S → ASA | aB | a | AS | SA | SA → B | SB → b

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.31

Page 96: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Conversion Example (5)

S0 → S

S → ASA | aB | a | AS | SA

A → B | SB → b

(5) Remove unit rule S0 → S:

S0 → S | ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → B | SB → b

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.32

Page 97: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Conversion Example (6)

S0 → ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → B | SB → b

(6) Remove unit rule A → B:

S0 → ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → B | S | bB → b

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.33

Page 98: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Conversion Example (7)

S0 → ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → S | bB → b

Remove unit rule A → S:

S0 → ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → S | b | ASA | aB | a | AS | SA

B → bSlides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.34

Page 99: Lecture 4 – Outline - TAU › ~bchor › CM › Compute4.pdf · Lecture 4 – Outline So far we saw finite automata, regular languages, regular expressions, pumping lemma for regular

Conversion Example (8)

S0 → ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → b | ASA | aB | a | AS | SA

B → b

(8) Final simplification – treat long rules:

S0 → AA1 | UB | a | SA | AS

S → AA1 | UB | a | SA | AS

A → b | AA1 | UB | a | SA | AS

A1 → SA

U → a

B → b√

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.35