cs 275 automata and formal language theory · ii.6.1. push down automata motivation i there are...

50
CS 275 Automata and Formal Language Theory Course Notes Part II: The Recognition Problem (II) Chapter II.6.: Push Down Automata Anton Setzer (Based on a book draft by J. V. Tucker and K. Stephenson) Dept. of Computer Science, Swansea University http://www.cs.swan.ac.uk/csetzer/lectures/ automataFormalLanguage/12/index.html April 14, 2013 CS 275 Chapter II.6. 1/ 50

Upload: others

Post on 09-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

CS 275 Automata and Formal Language TheoryCourse Notes

Part II: The Recognition Problem (II)Chapter II.6.: Push Down Automata

Anton Setzer(Based on a book draft by J. V. Tucker and K. Stephenson)

Dept. of Computer Science, Swansea University

http://www.cs.swan.ac.uk/∼csetzer/lectures/automataFormalLanguage/12/index.html

April 14, 2013

CS 275 Chapter II.6. 1/ 50

Page 2: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

II.6.2. Equivalence of Final-State and Empty-Stack-PDAs

II.6.3. Equivalence of CFG and PDA

CS 275 Chapter II.6. 2/ 50

Page 3: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Motivation

I There are context free languages which are not regular, so they can’tbe recognised by a NFA.

I The problem is that when parsing a language such asL = {ww R | w ∈ {a, b}∗}, when checking the second half of theword, one needs to know the first half of the word in reverse order.

I An NFA has only finitely many states, and therefore only finitememory.

I We can repair the problem by adding a stack to memory, which allowsus to record in case of L the first half of the word.

I The resulting machine will be called a:::::::::::::push-down

:::::::::::::automaton

(:::::PDA).

CS 275 Sect. II.6.1. 3/ 50

Page 4: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Architecture of a PDA

I A PDA consists ofI An input type, containing the input word.

I The input tape will be read only from left to right.

I A finite state q.I A stack.

CS 275 Sect. II.6.1. 4/ 50

Page 5: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Picture

a cb

finite State

q

A

Input TapeStack

top

CS 275 Sect. II.6.1. 5/ 50

Page 6: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Instructions of a PDA

I A PDA has two kinds of instruction:I An empty move instructions which depending on

the state, the top symbol of the stack,without looking at the next symbol on the input tape

chooses anew state,a possibly empty sequence of stack symbolsto replace the top symbol on the stack,and keeps the pointer to the input tape.

I An ordinary instruction, which depending onthe state, the top symbol of the stack,and the next input symbol on the tapechooses anew state,a possibly empty sequence of stack symbolsto replace the top symbol on the stack,and moves the pointer on the input tape forward.

CS 275 Sect. II.6.1. 6/ 50

Page 7: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Execution of a PDA

I The automaton starts with the head on the left most symbol of theinput tape, with some initial state and a start symbol on the stack.

I It will then do the following non-deterministically:I If the stack is empty, it will get stuck.I Otherwise, depending on the state s and the top stack symbol x it

will do one of the following:I If s and x match an empty move transition then

replace the top stack symbol by the new symbols to be put on thestack,switch to a new stateand keep the position on the input tape as before.

I If the next symbol on the input tape is yand s, x , y match an ordinary transition thenreplace the top stack symbol by the new symbols to be put on thestack,switch to a new stateand keep the position on the input tape as before.

CS 275 Sect. II.6.1. 7/ 50

Page 8: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Execution of a PDA

I If none of the above instructions matches, the PDA gets stuck.

I The PDA will stop ifI it gets stuck (because of empty stack or having no operation possible)

while still reading the word,I it wants to read a letter and has reached the end of the word.

I There are two kinds of languages defined from a PDA:I The language accepted by a final state.

A word is accepted by final state, if the PDA reads the complete wordand reaches a state which is final (accepting).

I The language accepted by empty stack.A word is accepted by empty stack, if the PDA reads the completeword and then obtains empty stack.

CS 275 Sect. II.6.1. 8/ 50

Page 9: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Acceptance by Final State vs Empty State

