SlideShare a Scribd company logo
1 of 96
Download to read offline
1
CS311 Computational Structures
Context-free Languages:
Grammars and Automata
Andrew Black
Andrew Tolmach
Lecture 8
Chomsky hierarchy
In 1957, Noam Chomsky published Syntactic
Structures, an landmark book that defined the so-
called Chomsky hierarchy of languages
2
original name language generated productions:
Type-3 Grammars Regular
Type-2 Grammars Contex-free
Type-1 Grammars Context-sensitive
Type-0 Grammars Recursively-enumerable no restriction
∩
∩
∩
A → γ
αAβ → αγβ
A, B: variables, a, b terminals, α, β sequences of terminals and variables
A → α and A → αB
Regular languages
• Closed under ∪∩∗·and ⎯
• Recognizable by finite-state automata
• Denoted by Regular Expressions
• Generated by Regular Grammars
3
Context-free Grammars
4
Context-free Grammars
• More general productions than regular
grammars
S → w
 
 
 where w is any string of terminals

 
 
 
 
 and non-terminals
4
Context-free Grammars
• More general productions than regular
grammars
S → w
 
 
 where w is any string of terminals

 
 
 
 
 and non-terminals
• What languages do these grammars
generate?
S →(A)
A → ε | aA | ASA
4
Context-free Grammars
• More general productions than regular
grammars
S → w
 
 
 where w is any string of terminals

 
 
 
 
 and non-terminals
• What languages do these grammars
generate?
S →(A)
A → ε | aA | ASA
4
S → ε | aSb
Context-free languages more
general than regular languages
5
Context-free languages more
general than regular languages
• {anbn | n ≥ 0} is not regular
5
Context-free languages more
general than regular languages
• {anbn | n ≥ 0} is not regular
‣ but it is context-free
5
Context-free languages more
general than regular languages
• {anbn | n ≥ 0} is not regular
‣ but it is context-free
• Why are they called “context-free”?
5
Context-free languages more
general than regular languages
• {anbn | n ≥ 0} is not regular
‣ but it is context-free
• Why are they called “context-free”?
‣ Context-sensitive grammars allow more than one
symbol on the lhs of productions
5
Context-free languages more
general than regular languages
• {anbn | n ≥ 0} is not regular
‣ but it is context-free
• Why are they called “context-free”?
‣ Context-sensitive grammars allow more than one
symbol on the lhs of productions
° xAy → x(S)y can only be applied to the non-terminal A
when it is in the context of x and y
5
Context-free grammars are widely
used for programming languages
• From the definition of Algol-60:

 procedure_identifier::= identifier.

 actual_parameter::= string_literal | expression | array_identifier | switch_identifier | procedure_identifier.

 letter_string::= letter | letter_string letter.

 parameter_delimiter::= "," | ")" letter_string ":" "(".

 actual_parameter_list::= actual_parameter | actual_parameter_list parameter_delimiter actual_parameter.

 actual_parameter_part::= empty | "(" actual_parameter_list} ")".

 function_designator::= procedure_identifier actual_parameter_part.
• We say: “most programming languages are
context-free”
‣ This isnʼt strictly true
‣ … but we pretend that it is!
6
Example

 adding_operator::= "+" | "−" .

 multiplying_operator::= "×" | "/" | "÷" .

 primary::= unsigned_number | variable | function_designator | "(" arithmetic_expression ")".

 factor::= primary | factor | factor power primary.

 term::= factor | term multiplying_operator factor.

 simple_arithmetic_expression::= term | adding_operator term |
simple_arithmetic_expression adding_operator term.

 if_clause::= if Boolean_expression then.

 arithmetic_expression::= simple_arithmetic_expression |
