Inf2A: CFGs and PDAs Stuart Anderson School of Informatics - - PowerPoint PPT Presentation

inf2a cfgs and pdas
SMART_READER_LITE
LIVE PREVIEW

Inf2A: CFGs and PDAs Stuart Anderson School of Informatics - - PowerPoint PPT Presentation

Context-free Languages Pushdown Automata Inf2A: CFGs and PDAs Stuart Anderson School of Informatics University of Edinburgh October 29, 2009 university-logo Stuart Anderson Context-Free Languages 1 1/20 Context-free Languages Examples


slide-1
SLIDE 1

university-logo Context-free Languages Pushdown Automata

Inf2A: CFGs and PDAs

Stuart Anderson

School of Informatics University of Edinburgh

October 29, 2009

Stuart Anderson Context-Free Languages 1 1/20

slide-2
SLIDE 2

university-logo Context-free Languages Pushdown Automata Examples

Context-free Languages

Context-free languages are widely used to describe simple languages utilizing parenthesis structure. The are very widely studied and many properties of the class are known. Algorithms for fast parsing of such languages have been studied extensively. Over the next three weeks we consider some of the work

  • n context-free languages.

Stuart Anderson Context-Free Languages 1 2/20

slide-3
SLIDE 3

university-logo Context-free Languages Pushdown Automata Examples

A simple CFG

S → NP VP Det → the N → cat V → sleeps NP → Det N Det → a N → dog V → barks VP → V Mod Det → my Mod → well Mod → loudly

Stuart Anderson Context-Free Languages 1 3/20

slide-4
SLIDE 4

university-logo Context-free Languages Pushdown Automata Examples

A derivation

S ⇒ NP VP using S → NP VP ⇒ Det N VP using NP → Det N ⇒ the N VP using Det → the ⇒ the dog VP using N → dog ⇒ the dog V Mod using VP → V Mod ⇒ the dog barks Mod using V → barks ⇒ the dog barks loudly using Mod → loudly.

Stuart Anderson Context-Free Languages 1 4/20

slide-5
SLIDE 5

university-logo Context-free Languages Pushdown Automata Examples

Another Derivation and its Tree

S ⇒ NP VP ⇒ Det N VP ⇒ my N VP ⇒ my cat VP ⇒ my cat V Mod ⇒ my cat sleeps Mod ⇒ my cat sleeps well. S

  • NP
  • VP
  • Det
  • N
  • V
  • Mod
  • my

cat sleeps well

Stuart Anderson Context-Free Languages 1 5/20

slide-6
SLIDE 6

university-logo Context-free Languages Pushdown Automata Examples

Languages that are C-F but are not Regular

{anbn | n ≥ 0}, palindromes over {a, b}∗, sequences of palindromes each of length greater than one, balanced strings of parentheses. Can you demonstrate they are not Regular? (How?) Can you demonstrate they are Context-Free? (How?)

Stuart Anderson Context-Free Languages 1 6/20

slide-7
SLIDE 7

university-logo Context-free Languages Pushdown Automata Examples

Getting rid of ε and unit Productions

Unclear whether we are “making progress” when we apply a production. Without these productions we either increase the number of terminal symbols or increase the length of the string each time we apply a production. If we have a CFG G = (N, Σ, P, S) then we make a new set of productions P that includes P and also P satisfies two rules:

If A → ε is in P and B → αAβ is also in P then B → αβ is in

  • P.

If A → C is in P and B → αAβ is also in P then B → αCβ is in P.

ε- and unit productions in P are redundant because for any derivation using such a production we can find a shorter derivation without that use (Kozen 21.3)

Stuart Anderson Context-Free Languages 1 7/20

slide-8
SLIDE 8

university-logo Context-free Languages Pushdown Automata Examples

Converting to Griebach Normal Form (sketch - see Kozen Ch 21)

Starting with a grammar G0 = (N0, Σ, P0, S0) convert it to a grammar G = (N, Σ, P, S) in Chomsky Normal Form (possibly L(G0) − L(G) = {ε}) Write α