I Clearly, acceptance by final state is natural generalisation of a NFA.

I The standard LL(k), LR(k), LALR parsing algorithms use adeterministic PDA accepting by final state.

I CFG correspond in a natural way to non-deterministic PDA whichaccept by empty stack.

I We will see that the languages accepted by PDAs by empty stack andaccepted by PDAs by final state are equivalent.

I Therefore PDAs accepting by empty state correspond to intermediatemachines for proving the equivalence of context free languages andlanguages accepted by final state by a PDA.

CS 275 Sect. II.6.1. 9/ 50

Page 10: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Acceptance by Final State vs Empty State

I Deterministic PDAs accepting by empty stack only accept languagesL which have the prefix property, i.e. if w is a proper prefix of a wordw ′ ∈ L, then w 6∈ L.

I Since parsing should be ideally done by a deterministic machine,deterministic PDAs accepting by final state form a more naturalmodel.

CS 275 Sect. II.6.1. 10/ 50

Page 11: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Convention Regarding Stacks

I When writing w for the stack, the top element will be theleft most element of w.

CS 275 Sect. II.6.1. 11/ 50

Page 12: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Definition of PDA

Definition

A::::::push

:::::::down

::::::::::::automaton (

:::::PDA) P = (T ,Q, Γ, q0,Z0,F , δ) consists of

I a::::::input

::::::::::alphabet T ;

I a finite set of:::::::states Q;

I a::::::stack

:::::::::::alphabet Γ;

I a:::::start

::::::state q0 ∈ Q;

I an:::::start

:::::::stack

:::::::::symbol Z0 ∈ Γ.

I a set of:::::::::::accepting or

:::::final states F ⊆ Q;

I a:::::::::::transition

:::::::::relation δ ⊆ Q × Γ× (T ∪ {ε})× Q × Γ∗,

where we write (q,Z )a−→ (q′,w) for (q,Z , a, q′,w) ∈ δ.

CS 275 Sect. II.6.1. 12/ 50

Page 13: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Interpretation of the Components

I The input alphabet T will be the alphabet on the tape; so the PDAwill look at elements of T ∗ and check whether it accepts them or not;

I The stack will consist of elements of the stack alphabet Γ.

I The PDA will start in start state q0 with stack consisting of the startstack symbol Z0.

I F will be the set of final states in case we consider the languageaccepted by final state.

I (q,Z )a−→ (q′,w) means that the PDA can, when in state q, and if

the next tape symbol is a move on the tape once to the right, changeto state q′, and replace the top stack symbol Z by w .

I (q,Z )ε−→ (q′,w) means that the PDA can, when in state q and

having top symbol Z on the stack, make a move without looking atthe next letter, and switch to state q′, and replace the top symbol onthe stack by w .

CS 275 Sect. II.6.1. 13/ 50

Page 14: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Example

I We define a PDA which accepts the language

L := {ww R | w ∈ {a, b}∗}

I We will use the stack in order to record the first half of the word, soneed the stack symbols a, b.

I In addition we need the bottom symbol of the stack Z0.After having finished parsing the word, if we read this symbol thePDA will more to an accepting state.

I There are 3 states:I Initial state q0.

In this state the PDA will read the first half of the word, and push itonto the stack.

I An intermediate state q1.When reading the second half of the word, the PDA will be in thisstate and pop symbols from the stack.

I A final accepting state.

CS 275 Sect. II.6.1. 14/ 50

Page 15: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Example

I So our PDA will be

({a, b}, {q0, q1, q2}, {Z0, a, b}, q0,Z0, {q2}, δ)

CS 275 Sect. II.6.1. 15/ 50

Page 16: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Transitions

We have the following transitions in δ:

I Initially the PDA is in state q0, sees stack symbol Z0, and then readsthe first symbol on the tape and pushes it on the stack:

(q0,Z0)a−→ (q0, aZ0)

(q0,Z0)b−→ (q0, bZ0)

I When reading future letters, the PDA will, when in state q0, pushthem on the stack:

(q0, a)a−→ (q0, aa)

(q0, b)a−→ (q0, ab)

