Transcript Document

Chapter 5 Context-Free Languages
• Regular language are effective in describing certain simple
patterns. But it is not enough to describe programming languages.
For example,thelanguage generatedfrom thegrammarS  aSb| 
is not regular.
• Context-Free languages cover more complicated languages.
Definition5.1
A grammarG  (V,T,S,P)is said to be context- free if all productions in P
have theform
A  x,
where A  V and x  (V  T)* .
A language L is said to be context- free if and onlyif thereis
a context- free grammarG such thatL  L(G).
Ai  x
A1 A2  Ai 1 Ai Ai 1  Ak  A1 A2  Ai 1xAi 1  Ak
Context free
Example5.1 T hegrammarG  ({S},{a,b},S,P), with productions
S  aSa,
S  bSb,
S λ,
is context- free.Whatis thelanguage which accpets?
Solution
A typicalderivationin thisgrammaris
S  aSa  aaSaa aabSbaa aabbaa.
Linear grammars
It clear thatL(G)  {ww R : w  {a, b}*}.
Example5.2 T hegrammarG  ({S , A, B},{a,b},S,P), wit h product ions
S  abB,
A  aaBb,
B  bbAa,
A λ ,
is cont ext- free.Whatis t helanguage which accpet s?
Solution : L(G)  {ab(bbaa)nbba(ba)n : n  0 }.
Example5.3 Show language L  {a nbm:n  m} is context- free.
Solution
For the case n  m, we have
S  AS1 ,
For the case n  m, we have thesimilar one:
S  S1B ,
S1  aS1b|λ,
S1  aS1b|λ,
A  aA|a.
B  bB|b.
It is not a linear grammar.
T herefore,the answer is as follows:
S  AS1|S1B ,
S1  aS1b|λ,
A  aA|a,
B  bB|b.
Leftmost and Rightmost Derivations
Definition5.2 A derivation is said to be leftmost if in each step the
leftmost variable in the sentential form is replaced. If in each step the
rightmost variable is replaced, we call the derivation rightmost.
Example5.5 Consider the grammar with productions
S  aAB,
S
A  bBb,
B  A|λ |
T hen S  aAB  abBbB abbBbbB
 abbbbB abbbb
is a leftmostderivationof st ringabbbb.
S  aAB  aA  abBb abAb
 abbBbb abbbb
is a rightmostderibat ionof st ringabbbb.
a
A
B
b
B
b
b
B
b


Definition 5.3 Let G=(V,T,S,P) be a context-free grammar.
An ordered tree is a derivation tree for G if and only if it has
the following properties:
S
a
If vertexA V and
its chidren a1 , a2 ,...,an ,
then A  a1a2  an
is a producionin P.
The root is labeled S.
Each interior vertex has a label from V.
A
B
b
B
b

b
B
b
Each leaf has a label fromT  {λ}.

T heleaf labeled with λ has no siblings.
T heorem5.1 Let G  (V,T,S,P)be a context- free grammar.
T henfor everyw  L(G), thereexistsa derivations treeof G
whose yieldis w. Conversely, theyield of any derivationtree
is in L(G).
5.2 Parsing and Ambiguity
Parsing and Membership
Given a grammarG, the termparsingdescribes finding a sequence
of productions by which a w  L(G)is derived.
To check if w is a member of L(G), we use the exhaustive
search parsing method. It is a form of top-down.
Example5.7 Consider the grammar S  SS|aSb|bSa|λ and stringw  aabb.
1.
2.
3.
4.
S  SS,
S  aSb,
S  bSa,
S λ.
S  SS  SSS,
S  SS  aSbS,
S  SS  bSaS,
S  SS  S .
S  aSb  aSSb,
S  aSb  aaSbb,
S  aSb  abSab,
S  aSb  ab.
S  aSb  aaSbb  aabb
Problemof exhaustivesearch pasing:
(1) T heparsingis not efficient.
(2) when themethodparsesa w, it is possible thatit never term
inatesfor w  L(G).
In example5.7 ( grammar S  SS|aSb|bSa|λ), when themethodparses
w  abb, it will go on producingtrialsententialformsindefinitely.
How tosolve theproblem?
Removetheproductions in formof A  λ and A  B.
Example5.8 T hegrammar S  SS|aSb|bSa|ab|bageneratesthelanguage
in Example5.7 without he
t emptystring.
Given any w  {a,b} , theexhaustivesearch parsingmethod
will always terminatein no more |w| rounds, since thesententialform
grows by at least onesymbol in each round. T herefore,we haveeither
produced a parsingor we know thatw  L(G).
T heorem5.2 Suppose thatG  (V,T,S,P)is a context- free grammar which
does not haveany rules of theform A  λ or A  B,
where A,B  V . T hen theexhaustivesearch parsingmethodcan be made into
an algorithmwhich,for any w  Σ * , eit herproducesa parsingof w, or tellsus
that no parsingis possible.
Ambiguity in grammars and languages
Definition5.5 A context- free grammarG is said to be ambiguous
if thereexistssome w  L(G) that has at least twodistinct derivationtrees.
Example5.10T hegrammar with productions
S  aSb|SS|λ, is ambiguous.
S
S
S
a
S b

