Loading in 5 sec....

Intro to Theory of ComputationPowerPoint Presentation

Intro to Theory of Computation

- 236 Views
- Uploaded on
- Presentation posted in: General

Intro to Theory of Computation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

- LECTURE7
- Theory of Computation
- Context-free grammar (CFG)
- Equivalence of CFGs and PDAs

Intro to Theory of Computation

CS

464

Sofya Raskhodnikova

Sofya Raskhodnikova; based on slides by Nick Hopper

CONTEXT-FREEGRAMMARS (CFGs)

production

rules

start variable

A → 0A1

A → B

B → #

variables

terminals

A

0A1

00A11

00B11

00#11

Sofya Raskhodnikova; based on slides by Nick Hopper

VALLEY GIRL GRAMMAR

<PHRASE> → <FILLER><PHRASE>

<PHRASE> → <START><END>

<FILLER> → LIKE

<FILLER> → UMM

<START> → YOU KNOW

<START> → ε

<END> → GAG ME WITH A SPOON

<END> → AS IF

<END> → WHATEVER

<END> → LOSER

Sofya Raskhodnikova; based on slides by Nick Hopper

VALLEY GIRL GRAMMAR

<PHRASE> → <FILLER><PHRASE> | <START><END>

<FILLER> → LIKE | UMM

<START> → YOU KNOW | ε

<END> → GAG ME WITH A SPOON | AS IF | WHATEVER | LOSER

Sofya Raskhodnikova; based on slides by Nick Hopper

A CFGis a 4-tuple G = (V, Σ, R, S)

Vis a finite set of variables

Σ is a finite set of terminals (disjoint from V)

R is set of production rules of the form A → W, where A V and W (VΣ)*

S V is the start variable

L(G) is the set of strings generated by G

A language is context-free

if it is generated by a some CFG.

Sofya Raskhodnikova; based on slides by Nick Hopper

A CFGis a 4-tuple G = (V, Σ, R, S)

Vis a finite set of variables

Σ is a finite set of terminals (disjoint from V)

R is set of production rules of the form A → W, where A V and W (VΣ)*

S V is the start variable

R = { S → 0S1, S → ε }

Example: G = {{S}, {0,1}, R, S}

L(G) = { 0n1n | n ≥ 0 }

Sofya Raskhodnikova; based on slides by Nick Hopper

CFG terminology

production

rules

start variable

A → 0A1

A → B

B → #

variables

terminals

A

0A1

00A11

00B11

00#11

uVwyields uvw if (V → v) R.

A derives 00#11 in 4 steps.

Sofya Raskhodnikova; based on slides by Nick Hopper

Example

GIVE A CFG FOR EVEN-LENGTH PALINDROMES

S → S for all Σ

S → ε

Sofya Raskhodnikova; based on slides by Nick Hopper

Example

GIVE A CFG FOR THE EMPTY SET

G = { {S}, Σ, , S }

Sofya Raskhodnikova; based on slides by Nick Hopper

GIVE A CFG FOR…

L3 = { strings of balanced parens }

L4 = { aibjck | i, j, k ≥ 0 and (i = j or j = k) }

Sofya Raskhodnikova; based on slides by Nick Hopper

The syntactic grammar for the Java programming language

BasicForStatement:

for ( ; ; ) Statement

for ( ; ; ForUpdate ) Statement

for ( ; Expression ; ) Statement

for ( ; Expression ; ForUpdate ) Statement

for ( ForInit ; ; ) Statement

for ( ForInit ; ; ForUpdate ) Statement

for ( ForInit ; Expression ; ) Statement

for ( ForInit ; Expression ; ForUpdate ) Statement

Sofya Raskhodnikova; based on slides by Nick Hopper

COMPILER MODULES

LEXER

PARSER

SEMANTIC ANALYZER

TRANSLATOR/INTERPRETER

Sofya Raskhodnikova; based on slides by Nick Hopper

Parse trees

A

A

A

B

0

0

#

1

1

A

0A1

00A11

00B11

00#11

Sofya Raskhodnikova; based on slides by Nick Hopper

string

pop

push

PDAs: reminder

ε,ε → $

0,ε→ 0

1,0→ε

ε,$ → ε

1,0→ε

The language of P is the set of strings it accepts.

PDAs are nondeterministic.

Sofya Raskhodnikova; based on slides by Nick Hopper

Equivalence of CFGs & PDAs

A language is generated by a CFG

It is recognized by a PDA

Sofya Raskhodnikova; based on slides by Nick Hopper

Converting a CFG to a PDA

Suppose L is generated by a CFG G = (V, Σ, R, S).

Construct a PDA P = (Q, Σ, Γ, , q, F) that recognizes L.

Idea: P will guess steps of a derivation of its input w

and use its stack to derive it.

Sofya Raskhodnikova; based on slides by Nick Hopper