(q0, a)b−→ (q0, ba)

(q0, b)b−→ (q0, bb)

CS 275 Sect. II.6.1. 16/ 50

Page 17: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Transitions

I The PDA will guess when it has reached the middle of the stack, andthen switch silently (making a ε-transition, i.e. a transition withoutreading the next symbol) to state q1 without modifying the stack.This can happen at the beginning (without having read a symbol, sostack is Z0), or when it has already pushed some symbol on the stack,so the stack symbol can be any of a, b,Z0:

(q0,Z0)ε−→ (q1,Z0)

(q0, a)ε−→ (q1, a)

(q0, b)ε−→ (q1, b)

CS 275 Sect. II.6.1. 17/ 50

Page 18: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Transitions

I When in state q1, the PDA compares whether the letters it reads areidentical to those it read in the first part, so whether the letter itreads is identical to the letter on the stack.If yes it empties the stack.

(q1, a)a−→ (q1, ε)

(q1, b)b−→ (q1, ε)

CS 275 Sect. II.6.1. 18/ 50

Page 19: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Transitions

I When the stack is emptied, while in state q1, the PDA can move tothe accepting state q2. It will as well empty the stack.If there are more letters to be read, the PDA will get stuck in thatstate, because there will be no transitions in this state.

(q1,Z0)ε−→ (q2, ε)

CS 275 Sect. II.6.1. 19/ 50

Page 20: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

Presentation of the PDA by Table

PDA P

terminals a, b

states q0, q1, q2

stack alphab. Z0, a, b

start state q0

start stack Z0

final q2

transitions (q0,Z0)a−→ (q0, aZ0) (q0,Z0)

b−→ (q0, bZ0)

(q0, a)a−→ (q0, aa) (q0, b)

a−→ (q0, ab)

(q0, a)b−→ (q0, ba) (q0, b)

b−→ (q0, bb)

(q0,Z0)ε−→ (q1,Z0) (q0, a)

ε−→ (q1, a)

(q0, b)ε−→ (q1, b) (q1, a)

a−→ (q1, ε)

(q1, b)b−→ (q1, ε) (q1,Z0)

ε−→ (q2, ε)

Page 21: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Graphical Presentation

We can present a PDA as well graphically by a transition diagram with

I states, start state, final states represented as before,

I transitions labelled with an expression

a,Z/w

where a ∈ T , Z ∈ Γ, w ∈ Γ∗,where a transition from state q to q′ labelled by a,Z/w stands for

(q,Z )a−→ (q′,w)

I The alphabet T is implicitly given by the elements of the alphabetappearing in the transitions.

CS 275 Sect. II.6.1. 21/ 50

Page 22: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Example in Graphical Notation

a, a/εb, b/ε

q0 q1 q2

b, b/bb

a,Z0/aZ0b,Z0/bZ0a, a/aa

b, a/baa, b/ab

ε,Z0/Z0

ε, a/aε, b/b

ε,Z0/ε

CS 275 Sect. II.6.1. 22/ 50

Page 23: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Configuration of a PDA

I The complete state of a PDA is given by its stack and an element ofQ.

I We call this the configuration of a PDA.

Definition

Let P = (T ,Q, Γ, q0,Z0,F , δ) be a PDA.A

:::::::::::::::configuration of P is given by an element (q, z) where q ∈ Q, z ∈ Γ∗.

CS 275 Sect. II.6.1. 23/ 50

Page 24: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Transition of Configurations

I We extend the relation (q,Z )a−→ (q′, z) for q, q′ ∈ Q, Z ∈ Γ, z ∈ Γ∗,

a ∈ T ∪ {ε} to a one step relation

(q, z)a−→

1(q′, z ′)

between configurations (q, z), (q′, z ′) and w ∈ T ∗ expressing that:

I If the PDA has configuration (q, z), then it can make a one stepmovement using a to configuration (q′, z ′).

I Furthermore we define an n-step transition relation

(q, z)w−→

n(q′, z ′)

and a transition relation expressing that we can move from oneconfiguration to another in arbitrarily many steps

(q, z)w−→∗