a
S b

S
a
S b
a
S b

Example5.11 Consider the grammarG  (V,T,E,P)with product ions
V  {E , I }, T  {a, b, c,,*,(, )},
and product ions
E  I | E  E | E*E | (E),
I  a|b|c.
T hegrammaris ambiguous. For instance,a  b*c has two differentderivationtrees.
E
E
E
+ E
I
E
a
*
E
E
E
I
I
b
c
*
E
+ E
I
I
I
c
a
b
Example5.12 T o rewrite thegrammarin Example5.11,we introducenew variables,
taking V  {E , T , F , I }, and replaceproductions with
E  T | E  T , T  F|T*F, F  I|(E), I  a|b|c.
T hegrammaris unambiguous.
5.3 Context-free Grammars and Programming Languages
Why we learn the theory of formal languages?----We use the
theory to design programming languages and to design the
interpreters and compiles for them.
.
Example Consider the context- free grammer with productions
E  T | E  T , T  F|T*F, F  I|(E),
I  I1I 2 , I1  1|2|3|4|5|6|7|8|9, I 2  I 3 I 2 |  , I 3  0 | 1|2|3|4|5|6|7|8|9.
It generatesall theexpressionof integers.
In Programming Language:
 expression:: term | expression   term 
Example if-then-else statement can be defined by context-free grammar as
<if_statement>::=If<expression><then_clause><else_clase>.
A programming language has two aspects: syntax and semantics.
Related to context-free
grammar.
Chapter 7 Pushdown Automata
Pushdown Automata
Stack
Initial state q0
Input file
1  2 3 4 5  6 7 8  9
Finite Automata
…
Control unit
(transition function δ)
β1
α1
α2
α3
α4
α5
δ(q0,σ1,α1 )  {(q1, β1α1 ),...}
Initial state q0
Input file
1  2 3 4 5  6 7 8  9
…
Control unit
(transition function δ)
δ(q0 ,σ1 )  q1
Definition 7.1
A nondeterministic pushdown accepter or npda is defined by
the septuple M  (Q,, T ,  , q0 , z, F )
where
Q is a finit eset of int ernalstatesof thecontrolunit,
 is theinput alphabet,
T is a finteset of simbols called thestatealphabet,
 : Q  {  {}}  T  P(Q  T *),
q 0  Q is theinitalstateof thecontrolunit,
z  T is thestack stat symbol,
F  Q is theset of final states.
Definition7.2
T helanguage L accepted by an npda M  (Q,Σ,Σ,  , q0 ,z, F ) is defined
formallyas follows:
L(M)  {w  Σ* : ( q0 , w, z ) *M ( p,  , u ), p  F , u  T *}.
In words, thelanguage consist sof all st ringsw thatcan put M intoa final st at e
at theend of thest ring.T hefinal st ack contentu is irrelevantto thisdefinition
of acceptance.
Context-free Language and Pushdown Automata
Theorem 7.1
For any context-free language L, there is an npda M such that
L = L(M).
L  {a b : n  0}  {a}
n n
Example7.1
Consider an npda with
Q  {q0 , q1 , q2 , q3},
a,1,11
q0
  {a, b},
a,0,  ;
T  {0,1},
 ,0,  ;
z  0,
F  {q3},
with initialstateq0 and
 ( q0 , a,0)  {( q1 ,10), ( q3 ,  )},
 ( q0 ,  ,0)  {( q3 ,  )},
 ( q1 , a,1)  {( q1 ,11)},
 ( q1 , b,1)  {( q2 ,  )},
 ( q2 , b,1)  {( q2 ,  )},
 ( q2 ,  ,0)  {( q3 ,  )},
q3
a,0,10
q1
b,1, 
 ,0, 
q2
b,1, 