BU CS 332 Theory of Computation Lecture 7: Reading: More on CFGs - - PowerPoint PPT Presentation

bu cs 332 theory of computation
SMART_READER_LITE
LIVE PREVIEW

BU CS 332 Theory of Computation Lecture 7: Reading: More on CFGs - - PowerPoint PPT Presentation

BU CS 332 Theory of Computation Lecture 7: Reading: More on CFGs Sipser Ch 2.1 2.3 Pushdown Automata Mark Bun February 12, 2020 Context Free Grammar (Formal) A CFG is a 4 tuple is a finite set of variables is a


slide-1
SLIDE 1

BU CS 332 – Theory of Computation

Lecture 7:

  • More on CFGs
  • Pushdown Automata

Reading: Sipser Ch 2.1‐2.3

Mark Bun February 12, 2020

slide-2
SLIDE 2

Context‐Free Grammar (Formal)

A CFG is a 4‐tuple

  • is a finite set of variables
  • is a finite set of terminal symbols (disjoint from )
  • is a finite set of production rules of the form

, where and

  • is the start symbol

2/12/2020 CS332 ‐ Theory of Computation 2

slide-3
SLIDE 3

Context‐Free Grammar

Example Grammar Parse Tree | | | Derivation

2/12/2020 CS332 ‐ Theory of Computation 3

slide-4
SLIDE 4

Context‐Free Languages

2/12/2020 CS332 ‐ Theory of Computation 4

𝑀 is a context‐free language if it is the language of some CFG

Questions about CFLs

  • 1. Which languages are not

context‐free?

  • 2. How do we recognize

whether 𝑥 ∈ 𝑀?

  • 3. What are the closure

properties of CFLs?

slide-5
SLIDE 5

Pumping Lemma for context‐free languages

2/12/2020 CS332 ‐ Theory of Computation 5

Let be a context‐free language. Then there exists a “pumping length” such that 1. 2. 3.

𝑗 𝑗 

for all For every where , can be split into five parts where:

slide-6
SLIDE 6

Pumping Lemma example

2/12/2020 CS332 ‐ Theory of Computation 6

Claim:

  • is not context‐free

Proof: Assume is context‐free with pumping length

  • 1. Find

with

  • 2. Show that

cannot be pumped If = with | | , then… Case 1: both contain only one kind of symbol Case 2: Either or contains two kinds of symbols

slide-7
SLIDE 7

Pumping Lemma example

2/12/2020 CS332 ‐ Theory of Computation 7

Claim:

  • is not context‐free

Proof: Assume is context‐free with pumping length

  • 1. Find

with

  • 2. Show that

cannot be pumped If = with | | , then… Case 1: both contain only one kind of symbol

slide-8
SLIDE 8

2/12/2020 CS332 ‐ Theory of Computation 8

slide-9
SLIDE 9

Pumping Lemma example

2/12/2020 CS332 ‐ Theory of Computation 9

Claim:

  • is not context‐free

Proof: Assume is context‐free with pumping length

  • 1. Find

with

  • 2. Show that

cannot be pumped If = with | | , then… Case 2: Either or contains two kinds of symbols

slide-10
SLIDE 10

Pumping Lemma: Proof idea

Let be a context‐free language. If is long enough, then every parse tree for has a repeated variable.

2/12/2020 CS332 ‐ Theory of Computation 10

slide-11
SLIDE 11

Pumping Lemma Proof

What does “long enough” mean? (How do we choose the pumping length ?)

  • Let

be a CFG for

  • Suppose the right‐hand side of every rule in

uses at most symbols

  • Let

Claim: If with , then the smallest parse tree for has height at least

2/12/2020 CS332 ‐ Theory of Computation 11

slide-12
SLIDE 12

Pumping Lemma Proof

Claim: If with , then the smallest parse tree for has height at least

  • By the pigeonhole principle, there is a path down the parse tree

with a repeated variable 𝑆

  • Choose two such occurrences within the bottom 𝑊 1 levels

2/12/2020 CS332 ‐ Theory of Computation 12

