CS20a: summary (Oct 17, 2002) Context-free languages Grammars G = - - PowerPoint PPT Presentation

cs20a summary oct 17 2002
SMART_READER_LITE
LIVE PREVIEW

CS20a: summary (Oct 17, 2002) Context-free languages Grammars G = - - PowerPoint PPT Presentation

CS20a: summary (Oct 17, 2002) Context-free languages Grammars G = (V, T, P, S) Grammar simplification Eliminate epsilon productions (A -> epsilon) Eliminate unit productions (A -> B) Eliminate useless symbols


slide-1
SLIDE 1

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

1

CS20a: summary (Oct 17, 2002)

  • Context-free languages

– Grammars G = (V, T, P, S) – Grammar simplification

  • Eliminate epsilon productions (A -> epsilon)
  • Eliminate unit productions (A -> B)
  • Eliminate useless symbols

– Normal forms

  • Chomsky
  • Griebach (not covered in lecture)
  • Next: pushdown automata

– N-PDA = CFG – D-PDA < CFG

slide-2
SLIDE 2

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

2

Pushdown automata

  • A PDA has

– An input tape – A finite control – A stack

  • First, we’ll consider nondeterministic PDAs
  • Actions:

– Read: Examine input, move tape head right, and replace the top of the stack with a new symbol (nondeterministically--there may be many choices) – Think: manipulate stack without reading

slide-3
SLIDE 3

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

3

PDA machine

1 + 2 * 3 + 4 ;

Finite Control Read-only tape Tape head

  • 1. In state q
  • a. read a symbol c, stack symbol Z
  • b. move the tape head right
  • b. goto state delta(q, c, Z).1
  • d. replace Z with delta(q, c, Z).2
  • -or:
  • a. goto state delta(q, ε, Z).1
  • b. replace Z with delta(q, c, Z).2
  • 2. Accept iff the FA is in a final

state after reading the last symbol Stack 2 + 1 Z

slide-4
SLIDE 4

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

4

Acceptance conditions

  • Two traditional conditions

– Process the input, and accept if the stack is empty – Process the input, and accept if the PDA is in a final state

slide-5
SLIDE 5

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

5

PDA: formal definition

A (nondetermistic) PDA is a 7-tuple (Q, Σ, Γ, δ, q0, Z0, F)

  • Q is a set of states
  • Σ is an input alphabet
  • Γ is a stack alphabet
  • q0 is the start state
  • Z0 is a stack symbol called the start symbol
  • F ⊆ Q is a set of final states
  • δ : Q × (Σ ∪ {ǫ}) × Γ → 2Q×Γ ∗)
slide-6
SLIDE 6

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

6

PDA execution: reading a symbol

  • Consider δ(q, a, Z) = {(p1, γ1), . . . , (pm, γn)}
  • Then the PDA can:

– enter some state pi – pop the stack (the symbol Z) – push all elements γi right-to-left (so the first symbol of γi is at the top) – advance the tape head

slide-7
SLIDE 7

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

7

PDA execution: epsilon transition

  • Consider δ(q, ǫ, Z) = {(p1, γ1), . . . , (pm, γn)}
  • Then the PDA can:

– enter some state pi – pop the stack (the symbol Z) – push all elements γi right-to-left – does not advance the tape head

slide-8
SLIDE 8

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

8

Balanced parentheses