L

− →

G β if β can be derived from α in G by rewriting

  • nly the leftmost symbol in any sentential form.

Define RA,a = {β ∈ N∗ | A

L

− →

G aβ} and observe that this is

a regular set. Define a grammar GA,a with start symbol TA,a with L(GA,a) = RA,aand all the rules are of the form X → BY or X → ε where X and Y are nonterminals of GA,a and B is a nonterminal of G.

Stuart Anderson Context-Free Languages 1 8/20

slide-9
SLIDE 9

university-logo Context-free Languages Pushdown Automata Examples

Conversion to Griebach Normal Form (ctd)

Assume all the nonterminals of G and GA,a are disjoint and combine all the grammars into a new grammar G1 with the same start symbol as G. This generates the same language as G because the new rules are not accessible from the start symbol. The rules in G1 are all of the form X → b, X → ε, or X → BY where B is a non-terminal of G Derive a new grammar G2 from G1 by removing each production of the form X → BY with the set of productions {X → bTB,bY | b ∈ Σ}. Derive a new grammar G3 from G2 by removing ε-productions and the resulting grammar is in GNF (because all the productions except ε productions contain terminals).

Stuart Anderson Context-Free Languages 1 9/20

slide-10
SLIDE 10

university-logo Context-free Languages Pushdown Automata Examples

Conversion to Griebach Normal Form - Example

S → AB | AC | SS, C → SB, A → (, B →) RS,( = (B +C)S∗, RC,( = (B +C)S∗B, RA,( = RB,) = {ε} rest are ∅ TS,( → BX | CX, X → SX | ε . . . S → (TA,(B | (TA,(C | (TS,(S, C → (TS,(B, A → (, . . .

Stuart Anderson Context-Free Languages 1 10/20

slide-11
SLIDE 11

university-logo Context-free Languages Pushdown Automata Examples

Derivations with GNF Grammars

Consider the GNF grammar G with rules: S → (SA | #, A →) Suppose we want to check that (((#))) is in L(G) S ⇒ (SA ⇒ ((SAA ⇒ (((SAAA ⇒ (((#AAA ⇒ (((#)AA ⇒ (((#))A ⇒ (((#)))

Stuart Anderson Context-Free Languages 1 11/20

slide-12
SLIDE 12

university-logo Context-free Languages Pushdown Automata Examples

Extending Automata

FSAs are not adequate to recognise context-free languages (pumping lemma). How can we generalise FSAs to accept context-free languages? We extend FSAs with a single stack, this is called a pushdown automaton. Now we meet pushdown automata and see how we can use them, systematically, to create a recogniser for any language described by a CFG.

Stuart Anderson Context-Free Languages 1 12/20

slide-13
SLIDE 13

university-logo Context-free Languages Pushdown Automata Example Configurations and Computations Example Computation Formalities

What is a Pushdown Automaton (PDA)?

We augment an FSA (deterministic/non-deterministic, with

  • r without ε-transitions) with a single stack of symbols

drawn from some alphabet. The automaton is augmented so it can read the next input symbol and the top of stack symbol. The automaton can also manipulate the stack by pushing and popping symbols from the stack. A transition becomes more complex because we have more to do. Using the current state, input symbol, and top

  • f stack symbol we decide what to do and that comprises

the specification of the next state and what we do to the stack (either push, pop, or leave unchanged)

Stuart Anderson Context-Free Languages 1 13/20

slide-14
SLIDE 14

university-logo Context-free Languages Pushdown Automata Example Configurations and Computations Example Computation Formalities

Example

  • 1

[,λ;X

  • 2

),A;λ

  • (,λ;A
  • ],X;λ
  • 3

Transitions have the form a, A; β where a, A is the current input symbol (or ε) and the current top of stack symbol (or λ)and β is the new sequence of stack symbols to be written on the top of the stack. We use λ as the empty stack symbol so there are three kinds of changes we can make to the stack:

a, A; λ pops an A symbol from the stack, a, λ; β pushes the string β onto the stack, and a, A; β replaces the A on top of the stack with β.

Stuart Anderson Context-Free Languages 1 14/20

slide-15
SLIDE 15

university-logo Context-free Languages Pushdown Automata Example Configurations and Computations Example Computation Formalities

Configurations and Computations

A configuration of a PDA P is written (q, x, γ) where q is the current state, x is the remaining input to be read, and γ is the stack written as a string with the leftmost symbol on the top of stack. A computation of a PDA P is any sequence of configurations c1, . . . , cn where ci+1 is derived from ci by applying a transition to it. For example c′ = (q′, x, βγ) is derived from c = (q, ax, Aγ) in P if there is a transition labelled a, A; β between q and q′ in P.

Stuart Anderson Context-Free Languages 1 15/20

slide-16
SLIDE 16

university-logo Context-free Languages Pushdown Automata Example Configurations and Computations Example Computation Formalities

Configurations and Computations - continued

A string w is accepted by PDA P if there is a computation from (q0, w, ⊥) (where ⊥∈ Γ is the sole initial symbol on the stack) whose last configuration is (qf, ε, γ) where q0 is an initial state of P and qf is a final state. In some cases we may choose a variant definition of acceptance - a string is accepted on empty stack if it causes the machine to remove the initial instance of ⊥ from the stack.

Stuart Anderson Context-Free Languages 1 16/20

slide-17
SLIDE 17

university-logo Context-free Languages Pushdown Automata Example Configurations and Computations Example Computation Formalities

  • 1

[,λ;X

  • 2

),A;λ

  • (,λ;A
  • ],X;λ
  • 3

Consider the initial configuration: (1, [(())], λ) (2, (())], X) (2, ())], AX) (2, ))], AAX) (2, )], AX) (2, ], X) (3, ε, λ) What is the language recognised? What is a failing string?

Stuart Anderson Context-Free Languages 1 17/20

slide-18
SLIDE 18

university-logo Context-free Languages Pushdown Automata Example Configurations and Computations Example Computation Formalities

Example: CFGs and PDAs

Consider a grammar with productions: S → [A]; A → ε | AA | (A) Consider constructing a three state PDA:

  • 1

[,λ;A]

  • 2

ε,A;λ ),);λ

  • (,A;A)
  • ε,A;AA
  • ],];λ
  • 3