slide-13
SLIDE 13

Context‐Free Languages

2/12/2020 CS332 ‐ Theory of Computation 13

𝑀 is a context‐free language if it is the language of some CFG

Questions about CFLs

  • 1. Which languages are not

context‐free?

  • 2. How do we recognize

whether 𝑥 ∈ 𝑀?

  • 3. What are the closure

properties of CFLs?

slide-14
SLIDE 14

Pushdown Automata

2/12/2020 CS332 ‐ Theory of Computation 14

slide-15
SLIDE 15

2/12/2020 CS332 ‐ Theory of Computation 15

Regular Expressions : Finite Automata :: Context‐Free Languages : ???

slide-16
SLIDE 16

Pushdown Automata

2/12/2020 CS332 ‐ Theory of Computation 16

Input 𝑏 𝑐 𝑏 𝑏 Finite control … Input 𝑏 𝑐 𝑏 𝑏 Finite control … 𝑦 𝑦 𝑧 Memory: Infinite Stack

Pushdown Automata (PDAs): Machine with unbounded structured memory in the form of a stack Finite Automata (FAs): Machine with a finite amount of unstructured memory

slide-17
SLIDE 17

Pushdown Automaton (the idea)

  • Nondeterministic finite automaton + stack
  • Stack has unlimited size, but machine can only manipulate (push,

pop, read) symbol at the top

2/12/2020 CS332 ‐ Theory of Computation 17

Input 𝑏 𝑐 𝑏 𝑏 Finite control … 𝑦 𝑦 𝑧 Memory: Infinite Stack

Transitions of the form: 𝑏, 𝑦 → 𝑦′

slide-18
SLIDE 18

Example: Even Palindromes

2/12/2020 CS332 ‐ Theory of Computation 18

Input 𝑏 𝑐 𝑐 𝑐 𝑐 𝑏 Finite control … Memory: Infinite Stack

slide-19
SLIDE 19

Example: Even Palindromes

2/12/2020 CS332 ‐ Theory of Computation 19

Input 𝑏 𝑐 𝑐 𝑐 𝑐 𝑏 Finite control … Memory: Infinite Stack

Algorithmic Description

1. Place the marker $ on the stack 2. Nondeterministically, either a) Read a character and push it to the stack, or b) Go to the next step 3. Nondeterministically, either a) Pop the stack if it matches the next character or b) Go to the next step 4. Accept if the top of the stack is $

slide-20
SLIDE 20

Example: Even Palindromes

2/12/2020 CS332 ‐ Theory of Computation 20

𝜁, 𝜁 → $ 𝑏, 𝜁 → 𝑏 𝜁, 𝜁 → 𝜁 𝑏, 𝑏 → 𝜁 𝜁, $ → 𝜁

𝑔

𝑐, 𝜁 → 𝑐 𝑐, 𝑐 → 𝜁

slide-21
SLIDE 21

Pushdown Automaton (formal)

A PDA is a 6‐tuple (sorry)

  • is a finite set of states
  • is the input alphabet
  • is the stack alphabet
  • :
  • is the transition function
  • is the start state
  • is the set of final states

2/12/2020 CS332 ‐ Theory of Computation 21

accepts a string if, starting from

0 and an empty

stack, there exists a path to an accept state that can be followed by reading all of .

slide-22
SLIDE 22

Example: Even Palindromes

2/12/2020 CS332 ‐ Theory of Computation 22

𝜁, 𝜁 → $ 𝑏, 𝜁 → 𝑏 𝜁, 𝜁 → 𝜁 𝑏, 𝑏 → 𝜁 𝜁, $ → 𝜁

𝑔

𝑐, 𝜁 → 𝑐 𝑐, 𝑐 → 𝜁 𝜀 : 𝑅 Σ Γ

→ 𝑄 𝑅 Γ

  • 𝜀 𝑞, 𝑐, 𝜁

𝜀 𝑟, 𝑏, 𝑏 𝜀 𝑟, 𝑏, 𝑐