M = ({q1}, {), (}, {S, (}, δ, S, {}) δ(q1, (, S) = {(q1, ()} δ(q1, (, () = {(q1, ()} δ(q1, ), () = {(q1, ǫ)} δ(q1, ǫ, S) = {(q1, ǫ)}

slide-9
SLIDE 9

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

9

Accepting w$wR

  • (Machine given in class)
slide-10
SLIDE 10

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

10

Instantaneous descriptions

An instantaneous description (ID) is a 3-tuple (q, w, γ)

  • q ∈ Q is a state
  • w ∈ Σ∗ is the rest of the input
  • γ ∈ Γ ∗ is the contents of the stack, read top-to-

bottom

slide-11
SLIDE 11

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

11

ID

1 + 2 * 3 + 4 ;

Finite Control Read-only tape Tape head ID = (q, "*3+4;", "2+1") where q is the current machine state Stack 2 + 1 Z

slide-12
SLIDE 12

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

12

ID transitions

  • Let M = (Q, Σ, Γ, δ, q0, Z0, F) be a PDA
  • Let a ∈ Σ ∪ {ǫ} be an input symbol
  • Then, (q, aw, Zα) →M (p, w, βα) if δ(q, a, Z) con-

tains (p, β)

  • Define →∗

M as the transitive closure of →M (so it is

reflexive, symmetric, and transitive)

slide-13
SLIDE 13

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

13

Executions

  • An execution σ = {σ1, . . . , σn} is a string σ ∈ ID∗,

where – σi →M σi+1

slide-14
SLIDE 14

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

14

Acceptance

  • A machine M = (Q, Σ, Γ, δ, q0, Z0, F) accepts string

w if – (empty stack) there exists an execution (q0, w, Z0) →M (q, ǫ, {}) – (final state) there exists an execution (q0, w, Z0) →M (q, ǫ, γ) and q ∈ F

slide-15
SLIDE 15

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

15

Deterministic PDAs

  • Let M = (Q, Σ, Γ, δ, q0, Z0, F) be a PDA
  • Then M is deterministic if:

– Whenever δ(q, ǫ, Z) ≠ {}, then δ(q, a, Z) = {} for any a ∈ Σ – |δ(q, a, Z)| ≤ 1 for any a ∈ Σ

  • Note: wwR is accepted by a nondetermistic PDA,

but not by any deterministic PDA

slide-16
SLIDE 16

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

16

Empty stack vs final state

  • Final state PDA -> empty stack PDA

– Simulate: whenever the PDA reaches a final state, empty the stack

  • Empty stack PDA -> final state PDA

– Add a special marker $ at the bottom of the stack – Add transitions delta(q, epsilon, S) qf for some new state qf in F

slide-17
SLIDE 17

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

17

Compiling a CFG to a PDA

Theorem If G = (V, T, P, S), then there is a PDA M where L(G) = L(M) (empty stack)

slide-18
SLIDE 18

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

18

Greibach Normal Form (GNF)

Greibach normal form every context-free language L without ǫ can be generated by a CFG wherr each pro- duction has the form A → aα where a is a terminal, and α is a sequence of symbols.

slide-19
SLIDE 19

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

19

GNF Construction: part 1

Proof

  • Let G = (V, T, P, S) be a CFG in Chomsky normal

form where L = L(G)

  • This means all productions have the form A → a
  • r A → BC
  • First, number the productions
  • Next, require that if Ai → Ajα is a production,

then j > i

slide-20
SLIDE 20

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

20

Production ordering

By (complete) induction on the productions

  • Consider production i
  • For j <, assume all productions have the form Aja
  • r Aj → Akα where k > j
  • If Ai → Alα, and l < i then substitute Al for its

right-hand-side

  • Repeat, until Ai → Amα for m ≥ l
slide-21
SLIDE 21

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

21

GNF productions

  • For each Ai → Ajα, expand Aj so that the produc-

tion starts with a terminal

  • Each rhs for Bi starts with a terminal or a Ai
  • If Ai, expand to get a terminal
slide-22
SLIDE 22

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

22

Building the PDA from the GNF

To build the PDA for a grammar G = (V, T, P, S) in GNF

  • Each production has the form A → a1 . . . anX1 . . . Xm
  • Define M = ({q}, T, V, δ, q, S, {})
  • Let δ(q, a, A) contains (q, γ) iff A → aγ ∈ P
slide-23
SLIDE 23

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

23

Constructed forms

Now we have productions of the following forms:

  • Ai → Ajα for j > i
  • Ai → aα for terminal a
  • Bi → γ for γ ∈ (V ∪ {B1, . . . , Bm})∗
slide-24
SLIDE 24

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

24

Self-productions

  • Next, suppose Ai → Aiα,
  • Let AiAiα1 | · · · | Aiαn be the productionw ith

rhs starting with Ai

  • Let Ai → β1 | · · · | βm be the remaining produc-

tions

  • Add a new nonterminal B, and productions

Ai → βj Ai → βjB B → αi B → αiB

slide-25
SLIDE 25

Computation, Computers, and Programs CFG/PDA http://www.cs.caltech.edu/~cs20/a October 17, 2002

25

Building a CFG from a PDA

Theorem If M is a PDA, then there is a CFG G = (V, T, P, S) s.t. L(M) = L(G)

  • Let M = (Q, Σ, Γ, δ, q0, Z0, F)
  • Let V = {[q, A, p] | q, p ∈ Q ∧ A ∈ Γ} ∪ S
  • Productions

– S → [q0, Z0, q] for each q ∈ Q – [q, A, qm+1] → a[q1, B1, q2] · · · [qm, Bm, qm+1] for each (q1, B1 . . . Bm) ∈ δ(q, a, A)