Algorithmic description of PDA

(1) Place the marker symbol $ and the start variable on the stack.

(2) Repeat forever:

If a variable V is on top of the stack, and (V ! s) 2 R,

pop V and

push string s on the stack

(b) If a terminal is on top of the stack,

pop it and match it with input.

in reverse order.

(3) On (ε,$), accept.

Sofya Raskhodnikova; based on slides by Nick Hopper

Designing states of PDA

(qstart) Push S$ and go to qloop

(qloop) Repeat the following steps forever:

(a) On (ε,V) where (V ! s) 2 R, push s and go to qloop

(b) On (,), pop and go to qloop

(c) On (ε,$) go to qaccept

Otherwise, the PDA will get stuck!

Sofya Raskhodnikova; based on slides by Nick Hopper

Designing PDA

ε,ε → S$

ε,A → w for rule A → w

a,a → ε for terminal a

ε,$ → ε

Sofya Raskhodnikova; based on slides by Nick Hopper

S → aTb

T → Ta | ε

ε,ε → $

ε,ε → T

ε,S → b

ε,ε → T

ε,ε → S

ε,T → a

ε,$ → ε

ε,ε → a

ε,T → ε

a,a → ε

b,b → ε

Sofya Raskhodnikova; based on slides by Nick Hopper

A language is generated by a CFG

It is recognized by a PDA

Sofya Raskhodnikova; based on slides by Nick Hopper

Converting a PDA to a CFG

Given PDA P = (Q, Σ, Γ, , q, F)

Construct a CFG G = (V, Σ, R, S) with L(G)=L(P)

First, simplify P so that:

(1) It has a single accept state, qaccept

(2) It empties the stack before accepting

(3) Each transition either pushes a symbol or pops a symbol, but not both

Sofya Raskhodnikova; based on slides by Nick Hopper

SIMPLIFY

ε,ε → $

0,ε→ 0

q1

q0

1,0→ε

ε,ε → 0

ε,ε → ε

ε,$ → ε

q2

q3

1,0→ε

ε,ε → 0

ε,ε → ε

q4

ε,0 → ε

q5

Sofya Raskhodnikova; based on slides by Nick Hopper

From PDA to CFG: main idea

For each pair of states p and q in P,

add a variable Apq to CFG

that generates all strings that that can take P from p to q without changing the stack*

V = {Apq | p,qQ }

S = Aq0qaccept

*Starting from any stack S in p, including empty stack,

P has stack S at q.

Sofya Raskhodnikova; based on slides by Nick Hopper

ε,ε → $

0,ε→ 0

q1

q0

1,0→ε

ε,ε → 0

ε,$ → ε

q2

q3

1,0→ε

ε,ε → 0

q4

ε,0 → ε

q5

Example

What strings does Aq0q1 generate?

none

What strings does Aq1q2 generate?

{0n1n | n > 0}

What strings does Aq1q3 generate?

none

Sofya Raskhodnikova; based on slides by Nick Hopper

From PDA to CFG: main idea

Apq generates all strings that take P from p to q without changing the stack

Let x be such a string

- P’s first move on x must be a push

- P’s last move on x must be a pop

Consider the stack while reading x. Either:

New portion of the stack first empties

only at the end of x

2. New portion empties before the end of x

Sofya Raskhodnikova; based on slides by Nick Hopper

New portion of the stack first empties

only at the end of x

stack

height

r

s

a

b

input

string

p

q

Apq→ aArsb

Sofya Raskhodnikova; based on slides by Nick Hopper

2. New portion empties before the end of x

stack

height

input

string

p

r

q

Apq→ AprArq

Sofya Raskhodnikova; based on slides by Nick Hopper

CFG construction

V = {Apq | p,qQ }

S = Aq0qaccept

For each p,q,r,s Q, t Γ and a,b Σε

If (r,t) (p,a,ε) and (q, ε) (s,b,t)

Then add the rule Apq→ aArsb

For each p,q,r Q,

add the rule Apq→ AprArq

For each p Q,

add the rule App→ ε

Sofya Raskhodnikova; based on slides by Nick Hopper

ε,ε → $

0,ε→ 0

q1

q0

1,0→ε

ε,ε → 0

ε,$ → ε

q2

q3

1,0→ε

ε,ε → 0

q4

ε,0 → ε

q5

Aqq→ ε

Apq→ AprArq

Aq0q3 → εAq1q2ε

Aq1q2 → 0Aq1q21

Aq1q2 → 0Aq1q11

none

What strings does Aq0q1 generate?

{0n1n | n > 0}

What strings does Aq1q2 generate?

What strings does Aq1q3 generate?

none

Sofya Raskhodnikova; based on slides by Nick Hopper

Equivalence of CFGs & PDAs

A language is generated by a CFG

It is recognized by a PDA

Sofya Raskhodnikova; based on slides by Nick Hopper