if_clause simple_arithmetic_expression else arithmetic_expression.
if a < 0 then U+V else if a * b < 17 then U/V else if k <> y then V/U else 0
7
Example derivation in a Grammar
• Grammar: start symbol is A
A → aAa
A → B
B → bB
B → ε
• Sample Derivation:
A aAa aaAaa aaaAaaa aaaBaaa
aaabBaaa aaabbBaaa aaabbaaa
• Language?
8
9
A
Aa
a
a
b
b
ε
a
a
a
A
A
B
B
B
A
A
a a a b b ε a a a
A
A
B
B
B
Derivations in Tree Form
Arithmetic expressions in a programming
language
• Derive: a + a × a
10
see more easily what this language is if you think of a as a left parenthesis
and b as a right parenthesis ")". =ewed in this way, L(G3)is the language of
strings of properly nested parentheses.
EXAMPLE 2.4 ........................................................................................................................
Consider grammar G4 = (V,S ,R, (EXPR)).
V is {(EXPR),(TERM),(FACTOR)}and E is {a,+, x, (, I}. The rules are
(EXPR)+ (EXPR)+(TERM)1 (TERM)
(TERM)+ (TERM)x (FACTOR)1 (FACTOR)
(FACTOR)+ ( (EXPR)) 1 a
The two strings a+axa and (a+a)xa can be generated with grammar
The parse trees are shown in the following figure.
11
FIGURE 2.5
Parse trees for the strings a+axa and (a+a)xa
11
FIGURE 2.5
Parse trees for the strings a+axa and (a+a)xa
Notice how the grammar gives the meaning a + (a×a)
Grammars in real computing
• CFGʼs are universally used to describe the
syntax of programming languages
• Perfectly suited to describing recursive syntax of
expressions and statements
• Tools like compilers must parse programs; parsers
can be generated automatically from CFGʼs
• Real languages usually have a few non-CF bits
• CFGʼs are also used in XML DTDʼs
12
Formal definition of CFG
• A Context-free grammar is a 4-tuple
(V, Σ, R, S) where
1. V is a finite set called the variables (non-
terminals)
2. Σ is a finite set (disjoint from V) called the
terminals,
3. R is a finite set of rules, where each rule maps
a variable to a string s ∈ (V ∪ Σ)*
4. S ∈ V is the start symbol
13
Definition of Derivation
• Let u, v and w be strings in (V ∪ Σ)*, and let
A →w be a rule in R,
• then uAv uwv (read: uAv yields uwv)
• We say that u v (read: u derives v) if
u = v or there is a sequence u1, u2, … uk,
k ≥ 0, s.t. u u1 u2 … uk v
• The language of the grammar is

 {w ∈ Σ* | S w }
14
*
*
Derivations Parse Trees
• Each derivation can be viewed as a
parse tree with variables at the internal
nodes and terminals at the leaves
• Start symbol is at the root
• Each yield step uAv uwv where w=w1w2...wn
corresponds to a node labeled A with children
w1,w2,...,wn.
• The final result in Σ* can be seen by reading
the leaves left-to-right
15
Simple CFG examples
• Find grammars for:
• L = {w ∈ {a,b}* | w begins and ends with the
same symbol}
• L = {w ∈ {a,b}* | w contains an odd number of
aʼs}
• L [(ε + 1)(01)*(ε + 0)]
• Draw example derivations
16
All regular languages have
context free grammars
• Proof:
• Regular language is accepted by an NFA.
• We can generate a regular grammar from the
NFA (Lecture 6, Hein Alg. 11.11)
• Any regular grammar is also a CFG. (Immediate
from the definition of the grammars).
17
Example
• S→aQ1 S→bR1
• Q1 →aQ1 Q1 →bQ2
• Q2 →aQ1 Q2 →bQ2
• R1 →aR2 R1 →bR1
• R2 →aR2 R2 →bR1
• Q1 →ε R1 →ε
• Resulting grammar may be quite
different from one we designed by hand.
18
Some CFGʼs generate
non-regular languages
• Find grammars for the following
languages
• L = {anbman | a,b ≥ 0}
• L = {w ∈ {a,b}* | w contains equal numbers of
aʼs and bʼs}
• L = {wwR | w ∈ {a,b}*}
• Draw example derivations
19
Ambiguity
• A grammar in which the same string can
be given more than one parse tree is
ambiguous.
• Example: another grammar for
arithmetic expressions
‹EXPR› → 
 ‹EXPR› + ‹EXPR› |

 
 
 
 ‹EXPR› × ‹EXPR› |

 
 
 
 ( ‹EXPR› )

 
 
 
 | a
• Derive: a + a × a
20
• This grammar is ambiguous: there are
two different parse trees for a + a × a
• Ambiguity is a bad thing if weʼre
interested in the structure of the parse
• Ambiguity doesnʼt matter if weʼre interested
only in defining a language.
21
GURE 2.6
e two parse trees for the string a+axain grammar G5
This grammar doesn't capture the usual precedence relations and s
Leftmost Derivations
• In general, in any step of a derivation, there
might be several variables that can be reduced
by rules of the grammar.
• In a leftmost derivation, we choose to always
reduce the leftmost variable.
• Example: given grammar S → aSb | SS | ε
• A left-most derivation:
S aSb aSSb aaSbSb aabSb aabb
• A non-left-most derivation:
S aSb aSSb aSb aaSbb aabb
22
Ambiguity via
left-most derivations
• Every parse tree corresponds to a
unique left-most derivation
• So if a grammar has more than one left-
most derivation for some string, the
grammar is ambiguous
• Note: merely having two derivations (not
necessarily left-most) for one string is not
enough to show ambiguity
23
Ambiguity
E E ⨉ E E + E ⨉ E
a + E ⨉ E a + a ⨉ E
a + a ⨉ a
E E + E a + E
a + E ⨉ E a + a ⨉ E
a + a ⨉ a
24
GURE 2.6
e two parse trees for the string a+axain grammar G5
This grammar doesn't capture the usual precedence relations and so
up the + before the x or vice versa. In contrast grammar Ga, gen
Context-free languages
• Closed under ∪, ∗ and ·, and under
∩ with a regular language
‣ How do we prove these properties?
• Not closed under intersection,
complement or difference
• Recognizable by pushdown automata
‣ A pushdown automaton is a generalization of a
finite-state automaton
25
Pushdown Automata
• Why canʼt a FSA
recognize anbn?
‣ “storage” is finite
• How can we fix the
problem?
‣ add unbounded storage
• Whatʼs the simplest kind
of unbounded storage
‣ a pushdown stack
26
finite
state
control
input
accept
reject
stack
History
• PDAs independently invented by
Oettinger [1961] and Schutzenberger
[1963]
• Equivalence between PDAs and CFG
known to Chomsky in 1961; first published
by Evey [1963].
27
Executing a PDA
• The behavior of a PDA at any point depends on
⟨state,stack,unread input⟩
• PDA begins in start state with stated symbol on the
stack
• On each step it optionally reads an input character,
pops a stack symbol, and non-deterministically
chooses a new state and optionally pushes one or
more stack symbols
• PDA accepts if it is in a final state and there is no
more input.
28
Example PDA
29
he PDA Ati that recognizes {on1" n >01
Example Execution: 0011
30
he PDA Ati that recognizes {on1" n >01
Stack
Begin in initial state
with empty stack
Example Execution: 0011
31
he PDA Ati that recognizes {on1" n >01
Stack
$
Example Execution: 0011
32
he PDA Ati that recognizes {on1" n >01
Stack
$
0
Example Execution: 0011
33
he PDA Ati that recognizes {on1" n >01
Stack
$
0
0
Example Execution: 0011
34
he PDA Ati that recognizes {on1" n >01
Stack
$
0
Example Execution: 0011
35
he PDA Ati that recognizes {on1" n >01
Stack
$
Example Execution: 0011
36
he PDA Ati that recognizes {on1" n >01
Stack
Accept!
PDAʼs can keep count!
• This PDA can recognize {0n1n | n ≥ 0} by
‣ First, pushing a symbol on the stack for each 0
‣ Then, popping a symbol off the stack for each 1
‣ Accepting iff the stack is empty when the end of input
is reached (and not before)
• The size of the stack is unbounded.
‣ That is, no matter how big the stack grows, it is
always possible to push another symbol on it.
‣ So PDAʼs can use the stack to count arbitrarily high
37
Pushdown Automata (PDA)
• A pushdown automaton    is defined as a
7-tuple:                                     , where:
‣ Q is a set of states,             is the start state
‣ Σ is the input alphabet,
‣   is the stack alphabet,            is the initial stack symbol
‣                                                  is the transition function
‣             is a set of final states, and
‣                      , the set X augmented with
38
M
Q
Γ
q0 ∈ Q
F ⊆ Q
εXε = X ∪ {ε}
Z0 ∈ Γ
M = (Q, Σ, Γ, δ, q0, Z0, F)
δ : (Q × Σε × Γε) → P{Q × Γ∗
}
Executing a PDA
• The configuration (or PDA + Hopcroftʼs
instantaneous description, ID) of a PDA
consists of
1. The PDA,
2. The current state of the PDA,
3. the remaining input, and
4. The contents of its stack.
39
• We defined
‣ The transitions                are applicable iff
°     is the current state,
°             , or the next character on the input tape is    , and
°             , or the top of the stack is
‣ If you select a transition             , then
° The new state is       
° if              ,      is popped off of the stack, and
° the (possibly empty) sequence of symbols     is pushed
onto the stack
Transitions
40
δ(q, a, γ)
q
a
q
γ
γ
(q , ω)
ω
γ = ε
γ = ε
a = ε
δ : (Q × Aε × Γε) → P{Q × Γ∗
}
PDAs are non-deterministic
For two reasons:
• The transition function δ answers a set
• We have the choice of taking an ε-
transition, or reading an input symbol
41
Trivial Example
Hein Example 12.4
42
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
• δ(s, ε, Y) = {(0, XY)}
• δ(0, ε, X) = {(0, ε)}
• δ(0, ε, Y) = {(f, Y)}
 
push(X) ε
ε
ε
Accepting a String
• There are two ways in which a PDA can
accept a string:
1. final state is in the set F (implies F non-empty)
2. if F is empty (there are no final states), then the
PDA accepts when the stack is empty
• The previous example accepts by …
43
Acceptance by Final State
44
A run of PDA M = (Q, A, Γ, δ, q0, γ0, F) is a sequence
such that:
(q0, γ0)
a0
→ (q1, s1)
a1
→ · · ·
an−1
→ (qn, sn)
for all i ∈ [0 .. n − 1]. (qi+1, γi+1) ∈ δ(qi, ai, γi) and
with q0, . . . , qn ∈ Q, s1, . . . , sn ∈ Γ∗
, and a0, . . . , an−1 ∈ A
si = γiti and si+1 = γi+1ti for some ti ∈ Γ∗
, and
w = a0a1a2 . . . an−1 is the input.
The run accepts     if qn ∈ F.w
The language of                  is given byM, L(M)
L(M) = {w ∈ A∗
| w is accepted by some run of M}
Acceptance by Empty Stack
• There is an alternative definition of
acceptance:
‣ If the set of final states is empty, then
‣ the PDA accepts an input string if it can read all of
the characters of the string and end up in a state
in which the stack is empty.
45
Acceptance by Empty Stack
46
such that:
(q0, γ0)
a0
→ (q1, s1)
a1
→ · · ·
an−1
→ (qn, sn)
for all i ∈ [0 .. n − 1]. (qi+1, γi+1) ∈ δ(qi, ai, γi) and
with q0, . . . , qn ∈ Q, s1, . . . , sn ∈ Γ∗
, and a0, . . . , an−1 ∈ A
si = γiti and si+1 = γi+1ti for some ti ∈ Γ∗
, and
w = a0a1a2 . . . an−1 is the input.
The run accepts     ifw
The language of                  is given byM, L(M)
L(M) = {w ∈ A∗
| w is accepted by some run of M}
A run of PDA M = (Q, A, Γ, δ, q0, γ0, ∅) is a sequence
sn = ε.
It doesnʼt matter!
• PDAs that accept by empty stack and
PDAs that accept by final state have
equivalent power
• What does this mean?
‣ That given either one, we can build the other
‣ Consequently, the class of languages that they
accept are the same
47
Example
(Hein 12.2)
        means that (in state 0), with input a and
with X on the top of the stack, we can transition
into state 0, and put an additional X onto the
stack.
48
push(X)
ε
Example
(Hein 12.2)
        means that (in state 0), with input a and
with X on the top of the stack, we can transition
into state 0, and put an additional X onto the
stack.
48
Empty stack
acceptance
push(X)
ε
49
M = ({0,1}, {a, b},
       {X}, δ, 0, X, ∅)
Example
(Hein 12.2)
push(X)
ε
‣ δ(0, a, X) = {(0, XX)}
‣ δ(0, ε, X) = {(1, ε)}
‣ δ(1, b, X) = {(1, ε)}
 
49
M = ({0,1}, {a, b},
       {X}, δ, 0, X, ∅)
Example
(Hein 12.2)
push(X)
ε
Transformation to a Final State PDA
50
ε
Transformation to a Final State PDA
50
ε
Transformation to a Final State PDA
50
S
ε
Transformation to a Final State PDA
50
SStart
ε
Transformation to a Final State PDA
50
SStart
_____
push(Y)
ε
Transformation to a Final State PDA
50
FSStart
_____
push(Y)
ε
Transformation to a Final State PDA
50
FSStart
_____
push(Y)
ε
Transformation to a Final State PDA
50
FSStart
_____
push(Y)
ε
Transformation to a Final State PDA
50
FS
   ε, Y   
  push(X)Start
_____
push(Y)
ε
Transformation to a Final State PDA
50
FS
   ε, Y   
  push(X)Start
 ε, Y 
  nop
 ε, Y 
  nop_____
push(Y)
ε
Transformation to a Final State PDA
‣ N = ({0,1, S, F}, {a, b}, {X,Y}, δ´, S, Y, {F})
50
FS
   ε, Y   
  push(X)Start
 ε, Y 
  nop
 ε, Y 
  nop_____
push(Y)
ε
Transformation to a Final State PDA
‣ N = ({0,1, S, F}, {a, b}, {X,Y}, δ´, S, Y, {F})
‣ δ´=δ+δ´(S, ε, Y) = {(0, XY)} +
     δ´(0, ε, Y) = {(F, ε)} + δ´(1, ε, Y) = {(F, ε)}
50
FS
   ε, Y   
  push(X)Start
 ε, Y 
  nop
 ε, Y 
  nop_____
push(Y)
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
          
push(Y)
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S
          
push(Y)
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S
          
push(Y)
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S
_____
push($)
          
push(Y)
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S
_____
push($)
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S
_____
push($)    ε, $   
  push(Y)
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S e
_____
push($)    ε, $   
  push(Y)
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S e
_____
push($)    ε, $   
  push(Y)
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S e
_____
push($)    ε, $   
  push(Y)
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S e
_____
push($)    ε, $   
  push(Y)
 ε, ε 
  
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S e
_____
push($)    ε, $   
  push(Y)
 ε, ε 
  
 ε, Y 
  pop
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S e
_____
push($)    ε, $   
  push(Y)
 ε, ε 
  
 ε, Y 
  pop
 ε, X 
  pop
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S e
_____
push($)    ε, $   
  push(Y)
 ε, ε 
  
 ε, $ 
  pop
 ε, Y 
  pop
 ε, X 
  pop
ε
ε
ε
Transformation to an EmptyStack PDA
51
Context-Free Languages and Pushdown Automata
Start
pop
!, X
0 f
push(X)
!, Y
s
nop
!, Y !
As the example shows, algorithm (12.5) doesn’t always give th
s. For example, a simpler PDA to accept {"} by final state ca
follows:
Start
S e
_____
push($)    ε, $   
  push(Y)
 ε, ε 
  
 ε, $ 
  pop
 ε, Y 
  pop
 ε, X 
  pop
ε
ε
ε
Other Variations
‣ Hein says: “the execution of a PDA always begins
with one symbol on the stack”
‣ Other authors say “the stack is initially empty”
‣ Does it matter?
52
Other Variations
‣ Hein says: “the execution of a PDA always begins
with one symbol on the stack”
‣ Other authors say “the stack is initially empty”
‣ Does it matter?
52
• Can you always convert an initially empty PDA to
one with an initial symbol on the stack?
Other Variations
‣ Hein says: “the execution of a PDA always begins
with one symbol on the stack”
‣ Other authors say “the stack is initially empty”
‣ Does it matter?
52
• Can you always convert an initially empty PDA to
one with an initial symbol on the stack?
• Can you always convert a PDA with an initial
symbol to one whose stack is initially empty?
Stack Operations
• Hopcroft et al. says:
‣ on each transition, to look at the top of the stack you must pop it, but
‣ you can push on as many stack symbols as you like, including the
one that you just popped.
• Other authors say
‣ you can push zero or one symbols onto the stack
• Hein says:
‣ on each transition, you can push, pop or nop the stack, but you
canʼt do more than one thing
• Does it matter?
53
More Examples
L1= {anbn: n≥0}
54
_____
push($)
  a,ε  
push(#)
 b,# 
 pop
 ε,$ 
 nop
 ε, ε 
  nop
Palindromes of even length
L2= {wwR⎮ w ∈ {a, b}*}
55
_____
push($)
  a,ε  
push(a)
 ε,$ 
 nop
 ε, ε 
  nop
  b,ε  
push(b)
b,b
pop
a,a
pop
Palindromes of even length
L2= {wwR⎮ w ∈ {a, b}*}
55
_____
push($)
  a,ε  
push(a)
 ε,$ 
 nop
 ε, ε 
  nop
  b,ε  
push(b)
b,b
pop
a,a
pop
• This machine is non-deterministic. (Why?)
Palindromes of even length
L2= {wwR⎮ w ∈ {a, b}*}
55
_____
push($)
  a,ε  
push(a)
 ε,$ 
 nop
 ε, ε 
  nop
  b,ε  
push(b)
b,b
pop
a,a
pop
• This machine is non-deterministic. (Why?)
• Can you design a deterministic PDA for L2?
Balanced Parenthesis
56
_____
push($)
 ε,$ 
 nop
  (,ε  
push(#)
 ),#
pop
L3= {w ∈ {(, )}* : w is a balanced string of parenthesis}
Equal numbers of as and bs
57
_____
push($)
 ε,$ 
 nop
  b,ε  
push(b)
 b,a 
 pop
  a,ε  
push(a)
 a,b 
 pop

More Related Content

What's hot

context free language
context free languagecontext free language
context free languagekhush_boo31
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Animesh Chaturvedi
 
Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Srimatre K
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4shah zeb
 
Theory of computing
Theory of computingTheory of computing
Theory of computingRanjan Kumar
 
Cs6503 theory of computation november december 2015 be cse anna university q...
Cs6503 theory of computation november december 2015  be cse anna university q...Cs6503 theory of computation november december 2015  be cse anna university q...
Cs6503 theory of computation november december 2015 be cse anna university q...appasami
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOCAbhayDhupar
 
2.5 ambiguity in context free grammars
2.5 ambiguity in context free grammars2.5 ambiguity in context free grammars
2.5 ambiguity in context free grammarsSampath Kumar S
 
Cs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papersCs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papersappasami
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of LanguageDipankar Boruah
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
1.1. the central concepts of automata theory
1.1. the central concepts of automata theory1.1. the central concepts of automata theory
1.1. the central concepts of automata theorySampath Kumar S
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR Zahid Parvez
 

What's hot (20)

context free language
context free languagecontext free language
context free language
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Cs6503 theory of computation november december 2015 be cse anna university q...
Cs6503 theory of computation november december 2015  be cse anna university q...Cs6503 theory of computation november december 2015  be cse anna university q...
Cs6503 theory of computation november december 2015 be cse anna university q...
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOC
 
Language
LanguageLanguage
Language
 
2.5 ambiguity in context free grammars
2.5 ambiguity in context free grammars2.5 ambiguity in context free grammars
2.5 ambiguity in context free grammars
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
 
Cs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papersCs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papers
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of Language
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
1.1. the central concepts of automata theory
1.1. the central concepts of automata theory1.1. the central concepts of automata theory
1.1. the central concepts of automata theory
 
Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 

Viewers also liked

Class7
 Class7 Class7
Class7issbp
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free GrammarsMarina Santini
 
Context free languages
Context free languagesContext free languages
Context free languagesJahurul Islam
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauagesdanhumble
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Abhimanyu Mishra
 
Lecture 7: Definite Clause Grammars
Lecture 7: Definite Clause GrammarsLecture 7: Definite Clause Grammars
Lecture 7: Definite Clause GrammarsCS, NcState
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicLeyo Stephen
 
Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?Animesh Chaturvedi
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Abhimanyu Mishra
 
Formal language & automata theory
Formal language & automata theoryFormal language & automata theory
Formal language & automata theoryNYversity
 
Normal forms cfg
Normal forms   cfgNormal forms   cfg
Normal forms cfgRajendran
 
Class8
 Class8 Class8
Class8issbp
 
Lect 5 2d clipping
Lect 5 2d clippingLect 5 2d clipping
Lect 5 2d clippingmajicyoung
 
Simplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationSimplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationNikhil Pandit
 

Viewers also liked (20)

Class7
 Class7 Class7
Class7
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Properties of cfg
Properties of cfgProperties of cfg
Properties of cfg
 
Context free languages
Context free languagesContext free languages
Context free languages
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3
 
Lecture 7: Definite Clause Grammars
Lecture 7: Definite Clause GrammarsLecture 7: Definite Clause Grammars
Lecture 7: Definite Clause Grammars
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
 
Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2
 
Formal language & automata theory
Formal language & automata theoryFormal language & automata theory
Formal language & automata theory
 
Normal forms cfg
Normal forms   cfgNormal forms   cfg
Normal forms cfg
 
L3 cfg
L3 cfgL3 cfg
L3 cfg
 
Reintroducing Web Technology
Reintroducing Web TechnologyReintroducing Web Technology
Reintroducing Web Technology
 
Class8
 Class8 Class8
Class8
 
Lect 5 2d clipping
Lect 5 2d clippingLect 5 2d clipping
Lect 5 2d clipping
 
06 clipping
06 clipping06 clipping
06 clipping
 
Simplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationSimplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of Computation
 
Fafl notes [2010] (sjbit)
Fafl notes [2010] (sjbit)Fafl notes [2010] (sjbit)
Fafl notes [2010] (sjbit)
 
Computational linguistics
Computational linguisticsComputational linguistics
Computational linguistics
 

Similar to Context free langauges (20)

contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
 
9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
intro.ppt
intro.pptintro.ppt
intro.ppt
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Module 1 TOC.pptx
Module 1 TOC.pptxModule 1 TOC.pptx
Module 1 TOC.pptx
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
01-Introduction&Languages.pdf
01-Introduction&Languages.pdf01-Introduction&Languages.pdf
01-Introduction&Languages.pdf
 
FLAT Notes
FLAT NotesFLAT Notes
FLAT Notes
 
Control structure
Control structureControl structure
Control structure
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
 
Regular expressions h1
Regular expressions h1Regular expressions h1
Regular expressions h1
 
Unit-1-part-1.pptx
Unit-1-part-1.pptxUnit-1-part-1.pptx
Unit-1-part-1.pptx
 
01.ppt
01.ppt01.ppt
01.ppt
 
Final formal languages
Final formal languagesFinal formal languages
Final formal languages
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
 
Formal language
Formal languageFormal language
Formal language
 
Syntax
SyntaxSyntax
Syntax
 
L_2_apl.pptx
L_2_apl.pptxL_2_apl.pptx
L_2_apl.pptx
 

Recently uploaded

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 

Recently uploaded (20)

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 

Context free langauges

  • 1. 1 CS311 Computational Structures Context-free Languages: Grammars and Automata Andrew Black Andrew Tolmach Lecture 8
  • 2. Chomsky hierarchy In 1957, Noam Chomsky published Syntactic Structures, an landmark book that defined the so- called Chomsky hierarchy of languages 2 original name language generated productions: Type-3 Grammars Regular Type-2 Grammars Contex-free Type-1 Grammars Context-sensitive Type-0 Grammars Recursively-enumerable no restriction ∩ ∩ ∩ A → γ αAβ → αγβ A, B: variables, a, b terminals, α, β sequences of terminals and variables A → α and A → αB
  • 3. Regular languages • Closed under ∪∩∗·and ⎯ • Recognizable by finite-state automata • Denoted by Regular Expressions • Generated by Regular Grammars 3
  • 5. Context-free Grammars • More general productions than regular grammars S → w where w is any string of terminals and non-terminals 4
  • 6. Context-free Grammars • More general productions than regular grammars S → w where w is any string of terminals and non-terminals • What languages do these grammars generate? S →(A) A → ε | aA | ASA 4
  • 7. Context-free Grammars • More general productions than regular grammars S → w where w is any string of terminals and non-terminals • What languages do these grammars generate? S →(A) A → ε | aA | ASA 4 S → ε | aSb
  • 8. Context-free languages more general than regular languages 5
  • 9. Context-free languages more general than regular languages • {anbn | n ≥ 0} is not regular 5
  • 10. Context-free languages more general than regular languages • {anbn | n ≥ 0} is not regular ‣ but it is context-free 5
  • 11. Context-free languages more general than regular languages • {anbn | n ≥ 0} is not regular ‣ but it is context-free • Why are they called “context-free”? 5
  • 12. Context-free languages more general than regular languages • {anbn | n ≥ 0} is not regular ‣ but it is context-free • Why are they called “context-free”? ‣ Context-sensitive grammars allow more than one symbol on the lhs of productions 5
  • 13. Context-free languages more general than regular languages • {anbn | n ≥ 0} is not regular ‣ but it is context-free • Why are they called “context-free”? ‣ Context-sensitive grammars allow more than one symbol on the lhs of productions ° xAy → x(S)y can only be applied to the non-terminal A when it is in the context of x and y 5
  • 14. Context-free grammars are widely used for programming languages • From the definition of Algol-60: procedure_identifier::= identifier. actual_parameter::= string_literal | expression | array_identifier | switch_identifier | procedure_identifier. letter_string::= letter | letter_string letter. parameter_delimiter::= "," | ")" letter_string ":" "(". actual_parameter_list::= actual_parameter | actual_parameter_list parameter_delimiter actual_parameter. actual_parameter_part::= empty | "(" actual_parameter_list} ")". function_designator::= procedure_identifier actual_parameter_part. • We say: “most programming languages are context-free” ‣ This isnʼt strictly true ‣ … but we pretend that it is! 6
  • 15. Example adding_operator::= "+" | "−" . multiplying_operator::= "×" | "/" | "÷" . primary::= unsigned_number | variable | function_designator | "(" arithmetic_expression ")". factor::= primary | factor | factor power primary. term::= factor | term multiplying_operator factor. simple_arithmetic_expression::= term | adding_operator term | simple_arithmetic_expression adding_operator term. if_clause::= if Boolean_expression then. arithmetic_expression::= simple_arithmetic_expression | if_clause simple_arithmetic_expression else arithmetic_expression. if a < 0 then U+V else if a * b < 17 then U/V else if k <> y then V/U else 0 7
  • 16. Example derivation in a Grammar • Grammar: start symbol is A A → aAa A → B B → bB B → ε • Sample Derivation: A aAa aaAaa aaaAaaa aaaBaaa aaabBaaa aaabbBaaa aaabbaaa • Language? 8
  • 17. 9 A Aa a a b b ε a a a A A B B B A A a a a b b ε a a a A A B B B Derivations in Tree Form
  • 18. Arithmetic expressions in a programming language • Derive: a + a × a 10 see more easily what this language is if you think of a as a left parenthesis and b as a right parenthesis ")". =ewed in this way, L(G3)is the language of strings of properly nested parentheses. EXAMPLE 2.4 ........................................................................................................................ Consider grammar G4 = (V,S ,R, (EXPR)). V is {(EXPR),(TERM),(FACTOR)}and E is {a,+, x, (, I}. The rules are (EXPR)+ (EXPR)+(TERM)1 (TERM) (TERM)+ (TERM)x (FACTOR)1 (FACTOR) (FACTOR)+ ( (EXPR)) 1 a The two strings a+axa and (a+a)xa can be generated with grammar The parse trees are shown in the following figure.
  • 19. 11 FIGURE 2.5 Parse trees for the strings a+axa and (a+a)xa
  • 20. 11 FIGURE 2.5 Parse trees for the strings a+axa and (a+a)xa Notice how the grammar gives the meaning a + (a×a)
  • 21. Grammars in real computing • CFGʼs are universally used to describe the syntax of programming languages • Perfectly suited to describing recursive syntax of expressions and statements • Tools like compilers must parse programs; parsers can be generated automatically from CFGʼs • Real languages usually have a few non-CF bits • CFGʼs are also used in XML DTDʼs 12
  • 22. Formal definition of CFG • A Context-free grammar is a 4-tuple (V, Σ, R, S) where 1. V is a finite set called the variables (non- terminals) 2. Σ is a finite set (disjoint from V) called the terminals, 3. R is a finite set of rules, where each rule maps a variable to a string s ∈ (V ∪ Σ)* 4. S ∈ V is the start symbol 13
  • 23. Definition of Derivation • Let u, v and w be strings in (V ∪ Σ)*, and let A →w be a rule in R, • then uAv uwv (read: uAv yields uwv) • We say that u v (read: u derives v) if u = v or there is a sequence u1, u2, … uk, k ≥ 0, s.t. u u1 u2 … uk v • The language of the grammar is {w ∈ Σ* | S w } 14 * *
  • 24. Derivations Parse Trees • Each derivation can be viewed as a parse tree with variables at the internal nodes and terminals at the leaves • Start symbol is at the root • Each yield step uAv uwv where w=w1w2...wn corresponds to a node labeled A with children w1,w2,...,wn. • The final result in Σ* can be seen by reading the leaves left-to-right 15
  • 25. Simple CFG examples • Find grammars for: • L = {w ∈ {a,b}* | w begins and ends with the same symbol} • L = {w ∈ {a,b}* | w contains an odd number of aʼs} • L [(ε + 1)(01)*(ε + 0)] • Draw example derivations 16
  • 26. All regular languages have context free grammars • Proof: • Regular language is accepted by an NFA. • We can generate a regular grammar from the NFA (Lecture 6, Hein Alg. 11.11) • Any regular grammar is also a CFG. (Immediate from the definition of the grammars). 17
  • 27. Example • S→aQ1 S→bR1 • Q1 →aQ1 Q1 →bQ2 • Q2 →aQ1 Q2 →bQ2 • R1 →aR2 R1 →bR1 • R2 →aR2 R2 →bR1 • Q1 →ε R1 →ε • Resulting grammar may be quite different from one we designed by hand. 18
  • 28. Some CFGʼs generate non-regular languages • Find grammars for the following languages • L = {anbman | a,b ≥ 0} • L = {w ∈ {a,b}* | w contains equal numbers of aʼs and bʼs} • L = {wwR | w ∈ {a,b}*} • Draw example derivations 19
  • 29. Ambiguity • A grammar in which the same string can be given more than one parse tree is ambiguous. • Example: another grammar for arithmetic expressions ‹EXPR› → ‹EXPR› + ‹EXPR› | ‹EXPR› × ‹EXPR› | ( ‹EXPR› ) | a • Derive: a + a × a 20
  • 30. • This grammar is ambiguous: there are two different parse trees for a + a × a • Ambiguity is a bad thing if weʼre interested in the structure of the parse • Ambiguity doesnʼt matter if weʼre interested only in defining a language. 21 GURE 2.6 e two parse trees for the string a+axain grammar G5 This grammar doesn't capture the usual precedence relations and s
  • 31. Leftmost Derivations • In general, in any step of a derivation, there might be several variables that can be reduced by rules of the grammar. • In a leftmost derivation, we choose to always reduce the leftmost variable. • Example: given grammar S → aSb | SS | ε • A left-most derivation: S aSb aSSb aaSbSb aabSb aabb • A non-left-most derivation: S aSb aSSb aSb aaSbb aabb 22
  • 32. Ambiguity via left-most derivations • Every parse tree corresponds to a unique left-most derivation • So if a grammar has more than one left- most derivation for some string, the grammar is ambiguous • Note: merely having two derivations (not necessarily left-most) for one string is not enough to show ambiguity 23
  • 33. Ambiguity E E ⨉ E E + E ⨉ E a + E ⨉ E a + a ⨉ E a + a ⨉ a E E + E a + E a + E ⨉ E a + a ⨉ E a + a ⨉ a 24 GURE 2.6 e two parse trees for the string a+axain grammar G5 This grammar doesn't capture the usual precedence relations and so up the + before the x or vice versa. In contrast grammar Ga, gen
  • 34. Context-free languages • Closed under ∪, ∗ and ·, and under ∩ with a regular language ‣ How do we prove these properties? • Not closed under intersection, complement or difference • Recognizable by pushdown automata ‣ A pushdown automaton is a generalization of a finite-state automaton 25
  • 35. Pushdown Automata • Why canʼt a FSA recognize anbn? ‣ “storage” is finite • How can we fix the problem? ‣ add unbounded storage • Whatʼs the simplest kind of unbounded storage ‣ a pushdown stack 26 finite state control input accept reject stack
  • 36. History • PDAs independently invented by Oettinger [1961] and Schutzenberger [1963] • Equivalence between PDAs and CFG known to Chomsky in 1961; first published by Evey [1963]. 27
  • 37. Executing a PDA • The behavior of a PDA at any point depends on ⟨state,stack,unread input⟩ • PDA begins in start state with stated symbol on the stack • On each step it optionally reads an input character, pops a stack symbol, and non-deterministically chooses a new state and optionally pushes one or more stack symbols • PDA accepts if it is in a final state and there is no more input. 28
  • 38. Example PDA 29 he PDA Ati that recognizes {on1" n >01
  • 39. Example Execution: 0011 30 he PDA Ati that recognizes {on1" n >01 Stack Begin in initial state with empty stack
  • 40. Example Execution: 0011 31 he PDA Ati that recognizes {on1" n >01 Stack $
  • 41. Example Execution: 0011 32 he PDA Ati that recognizes {on1" n >01 Stack $ 0
  • 42. Example Execution: 0011 33 he PDA Ati that recognizes {on1" n >01 Stack $ 0 0
  • 43. Example Execution: 0011 34 he PDA Ati that recognizes {on1" n >01 Stack $ 0
  • 44. Example Execution: 0011 35 he PDA Ati that recognizes {on1" n >01 Stack $
  • 45. Example Execution: 0011 36 he PDA Ati that recognizes {on1" n >01 Stack Accept!
  • 46. PDAʼs can keep count! • This PDA can recognize {0n1n | n ≥ 0} by ‣ First, pushing a symbol on the stack for each 0 ‣ Then, popping a symbol off the stack for each 1 ‣ Accepting iff the stack is empty when the end of input is reached (and not before) • The size of the stack is unbounded. ‣ That is, no matter how big the stack grows, it is always possible to push another symbol on it. ‣ So PDAʼs can use the stack to count arbitrarily high 37
  • 47. Pushdown Automata (PDA) • A pushdown automaton    is defined as a 7-tuple:                                     , where: ‣ Q is a set of states,             is the start state ‣ Σ is the input alphabet, ‣   is the stack alphabet,            is the initial stack symbol ‣                                                  is the transition function ‣             is a set of final states, and ‣                      , the set X augmented with 38 M Q Γ q0 ∈ Q F ⊆ Q εXε = X ∪ {ε} Z0 ∈ Γ M = (Q, Σ, Γ, δ, q0, Z0, F) δ : (Q × Σε × Γε) → P{Q × Γ∗ }
  • 48. Executing a PDA • The configuration (or PDA + Hopcroftʼs instantaneous description, ID) of a PDA consists of 1. The PDA, 2. The current state of the PDA, 3. the remaining input, and 4. The contents of its stack. 39
  • 49. • We defined ‣ The transitions                are applicable iff °     is the current state, °             , or the next character on the input tape is    , and °             , or the top of the stack is ‣ If you select a transition             , then ° The new state is        ° if              ,      is popped off of the stack, and ° the (possibly empty) sequence of symbols     is pushed onto the stack Transitions 40 δ(q, a, γ) q a q γ γ (q , ω) ω γ = ε γ = ε a = ε δ : (Q × Aε × Γε) → P{Q × Γ∗ }
  • 50. PDAs are non-deterministic For two reasons: • The transition function δ answers a set • We have the choice of taking an ε- transition, or reading an input symbol 41
  • 51. Trivial Example Hein Example 12.4 42 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start • δ(s, ε, Y) = {(0, XY)} • δ(0, ε, X) = {(0, ε)} • δ(0, ε, Y) = {(f, Y)}   push(X) ε ε ε
  • 52. Accepting a String • There are two ways in which a PDA can accept a string: 1. final state is in the set F (implies F non-empty) 2. if F is empty (there are no final states), then the PDA accepts when the stack is empty • The previous example accepts by … 43
  • 53. Acceptance by Final State 44 A run of PDA M = (Q, A, Γ, δ, q0, γ0, F) is a sequence such that: (q0, γ0) a0 → (q1, s1) a1 → · · · an−1 → (qn, sn) for all i ∈ [0 .. n − 1]. (qi+1, γi+1) ∈ δ(qi, ai, γi) and with q0, . . . , qn ∈ Q, s1, . . . , sn ∈ Γ∗ , and a0, . . . , an−1 ∈ A si = γiti and si+1 = γi+1ti for some ti ∈ Γ∗ , and w = a0a1a2 . . . an−1 is the input. The run accepts     if qn ∈ F.w The language of                  is given byM, L(M) L(M) = {w ∈ A∗ | w is accepted by some run of M}
  • 54. Acceptance by Empty Stack • There is an alternative definition of acceptance: ‣ If the set of final states is empty, then ‣ the PDA accepts an input string if it can read all of the characters of the string and end up in a state in which the stack is empty. 45
  • 55. Acceptance by Empty Stack 46 such that: (q0, γ0) a0 → (q1, s1) a1 → · · · an−1 → (qn, sn) for all i ∈ [0 .. n − 1]. (qi+1, γi+1) ∈ δ(qi, ai, γi) and with q0, . . . , qn ∈ Q, s1, . . . , sn ∈ Γ∗ , and a0, . . . , an−1 ∈ A si = γiti and si+1 = γi+1ti for some ti ∈ Γ∗ , and w = a0a1a2 . . . an−1 is the input. The run accepts     ifw The language of                  is given byM, L(M) L(M) = {w ∈ A∗ | w is accepted by some run of M} A run of PDA M = (Q, A, Γ, δ, q0, γ0, ∅) is a sequence sn = ε.
  • 56. It doesnʼt matter! • PDAs that accept by empty stack and PDAs that accept by final state have equivalent power • What does this mean? ‣ That given either one, we can build the other ‣ Consequently, the class of languages that they accept are the same 47
  • 57. Example (Hein 12.2)         means that (in state 0), with input a and with X on the top of the stack, we can transition into state 0, and put an additional X onto the stack. 48 push(X) ε
  • 58. Example (Hein 12.2)         means that (in state 0), with input a and with X on the top of the stack, we can transition into state 0, and put an additional X onto the stack. 48 Empty stack acceptance push(X) ε
  • 59. 49 M = ({0,1}, {a, b},        {X}, δ, 0, X, ∅) Example (Hein 12.2) push(X) ε
  • 60. ‣ δ(0, a, X) = {(0, XX)} ‣ δ(0, ε, X) = {(1, ε)} ‣ δ(1, b, X) = {(1, ε)}   49 M = ({0,1}, {a, b},        {X}, δ, 0, X, ∅) Example (Hein 12.2) push(X) ε
  • 61. Transformation to a Final State PDA 50 ε
  • 62. Transformation to a Final State PDA 50 ε
  • 63. Transformation to a Final State PDA 50 S ε
  • 64. Transformation to a Final State PDA 50 SStart ε
  • 65. Transformation to a Final State PDA 50 SStart _____ push(Y) ε
  • 66. Transformation to a Final State PDA 50 FSStart _____ push(Y) ε
  • 67. Transformation to a Final State PDA 50 FSStart _____ push(Y) ε
  • 68. Transformation to a Final State PDA 50 FSStart _____ push(Y) ε
  • 69. Transformation to a Final State PDA 50 FS    ε, Y      push(X)Start _____ push(Y) ε
  • 70. Transformation to a Final State PDA 50 FS    ε, Y      push(X)Start  ε, Y    nop  ε, Y    nop_____ push(Y) ε
  • 71. Transformation to a Final State PDA ‣ N = ({0,1, S, F}, {a, b}, {X,Y}, δ´, S, Y, {F}) 50 FS    ε, Y      push(X)Start  ε, Y    nop  ε, Y    nop_____ push(Y) ε
  • 72. Transformation to a Final State PDA ‣ N = ({0,1, S, F}, {a, b}, {X,Y}, δ´, S, Y, {F}) ‣ δ´=δ+δ´(S, ε, Y) = {(0, XY)} +      δ´(0, ε, Y) = {(F, ε)} + δ´(1, ε, Y) = {(F, ε)} 50 FS    ε, Y      push(X)Start  ε, Y    nop  ε, Y    nop_____ push(Y) ε
  • 73. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start            push(Y) ε ε ε
  • 74. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S            push(Y) ε ε ε
  • 75. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S            push(Y) ε ε ε
  • 76. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S _____ push($)            push(Y) ε ε ε
  • 77. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S _____ push($) ε ε ε
  • 78. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S _____ push($)    ε, $      push(Y) ε ε ε
  • 79. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S e _____ push($)    ε, $      push(Y) ε ε ε
  • 80. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S e _____ push($)    ε, $      push(Y) ε ε ε
  • 81. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S e _____ push($)    ε, $      push(Y) ε ε ε
  • 82. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S e _____ push($)    ε, $      push(Y)  ε, ε     ε ε ε
  • 83. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S e _____ push($)    ε, $      push(Y)  ε, ε      ε, Y    pop ε ε ε
  • 84. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S e _____ push($)    ε, $      push(Y)  ε, ε      ε, Y    pop  ε, X    pop ε ε ε
  • 85. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S e _____ push($)    ε, $      push(Y)  ε, ε      ε, $    pop  ε, Y    pop  ε, X    pop ε ε ε
  • 86. Transformation to an EmptyStack PDA 51 Context-Free Languages and Pushdown Automata Start pop !, X 0 f push(X) !, Y s nop !, Y ! As the example shows, algorithm (12.5) doesn’t always give th s. For example, a simpler PDA to accept {"} by final state ca follows: Start S e _____ push($)    ε, $      push(Y)  ε, ε      ε, $    pop  ε, Y    pop  ε, X    pop ε ε ε
  • 87. Other Variations ‣ Hein says: “the execution of a PDA always begins with one symbol on the stack” ‣ Other authors say “the stack is initially empty” ‣ Does it matter? 52
  • 88. Other Variations ‣ Hein says: “the execution of a PDA always begins with one symbol on the stack” ‣ Other authors say “the stack is initially empty” ‣ Does it matter? 52 • Can you always convert an initially empty PDA to one with an initial symbol on the stack?
  • 89. Other Variations ‣ Hein says: “the execution of a PDA always begins with one symbol on the stack” ‣ Other authors say “the stack is initially empty” ‣ Does it matter? 52 • Can you always convert an initially empty PDA to one with an initial symbol on the stack? • Can you always convert a PDA with an initial symbol to one whose stack is initially empty?
  • 90. Stack Operations • Hopcroft et al. says: ‣ on each transition, to look at the top of the stack you must pop it, but ‣ you can push on as many stack symbols as you like, including the one that you just popped. • Other authors say ‣ you can push zero or one symbols onto the stack • Hein says: ‣ on each transition, you can push, pop or nop the stack, but you canʼt do more than one thing • Does it matter? 53
  • 91. More Examples L1= {anbn: n≥0} 54 _____ push($)   a,ε   push(#)  b,#   pop  ε,$   nop  ε, ε    nop
  • 92. Palindromes of even length L2= {wwR⎮ w ∈ {a, b}*} 55 _____ push($)   a,ε   push(a)  ε,$   nop  ε, ε    nop   b,ε   push(b) b,b pop a,a pop
  • 93. Palindromes of even length L2= {wwR⎮ w ∈ {a, b}*} 55 _____ push($)   a,ε   push(a)  ε,$   nop  ε, ε    nop   b,ε   push(b) b,b pop a,a pop • This machine is non-deterministic. (Why?)
  • 94. Palindromes of even length L2= {wwR⎮ w ∈ {a, b}*} 55 _____ push($)   a,ε   push(a)  ε,$   nop  ε, ε    nop   b,ε   push(b) b,b pop a,a pop • This machine is non-deterministic. (Why?) • Can you design a deterministic PDA for L2?
  • 96. Equal numbers of as and bs 57 _____ push($)  ε,$   nop   b,ε   push(b)  b,a   pop   a,ε   push(a)  a,b   pop