(q′, z ′)

CS 275 Sect. II.6.1. 24/ 50

Page 25: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

One Step Transition of Configuration

Definition

Let P = (T ,Q, Γ, q0,Z0,F , δ) be a PDA.for q, q′ ∈ Q, Z ∈ Γ, z ∈ Γ∗, define the

::::one

::::::step

:::::::::::transition

::::::::::relation

between configurations (q, z) and (q′, z ′) and a ∈ T ∪ {ε}, written as

(q, z)a−→1

(q′, z ′)::::::::::::::::::

by:

If (q,Z )a−→ (q′, z) , then ∀z ′ ∈ Z ∗(q,Zz ′)

a−→1

(q′, zz ′)

Sincea−→

1extends

a−→, we often write write (q, z)a−→ (q′, z ′) instead of

(q, z)a−→

1(q′, z ′).

CS 275 Sect. II.6.1. 25/ 50

Page 26: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

n-Step Transition of Configuration

Definition

Let P = (T ,Q, Γ, q0,Z0,F , δ) be a PDA.We define for n ∈ N the n-step transition relation between configurations(q, z), (q′, z ′) and w ∈ T ∗, written as (q, z)

w−→n

(q′, z ′) as follows:

(q, z)ε−→

0(q, z)

If (q, z)a−→

1(q′, z ′) and (q′, z ′)

w−→n

(q′′, z ′′) ,

then (q, z)aw−→

n+1(q′′, z ′′)

CS 275 Sect. II.6.1. 26/ 50

Page 27: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Transition of Configuration

Definition

Let P = (T ,Q, Γ, q0,Z0,F , δ) be a PDA.We define the transition relation between configurations (q, z), (q′, z ′) andw ∈ T ∗, written as (q, z)

w−→ (q′, z ′) as follows:

(q, z)w−→∗

(q′, z ′) iff ∃n ∈ N.(q, z)w−→

n(q′, z ′)

CS 275 Sect. II.6.1. 27/ 50

Page 28: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Language Accepted by a PDA

Definition

Let P = (T ,Q, Γ, q0,Z0,F , δ) be a PDA.

I The::::::::::language

::::::::::::accepted

:::by

::::::final

::::::state

:::of

::P , denoted by Lfinal (P)

::::::::,

is defined as

Lfinal (P) := {w ∈ T ∗ | (q0,Z0)w−→∗

(q, z) for some q ∈ F , z ∈ Γ∗}

A:::::::::::final-state

::::::PDA is a PDA P for which we define its language to be

L(P) = Lfinal (P).

CS 275 Sect. II.6.1. 28/ 50

Page 29: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.1. Push Down Automata

Language Accepted by a PDA

Definition (Cont)

I The::::::::::language

::::::::::::accepted

:::by

::::::::empty

::::::stack

:::of

:::P , denoted by

Lempty (P):::::::::

, is defined as

Lempty (P) := {w ∈ T ∗ | (q0,Z0)w−→∗

(q, ε) for some q ∈ Q}

An::::::::::::::empty-stack

::::::PDA is a PDA P for which we define its language

to be L(P) = Lempty (P).

CS 275 Sect. II.6.1. 29/ 50

Page 30: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.2. Equivalence of Final-State and Empty-Stack-PDAs

II.6.1. Push Down Automata

II.6.2. Equivalence of Final-State and Empty-Stack-PDAs

II.6.3. Equivalence of CFG and PDA

CS 275 Sect. II.6.2. 30/ 50

Page 31: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.2. Equivalence of Final-State and Empty-Stack-PDAs

Equivalence Acceptance by Final/Empty Stack

Theorem

Let L be a language. The following are equivalent:

(1) L = L(P) for a final state PDA P.

(2) L = L(P) for an empty stack PDA P.

CS 275 Sect. II.6.2. 31/ 50

Page 32: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.2. Equivalence of Final-State and Empty-Stack-PDAs

Proof Idea of (1) ⇒ (2)

Let P = (T ,Q, Γ, q0,Z0,F , δ).The idea for constructing an empty stack PDA P ′ is as follows:

I P ′ operates essentially as P.

I If P reaches an accepting state, then P ′ can switch to a special state.In that state it empties using ε-transition the stack and and thereforeaccepts the string.

I Full details can be found in Additional Material.

CS 275 Sect. II.6.2. 32/ 50

Page 33: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.2. Equivalence of Final-State and Empty-Stack-PDAs

Proof of (2) ⇒ (1)

Let P = (T ,Q, Γ, q0,Z0,F , δ).The idea for constructing a final state PDA P ′ is as follows:

I P ′ keeps a special stack symbol Z ′0 at the bottom of the stack.

I It operates as P, until it observes that the top stack symbol is Z ′0.

I This indicates that P would have reached an empty stack andtherefore accepted the string.

I Therefore P ′ moves into a special accepting state qfinal andterminates.

I Full details can be found in the Additional Material.

CS 275 Sect. II.6.2. 33/ 50

Page 34: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

II.6.1. Push Down Automata

II.6.2. Equivalence of Final-State and Empty-Stack-PDAs

II.6.3. Equivalence of CFG and PDA

CS 275 Sect. II.6.3. 34/ 50

Page 35: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Equivalence Acceptance by Final/Empty Stack

We are going to show that the context free languages are exactly thelanguages which can be recognised by a (non-deterministic) empty stackPDA.Since empty stack PDA are final state PDA recognise the same languages,the context free languages are exactly the languages which can berecognised by a PDA.

CS 275 Sect. II.6.3. 35/ 50

Page 36: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Equivalence of empty stack PDA and CFG

Theorem

Let L be a language. The following are equivalent:

(1) L = L(G ) for a CFG G .

(2) L = L(P) for an empty stack PDA P.

CS 275 Sect. II.6.3. 36/ 50

Page 37: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Corollary

Because empty stack and final state PDAs are equivalent, from thetheorem follows immediately the following corollary:

Corollary

Let L be a language. The following are equivalent:

(1) L = L(G ) for a CFG G .

(2) L = L(P) for an empty stack PDA P.

(2) L = L(P) for a final state PDA P.

CS 275 Sect. II.6.3. 37/ 50

Page 38: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Proof of the Theorem

We will only show (1) ⇒ (2), (2) ⇒ (1) is quite sophisticated.

CS 275 Sect. II.6.3. 38/ 50

Page 39: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Proof of (1) ⇒ (2)

Assume G = (T ,N,S ,P) is a CFG. We need to construct a PDA whichsimulates G .There are two ways of constructing such a PDA, one follows theLL-parsing method, one uses the LR-parsing method.LL parsing will result in a PDA with a single state. The configuration canbe given by its stack.We will introduce in the main slides LL-parsing and in the Additionalmaterial LR-parsing.Deterministic versions of of them are the basis for many parsers.In the additional material you will find the full proof of (1) ⇒ (2) byshowing for the PDA based on the LL parser that it is equivalent to theCFG.

CS 275 Sect. II.6.3. 39/ 50

Page 40: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

PDA based on LL-Parsing

I::::::::::::LL-parsing stands for left-to-right parsing based on a leftmostderivation.

I It constructs a leftmost derivation top down, and is therefore anexample of a

::::top

:::::::down

::::::::parser.

I We take as example the grammar with the rules

S −→ ACA −→ aAbA −→ abC −→ cCdC −→ cd

I We consider a left-most derivation

S ⇒ AC ⇒ aAbC ⇒ aabbC ⇒ aabbcCd ⇒ aabbccdd

CS 275 Sect. II.6.3. 40/ 50

Page 41: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Example of LL Parsing

S −→ ACA −→ aAb A −→ abC −→ cCd C −→ cdS ⇒ AC ⇒ aAbC ⇒ aabbC ⇒ aabbcCd ⇒ aabbccdd

I We start on our PDA with stack S for the start symbol.I So we have stack S

I We guess that the rule is S −→ AC , and replace the top symbol S onthe stack by AC .

I So we have stack AC (the top symbol of the stack is the left most one).

