View
264
Download
0
Category
Preview:
Citation preview
CSC 473 Automata, Grammars & Languages 10/7/10
1
C SC 473 Automata, Grammars & Languages
Automata, Grammars and Languages
Discourse 04
Context-Free Grammarsand
Pushdown Automata
C SC 473 Automata, Grammars & Languages 2
Backus-Naur Form Grammars (CFGs)• Algol 60, Algol 68—first “block-structured” languages
• Ex:• CF Grammar
<program> ::= <block><statement> ::= s | <block><block> ::= begin <list> end<list> ::= <statement> ; <list> |<statement>
begin s ; begin s;s;s end ;s end
P B
S
S B
B L
L S L
L S
!
!
!
!
!
!
s
b e
;
:G { ;}! = s, b, e,
Nonterminals
=variables→←rules=productions
↑terminals
V
Start variable “S”↓
R
C SC 473 Automata, Grammars & Languages 3
Grammars are “Generators”
• ⇒ “yields” or “derives in one step” Apply one production to one variable in the string nondeterministic
P B L
!
" " b e
S B L! !b e b e bb ee
!
bse S !
"
bb ee !
;S L
!
"bb ee !
⇒ bbsee
; ;B L L L! !b e bb e e !
⇒ ; L !bs e !
;S L
!"b e⇒
;S S !b e !
; ;S S L
!"b e !
CSC 473 Automata, Grammars & Languages 10/7/10
2
C SC 473 Automata, Grammars & Languages 4
• One possible derivation. Variable being rewritten at eachstage is underscored
• two choices at each derivation step: Which variable (nonterminal) to be rewritten? Which rule with that variable as LHS to be applied?
• All possible terminal strings obtainable in this way makeup L(G)
; ; ;
; ; ; ; ; ;
; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ;
; ; ; ; ( )
P B L S L S S L
S S S S B S S L S
L S L S L
L S L L
S
L G
! ! ! !
! ! !
! ! !
! ! !
! !
" #
b e b e b e
b e b e b b e e
bs b e e bs b e se bs b e se
bs bs e se bs bs e se bs bs s e se
bs bs s e se bs bs s se se
bs bs s se se
A Particular Derivation
C SC 473 Automata, Grammars & Languages 5
Why CFGs?• Most natural or artificial (e.g. programming) languages are
not regular
• We know that the latter language is not regular, so …• Ex: C programs
( ) { | 1}n n
L G n! !
" = #b seb se
main(){}, main(){{}}, main(){{{}}},
main(){} main(){} | 1n n
n! !
" = #C
…
{ }
C SC 473 Automata, Grammars & Languages 6
Derivation (Parse) TreeP
B
b L e
S L;
sS ; L
SB
sb eL
S L;
s S ; L
s S
s; ; ; ;bs bs s se se
yield/frontier/terminal string =
CSC 473 Automata, Grammars & Languages 10/7/10
3
C SC 473 Automata, Grammars & Languages 7
Derivation (Parse) TreeP
B
S L;
sS ; L
B S
s
S L;
s S ; L
s S
s
Lb e
b eL
C SC 473 Automata, Grammars & Languages 8
Derivation (Parse) Tree (contʼd)P
B
b L e
S L;
sS ; L
SB
sb eL
S L;
s S ; L
s S
s
12
3
456
7
8
9
10
11 12
13 14
15
C SC 473 Automata, Grammars & Languages 9
Context-Free Grammar• Defn 2.2: A context-free grammar G is a 4-tuple
• is a finite set, the variables (nonterminals)• is a finite set disjoint from V, the terminals• is a finite set of rules, of the form
• is the start variable• Ex: strings with balanced parentheses. Formally:
• Ex: informally• Variables = upper case• Terminals = lower case
( , , ),G V R S= !
V
R
!
S V!
0( , , ),G V R S= ! S B={(,)} { }V B! = =
{ , , ( )}R B B BB B B!= " " "
, , ( )A w A V w V!
" # # $ %
0: | | ( )G B B BB B B!" " "
←technically, an ordered
pair (A, w)
CSC 473 Automata, Grammars & Languages 10/7/10
4
C SC 473 Automata, Grammars & Languages 10
Yields & Derives Relations• Defn. The relation yields (derives in 1 step)
is defined as follows: if is a rule in R, then
• Defn: derives in k steps:• Defn: derives:
• In other words:
• Defn: A derivation (of n steps) from is any sequence ofstrings satisfying:
1 2 1
1 2 1
iff or ( )( ) ( )
( )k
k
u v u v u u u
u u u u v
!
"
"
# = $ $ $
# # # #
!
!
, ( )u v V uAv uwv!
" # $ % &
( ) ( )V V! !
" # $ % & $ %
A w!
( )k
!
( )!
"
0 1 2 ku u u u! ! !!
0u
C SC 473 Automata, Grammars & Languages 11
Language Generated• Defn. The language generated by G is the set of all terminal
strings derived from S:
A partial derivation is one that starts with S and ends in a non-terminalstring containing variables in V
• Ex:
Partial: Terminal or terminated:
( ) { | }L G w w S w! !
= " # $ %
|
| |
S aAS a
A SbA SS ba
!
!
S aAS aSbAS! !
S aAS aSbAS aabAS
aabbaS aabbaa
! ! !
! !
C SC 473 Automata, Grammars & Languages 12
Derivations and Parse Trees• Ex:
0: | | ( )G B B BB B B!" " "
( ) (( )) (())
(())( ) (())()
( ) () ( )()
(( ))() (())()
B BB B B B B B
B
B BB B B B B
B
! ! ! !
! !
! ! ! !
! !
leftmost :
rightmost :
B B
BB
B
B
( B
B
)
( B )
B
B
( B
B
)
( B )
B
B
( B
B
)
!
( B )
B
B
( B
B
)
!
( B )
( B )
B
B
( B
B
)
!
( B )
! Notice: completed (terminated) parsetree is the same for both derivations—thoughthe sequence “grows” differently
CSC 473 Automata, Grammars & Languages 10/7/10
5
C SC 473 Automata, Grammars & Languages 13
Derivation → Parse Tree• Proposition 1: For every (terminated or partial) derivation
there is an unique parse tree T with frontierconstructible from D.
• Proposition 2: For every parse tree T in G and anytraversal order that is top-down (visits parents beforechildren), there is an unique derivation for the frontier of Tfrom S, and it is constructible from T.
• Corollary 3: For every parse tree T in G there is an uniqueleftmost derivation constructible from T.
Pf: Pre-order traverse T, expanding variables as theirnodes are visited.
1 2:
nD S u u u! ! !!
nu
C SC 473 Automata, Grammars & Languages 14
Ex: Leftmost DerivationE
T
! F
( )
T
( )
F
E
E + T
E
E + T
F
x
T
! FT
F
x
x
T
F
x
F
x|
|
( )|
E E T T
T T F F
F E x
! +
! "
!
C SC 473 Automata, Grammars & Languages 15
Ex: Leftmost DerivationE
T
! F
( )
T
( )
F
E
E + T
E
E + T
F
x
T
! FT
F
x
x
T
F
x
F
x
1
34
5
6
7
8
2
9
10
11
12
13
14
15
16
17
17
18
Preorder traversal
Recommended