Note, we have introduced ε transitions here Also we have allowed the PDA to push more than one symbol at a time. This is a non-deterministic PDA - what challenges does that have for the notion of acceptance in practice?

Stuart Anderson Context-Free Languages 1 18/20

slide-19
SLIDE 19

university-logo Context-free Languages Pushdown Automata Example Configurations and Computations Example Computation Formalities

Example: Computation

  • 1

[,λ;A]

  • 2

ε,A;λ ),);λ

  • (,A;A)
  • ε,A;AA
  • ],];λ
  • 3

(1, [(())], ⊥) (2, (())], A] ⊥) (2, ())], A)] ⊥) (2, ))], A))] ⊥) (2, ))], ))] ⊥) (2, )], )] ⊥) (2, ], ] ⊥) (2, ε, ⊥)

Stuart Anderson Context-Free Languages 1 19/20

slide-20
SLIDE 20

university-logo Context-free Languages Pushdown Automata Example Configurations and Computations Example Computation Formalities

Definition of A Pushdown Automaton

A PDA P = (Q, Σ, Γ, δ, q0, ⊥, F) Q is the set of states. Σ is the input alphabet. Γ is the stack alphabet. δ ⊆ Q × Σ ∪ {ε} × Γ ∪ {λ} × Q × Γ∗ is the transition relation q0 is the initial state. ⊥∈ Γ is the initial stack symbol. F is the set of final states.

Stuart Anderson Context-Free Languages 1 20/20