Chapter 5 Context-Free Languages

14
Chapter 5 Context-Free Languages regular. not is | grammar the from generated language the example, For aSb S • 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). L G L . T) (V x V A x, A P (V,T,S,P) G * such that grammar free - context a is there if only and if free - context be to said is language A and where form the have in s production all if free - context be to said is grammar A 5.1 Definition k i i k i i i A xA A A A A A A A A A 1 1 2 1 1 1 2 1 x A i Context free

description

Chapter 5 Context-Free Languages. 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. Context free. Linear grammars. It is not a linear grammar. S. a. A. - PowerPoint PPT Presentation

Transcript of Chapter 5 Context-Free Languages

Page 1: Chapter 5    Context-Free Languages

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).LG

L

.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

Page 2: Chapter 5    Context-Free Languages

accpets? which language theis What free.-context is

sproduction with ,grammar The 5.1 Example

λ, S

bSb, S

aSa,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

λ, A

bbAa, B

aaBb, A

abB,S

,P)B},{a,b},SA({SG

}.nbba(ba){ab(bbaa)L(G) nn 0: :Solution

Linear grammars

Page 3: Chapter 5    Context-Free Languages

free.-context is language Show 5.3 Example m}:nb{aL mn

aA|a. A

b|λaS S

,ASS

mn

,

have we, case For the

Solution

11

1

bB|b.B

b|λaS S

B ,SS

mn

,

:onesimilar thehave we, case For the

11

1

It is not a linear grammar.

bB|b. B

aA|a, A

b|λaS S

B ,|SASS

,

:follows as isanswer theTherefore,

11

11

Page 4: Chapter 5    Context-Free Languages

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

abbbb

abbbbabbBbb

abAbabBbaAaABS

abbbb

abbbbabbbbB

abbBbbBabBbBaABS

A|λ|B

bBbA

aABS

S

A Ba

bBb

bBb

Page 5: Chapter 5    Context-Free Languages

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

aaaA

aa 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

Page 6: Chapter 5    Context-Free Languages

5.2 Parsing and AmbiguityParsing and Membership

derived. is aby which sproduction of

sequence a finding describes parsing term the,grammar aGiven

L(G)w

G

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

λ.

bSaS

aSbS

SSS

S .4

, .3

, .2

, .1

.

,

abaSb S

abSab,aSb S

aaSbb,aSbS

aSSbaSbS

.

,

SSS S

bSaS,SS S

aSbS,SSS

SSSSSS

aabbaaSbbaSbS

Page 7: Chapter 5    Context-Free Languages

.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 produced

either 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

Page 8: Chapter 5    Context-Free Languages

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,B

BAλ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) w

G

ambiguous. is ,

sproductionth grammar wi The 5.10 Example

aSb|SS|λ S S

bSa

bSa

bSa

bSa

S

S

S

Page 9: Chapter 5    Context-Free Languages

trees.derivationdifferent twohas instance,For ambiguous. isgrammar The

|||

sproduction and

)},(,,*,,,,{},,{

sproduction with grammar heConsider t 5.11 Example

b*c a

a|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,TTETE

IFTE V

Page 10: Chapter 5    Context-Free Languages

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||||||||IIII

I|(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.

Page 11: Chapter 5    Context-Free Languages

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

Pushdown Automata

Chapter 7 Pushdown Automata

Page 12: Chapter 5    Context-Free Languages

Definition 7.1

A nondeterministic pushdown accepter or npda is defined by the septuple

states. final ofset theis Q F

symbol,stat stack theis Tz

unit, 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

TQP

T

Q

FzqT(Q,M

Page 13: Chapter 5    Context-Free Languages

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 The

7.2 Definition

0

0

u

Mw

TuFpupzwqΣ*w L(M)

F,zqQ,Σ,ΣMnpda

M

*

}{}0:{ anbaL nn

Page 14: Chapter 5    Context-Free Languages

)},,{()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

qq

qbq

qbq

qaq

qq

qqaq

qwith

qF

z

T

ba

qqqqQ

0q 1q

2q

,1,b

10,0,a

11,1,a

3q,1,b

,0,;,0,

;,0,

a