Chapter 5 Context-Free Languages Regular language are effective in describing certain simple...
-
Upload
wilfrid-lamb -
Category
Documents
-
view
223 -
download
0
description
Transcript of Chapter 5 Context-Free Languages Regular language are effective in describing certain simple...
Chapter 5 Context-Free Languages
regular.not is |grammar thefrom generated language theexample,For aSbS
• Regular language are effective in describing certain simple patterns. But it is not enough to describe programming languages.
• Context-Free languages cover more complicated languages.
L(G).LGL
.T)(VxVA
x,A
P(V,T,S,P)G
*
such that grammar free-context a is thereifonly and if free-context be tosaid is languageA
and where
form thehave in sproduction all if free-context be tosaid is grammar A
5.1 Definition
kiikiii AxAAAAAAAAAA 11211121 xAi Context free
accpets? which language theis What free.-context is
sproduction with ,grammar The 5.1 Example
λ, SbSb, SaSa,S
,S,P)({S},{a,b}G
}.},{:{wwL(G) clear thatIt
. isgrammar in this derivation A typical
Solution
*R baw
aabbaaaabSbaaaaSaaaSaS
accpets? which language theis What free.-context is
sproduction with ,,,grammar The 5.2 Example
λ, AbbAa, BaaBb, AabB,S
,P)B},{a,b},SA({SG
}.nbba(ba){ab(bbaa)L(G) nn 0: :Solution
Linear grammars
free.-context is language Show 5.3 Example m}:nb{aL mn
aA|a. Ab|λaS S ,ASS
mn
,
have we, case For theSolution
11
1
bB|b.Bb|λaS S
B ,SSmn
,
:onesimilar thehave we, case For the
11
1
It is not a linear grammar.
bB|b. BaA|a, A
b|λaS SB ,|SASS
,
:follows as isanswer theTherefore,
11
11
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.
. string of deribationrightmost a is
. string of derivationleftmost a is
Then
, ,
sproductionth grammar wi heConsider t 5.5 Example
abbbbabbbbabbBbb
abAbabBbaAaABSabbbb
abbbbabbbbBabbBbbBabBbBaABS
A|λ|BbBbAaABS
S
A Ba
bBb
bBb
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 Ba
bBb
bBb
}. from label a has leafEach {λT
The root is labeled S.
Each interior vertex has a label from V.
P.in producion a is then
,,...,,chidren its and vertex If
21
21
n
n
aaaAaa a
VA
siblings. no has with labeled leaf The λ
.in is treederivationany of yield the,Conversely . is yield whose
of treesderivation a exists there,every for Then grammar. free-context a be Let 5.1 Theorem
L(G)w
GL(G)w(V,T,S,P)G
5.2 Parsing and AmbiguityParsing and Membership
derived. is aby which sproduction of sequence a finding describes parsing term the,grammar aGiven
L(G)wG
To check if w is a member of L(G), we use the exhaustive search parsing method. It is a form of top-down.
. string and grammar heConsider t 5.7 Example aabbw|λSS|aSb|bSaS
λ.bSaSaSbSSSS
S .4, .3, .2
, .1
.
,
abaSb SabSab,aSb SaaSbb,aSbSaSSbaSbS
.
,
SSS SbSaS,SS SaSbS,SSSSSSSSS
aabbaaSbbaSbS
.for inatesnever termit that possible isit a parses method when the(2)efficient.not is parsing The (1)
:pasingsearch exhaustive of Problem
L(G) ww,
ly.indefinite forms sentential trialproducingon go it will , parses method when the),grammar ( 5.7 exampleIn
abbw|λSS|aSb|bSaS
. and of formin sproduction theRemove problem? thesolve toHow
BAλ A
string.empty he without t5.7 Examplein language thegeneratesgrammar The 5.8 Example |ab|ba SS|aSb|bSaS
. that know or we parsing a producedeither have weTherefore, round.each in symbol oneleast at by grows
form sentential thesince rounds, more noin terminatealways will method parsingsearch exhaustive the,any Given
L(G)w
|w|{a,b}w
possible. is parsing nothat us or tells , of parsing a produceseither ,any for which,algorithman
into made becan method parsingsearch exhaustive Then the . where,or form theof rulesany havenot does
ich grammar wh free-context a is that Suppose 5.2 Theorem
wΣw
VA,BBAλA
(V,T,S,P)G
*
Ambiguity in grammars and languages
trees.derivationdistinct least twoat hasthat some exists thereif ambiguous be tosaid is grammar free-contextA 5.5 Definition
L(G) wG
ambiguous. is , sproductionth grammar wi The 5.10 Example
aSb|SS|λ S S
bSa
bSa
bSa
bSa
S
S
S
trees.derivationdifferent twohas instance,For ambiguous. isgrammar The
||| sproduction and
)},(,,*,,,,{},,{ sproduction with grammar heConsider t 5.11 Example
b*c aa|b|c. I
(E),E*EEEIE
cbaTIE V(V,T,E,P)G
E
E+E
I E*E
a I
c
I
E
E*E
E+E
I
b
I
b a
I
c
s.unambiguou isgrammar The ,|
with sproduction replace and },,,,{ taking , variablesnew introduce we5.11, Examplein grammar therewrite To 5.12 Example
a|b|c.II|(E), FF|T*F,TTETEIFTE V
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.
integers. of expression theall generatesIt .987654321|0 ,|I ,987654321 ,
,| sproductionth grammer wi free-context heConsider t Example
3232121 ||||||||III||||||||IIIII|(E), FF|T*F,TTETE
termexpression|term::expression :Language gProgramminIn
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.
.
Control unit (transition function δ)
…Input file
1 2 3 4 5 6 7 8 9
Initial state 0q
110 q),σδ(q
Finite AutomataStack
Control unit (transition function δ)
…Input file
1 2 3 4 5 6 7 8 9
Initial state 0q
1α2α3α4α5α
),...}α,{( 111110 βq),α,σδ(q
1β
Pushdown Automata
Chapter 7 Pushdown Automata
Definition 7.1
A nondeterministic pushdown accepter or npda is defined by the septuple
states. final ofset theis Q F
symbol,stat stack theis Tzunit, control theof state inital theis Qq
,*)(T}}{{Q : alphabet, state thecalled simbols ofset fintea is
alphabet,input theis unit, control theof states internal ofset finitea is
where),,,,,
0
0
TQPT
Q
FzqT(Q,M
Theorem 7.1
For any context-free language L, there is an npda M such that
L = L(M).
Context-free Language and Pushdown Automata
.acceptance of definition this toirrelevant is content stack final The string. theof end at the state finala into put can that strings all of consists language the words,In
*}.,),,,( ),,( :{ :follows asformally
defined is ),,,( anby accepted Llanguage The7.2 Definition
0
0
uMw
TuFpupzwqΣ*w L(M)
F,zqQ,Σ,ΣMnpda
M
*
}{}0:{ anbaL nn
)},,{()0,,()},,{()1,,()},,{()1,,()},11,{()1,,()},,{()0,,(
)},,(),10,{()0,,( and state initial
},{,0
},1,0{},,{
},,,,{ npda with anConsider
7.1 Example
32
22
21
11
30
310
0
3
3210
qqqbqqbqqaqqq
qqaqqwith
qFzT
baqqqqQ
0q 1q
2q
,1,b
10,0,a
11,1,a
3q,1,b
,0,;,0,;,0,
a