Professional Documents
Culture Documents
Context Free Langauges
Context Free Langauges
Chomsky hierarchy
In 1957, Noam Chomsky published Syntactic Structures, an landmark book that dened the socalled Chomsky hierarchy of languages
original name Type-3 Grammars Type-2 Grammars Type-1 Grammars Type-0 Grammars
language generated
productions:
A and A B
A
A
no restriction
Regular languages
Closed under !"#and
Context-free Grammars
Context-free Grammars
Context-free Grammars
Context-free Grammars
S ! # | aSb
but it is context-free
but it is context-free
% %
but it is context-free
% %
but it is context-free
xAy ! x(S)y can only be applied to the non-terminal A when it is in the context of x and y
! ! ! ! ! !
% %
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
Grammar:
A ! aAa A!B B ! bB B!#
start symbol is A
Sample Derivation:
aAa aaAaa aaaAaaa aaaBaaa aaabbaaa aaabBaaa aaabbBaaa
Language?
8
a a a
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.
EXAM PLE
2.4 ........................................................................................................................
Consider grammar G4 = (V, S , R , (EXP R)). V is {(EX PR), (TERM),(FACTOR)}and E is {a, +, x, (, I}. T h e rules are (EXPR)+ (EXP R)+(T ERM1 )(TERM) (TERM) + (TERM)x (FACTOR)1 (FACTOR) (FACTOR)+ ( (EXPR)) 1 a T h e two strings a+ax a and (a+a) xa can be generated with grammar T h e parse trees are shown in the following figure.
Derive: a + a ' a
10
FIGURE
2.5
Notice how the grammar FIGURE 2.5 gives the meaning a + (a'a)
11 xa Parse trees for the strings a+axa and (a+a)
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
Denition 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 nal result in (* can be seen by reading the leaves left-to-right
15
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)]
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 denition of the grammars).
17
Example
S!aQ1 Q1 !aQ1 Q2 !aQ1 R1 !aR2 R2 !aR2 Q1 !# S!bR1 Q1 !bQ2 Q2 !bQ2 R1 !bR1 R2 !bR1 R1 !#
L = {anbman | a,b $ 0} L = {w % {a,b}* | w contains equal numbers of a&s and b&s} L = {wwR | w % {a,b}*}
Ambiguity
A grammar in which the same string can be given more than one parse tree is ambiguous.
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 GURE 2.6 interested in the structure of the parse e two parse trees for the string a+axain grammar G5
Ambiguity doesn&t matter if we&re interested only in dening a language. 21 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.
A left-most derivation:
aSb
aSSb
aaSbSb
aabSb
aabb
A non-left-most derivation:
aSb
aSSb
aSb
aaSbb
aabb
22
Note: merely having two derivations (not necessarily left-most) for one string is not enough to show ambiguity
23
Ambiguity
E GURE 2.6
E&E
E+E&E
E+E
a+E a+a&E
e two parse trees for the string a+axa in grammar G5 a+E&E a+a&E
a+E&E a+a&a a+a&a
This grammar doesn't capture the usual precedence relations and so up the before the x or vice versa. In contrast grammar24Ga, gen
Context-free languages
Closed under !, # and , and under " with a regular language
Pushdown Automata
% % %
storage is nite
reject
History
PDAs independently invented by Oettinger [1961] and Schutzenberger [1963] Equivalence between PDAs and CFG known to Chomsky in 1961; rst 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 nal state and there is no more input.
28
Example PDA
> 01
29
Stack
> 01
30
$ Stack
> 01
31
0 $ Stack
> 01
32
0 0 $ Stack
> 01
33
0 $ Stack
> 01
34
$ Stack
> 01
35
Stack Accept!
> 01
36
% % %
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)
% %
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
% % % % % %
Executing a PDA
The conguration (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
Transitions
We dened : (Q A ) P{Q }
The transition function " answers a set We have the choice of taking an !transition, or reading an input symbol
41
Trivial Example
!, Y push(X )
!, X pop
Start
push(X)
, Y ! nop
As the example shows, algorithm (12.5) doesnt always give th s. For }) by "(s, !, Y)a=simpler {(0, XYPDA )} to accept "(0, !{,"Y = final {(f, Ystate )} ca example, follows:
Start
42
Accepting a String
43
a0
a1
an1
such that:
% %
If the set of nal 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
a0
a1
an1
such that:
It doesn&t matter!
PDAs that accept by empty stack and PDAs that accept by nal 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)
push(X)
!!!!
with X on the top of the stack, we can transition into state 0, and put an additional X onto the stack.
Example
Empty stack acceptance
push(X)
(Hein 12.2)
!!!!
with X on the top of the stack, we can transition into state 0, and put an additional X onto the stack.
Example
M = ({0,1}, {a, b}, !!!!!!!{X}, ", 0, X, ))
push(X)
(Hein 12.2)
49
Example
M = ({0,1}, {a, b}, !!!!!!!{X}, ", 0, X, ))
push(X)
(Hein 12.2)
% %
#
49
50
50
50
Start S
50
_____ Start
push(Y)
50
_____ Start
push(Y)
S F
50
_____ Start
push(Y)
S F
50
_____ Start
push(Y)
S F
50
_____
50
_____
#!, Y!
!!nop
#!, Y!
!!nop
50
_____
#!, Y!
!!nop
#!, Y!
!!nop
50
_____
#!, Y!
!!nop
#!, Y!
!!nop
" = " + "(S, !, Y) = {(0, XY)} + !!!!!"(0, !, Y) = {(F, !)} + "(1, !, Y) = {(F, !)}
50
!, X !, Y
s
push(X ) pop
Start
!, Y
nop
As the example shows, algorithm (12.5) doesnt always give th s. For example, a simpler PDA to accept {"} by final state ca follows:
Start
51
!, X !, Y
s
push(X ) pop
Start
S
!, Y
nop
As the example shows, algorithm (12.5) doesnt always give th s. For example, a simpler PDA to accept {"} by final state ca follows:
Start
51
!, X !, Y
s
push(X ) pop
Start
S
!, Y
nop
As the example shows, algorithm (12.5) doesnt always give th s. For example, a simpler PDA to accept {"} by final state ca follows:
Start
51
!, X !, Y
s
push(X ) pop
Start _____
push($)
!, Y
nop
As the example shows, algorithm (12.5) doesnt always give th s. For example, a simpler PDA to accept {"} by final state ca follows:
Start
51
Start _____
push($)
push(X )
!, Y
nop
As the example shows, algorithm (12.5) doesnt always give th s. For example, a simpler PDA to accept {"} by final state ca follows:
Start
51
Start _____
push($)
s
###!, $!!! !!push(Y)
push(X )
!, Y
nop
As the example shows, algorithm (12.5) doesnt always give th s. For example, a simpler PDA to accept {"} by final state ca follows:
Start
51
Start _____
push($)
s
###!, $!!! !!push(Y)
push(X )
!, Y
nop
As the example shows, algorithm (12.5) doesnt always give th s. For example, a simpler PDA to accept {"} by final state ca follows:
Start
51
Start _____
push($)
s
###!, $!!! !!push(Y)
push(X )
!, Y
nop
As the example shows, algorithm (12.5) doesnt always give th s. For example, a simpler PDA to accept {"} by final state ca follows:
Start
51
Start _____
push($)
s
###!, $!!! !!push(Y)
push(X )
!, Y
nop
As the example shows, algorithm (12.5) doesnt always give th s. For example, a simpler PDA to accept {"} by final state ca follows:
Start
51
Start _____
push($)
s
###!, $!!! !!push(Y)
push(X )
!, Y
nop
#!, !! !! doesnt
As the example shows, algorithm (12.5) always give th s. For example, a simpler PDA to accept {"} by final state ca follows:
Start
51
Start _____
push($)
s
###!, $!!! !!push(Y)
push(X )
!, Y
nop
#!, !! !! doesnt
As the example shows, algorithm (12.5) always give th #!, Y! s. For example, a simpler PDA to accept {"} by final state ca !!pop follows:
Start
51
Start _____
push($)
s
###!, $!!! !!push(Y)
push(X )
!, Y
nop
#!, !! !! doesnt
, X! As the example shows,#! algorithm (12.5) always give th #!, Y! !!pop s. For example, a simpler PDA to accept {"} by final state ca !!pop follows:
Start
51
Start _____
push($)
s
###!, $!!! !!push(Y)
push(X )
!, Y
nop
#!, !! !! doesnt
#!, $! !!pop
, X! As the example shows,#! algorithm (12.5) always give th #!, Y! !!pop s. For example, a simpler PDA to accept {"} by final state ca !!pop follows:
Start
51
Start _____
push($)
s
###!, $!!! !!push(Y)
push(X )
!, Y
nop
#!, !! !! doesnt
#!, $! !!pop
, X! As the example shows,#! algorithm (12.5) always give th #!, Y! !!pop s. For example, a simpler PDA to accept {"} by final state ca !!pop follows:
Start
51
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?
Can you always convert an initially empty PDA to one with an initial symbol on the stack?
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?
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?
52
Stack Operations
% %
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.
% %
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}
!!a,!##
push(#)
!b,##
!pop
_____
push($)
!!,$#
!nop
!!, !#
! nop
54
a,a b,b
pop pop
_____
push($)
!!,$#
!nop
!!, !#
! nop
55
a,a b,b
pop pop
_____
push($)
!!,$#
!nop
!!, !#
! nop
55
a,a b,b
pop pop
_____
push($)
!!,$#
!nop
!!, !#
! nop
This machine is non-deterministic. (Why?) Can you design a deterministic PDA for L2?
55
Balanced Parenthesis
L3= {w % {(, )}* : w is a balanced string of parenthesis}
!!(,!##
push(#)
!),#
pop
_____
push($)
!!,$#
!nop
56
!a,b#
!pop
!!b,!##
push(b)
!b,a#
!pop
_____
push($)
!!,$#
!nop
57