I We guess that the rule for the top symbol is A −→ aAb, and replacethe top symbol A on the stack by aAb.

I So we have stack aAbC (the top of the stack is the symbol most to theleft).

CS 275 Sect. II.6.3. 41/ 50

Page 42: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Example of LL Parsing

S −→ ACA −→ aAb A −→ abC −→ cCd C −→ cdS ⇒ AC ⇒ aAbC ⇒ aabbC ⇒ aabbcCd ⇒ aabbccdd

I We have now stack aAbC . We can accept the letter a, and removethis symbol a from the stack.

I So we have stack AbC

I We guess that the rule is A −→ ab, and replace the top symbol A onthe stack by ab.

I So we have stack abbC

I Now we can 3 times accept a letter, namely a, b, b and remove themfrom the stack.

I So we have stack C .

CS 275 Sect. II.6.3. 42/ 50

Page 43: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Example of LL Parsing

S −→ ACA −→ aAb A −→ abC −→ cCd C −→ cdS ⇒ AC ⇒ aAbC ⇒ aabbC ⇒ aabbcCd ⇒ aabbccdd

I We have now stack C . We use rule C −→ cCd and replace C bycCd .

I So we have stack cCd

I We consume the next letter c and accept it and remove c from thestack.

I So we have stack Cd

I We expand C using C −→ cd to cd .I So we have stack cdd .

I Now we consume and accept letters c, d , d , clear them from thestack, and then accept the word because we have reached the emptystack.

CS 275 Sect. II.6.3. 43/ 50

Page 44: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

S

A

C C C C

CCC

A Ab

a

b bba

bbb

d

c

CC

d

d

d

c

d

d d

accept

ε ε ε

ε

ε

a

a

bbc

c d d

CS 275 Sect. II.6.3. 44/ 50

Page 45: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

LL(k) Parsing

I We had to guess which production to use.

I LL(k) is an algorithm to decide by using a lookahead of the next ksymbols, which production to use.

I For instance when the stack was AC we could have guessed byknowing that the next two letters are a, a, that we need to use theproduction A −→ aAb and not A −→ ab, since the latter would giveas next two letters ab.

I Usually only a lookahead of 1 symbol is used, but most standardgrammars (e.g. Java) have no LL(1) grammars.

CS 275 Sect. II.6.3. 45/ 50

Page 46: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Invariant

I The invariant kept above was that when having consumed string wand obtained stack v , then we have S ⇒∗ wv .

CS 275 Sect. II.6.3. 46/ 50

Page 47: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Resulting PDA

I We obtain a PDA with a single state. Therefore we omit the state,and ignore the start state, set of states, final states, the state inconfigurations, and have transitions

Za−→ z

for Z ∈ Γ, a ∈ T , z ∈ Γ∗.

I The stack symbols were the alphabet used in strings occurring in thederivations, i.e. T ∪ N.

I The start stack symbol was S .

CS 275 Sect. II.6.3. 47/ 50

Page 48: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Resulting PDA

I The PDA has the following productions:I If the top symbol is a non-terminal A, and there was a rule A −→ w ,

we could replace A by w , and make an ε-transition.So we have in this case a transition

Aε−→ w

I If the top symbol was a terminal a, then we could accept letter a andpop the symbol from the stack.So we have transitions

aa−→ ε

CS 275 Sect. II.6.3. 48/ 50

Page 49: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

Resulting Empty Stack Single State PDA

The empty stack PDA derived from G = (T ,N,S ,P) is as follows:

PDA P

terminals T

states single state

stack alphab. T ∪ N

start stack S

transitions Aε−→ w if A −→ w ∈ P

aa−→ ε if a ∈ T

In the additional material there is a proof L(P) = L(G ).

CS 275 Sect. II.6.3. 49/ 50

Page 50: CS 275 Automata and Formal Language Theory · II.6.1. Push Down Automata Motivation I There are context free languages which are not regular, so they can’t be recognised by a NFA

II.6.3. Equivalence of CFG and PDA

LR Parsing and Proof of Equivalence

This can be found in the Additional Material

CS 275 Sect. II.6.3. 50/ 50