context-free grammars

21
cs466(Prasad) L4CFG 1 Context-free Grammars

Upload: wilbur

Post on 19-Jan-2016

38 views

Category:

Documents


3 download

DESCRIPTION

Context-free Grammars. Recursive definition Grammar Derivation. What is the syntax of the description (meta-language)? External form of the grammar How do we associate a language with such a description? Semantics or use of the grammar - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Context-free Grammars

cs466(Prasad) L4CFG 1

Context-free Grammars

Page 2: Context-free Grammars

cs466(Prasad) L4CFG 2

• Recursive definition

• Grammar

• Derivation

,...},,{ aaaL

LaL

L

aaaSS

aSSS

Page 3: Context-free Grammars

cs466(Prasad) L4CFG 3

• What is the syntax of the description (meta-language)?

• External form of the grammar

• How do we associate a language with such a description?

• Semantics or use of the grammar

• Generators: Grammars specify languages by generating strings in the language.

• Recognizers: Grammars can be viewed as a notation for describing a family of recognition algorithms.

Page 4: Context-free Grammars

cs466(Prasad) L4CFG 4

CFG = (V, ,P,S)

• V : Finite set of variables/non-terminals

• : Alphabet/Finite set of terminals

• P : Finite set of rules/productions

• S : Start symbol

*)(V VA

A :

V

VS

Rule

Page 5: Context-free Grammars

cs466(Prasad) L4CFG 5

• a* represents a context-free language because we can write a CFG for it.

• A-rule, null-rule, -rule.

• Context-freeness:Context-freeness: An A-rule can be applied whenever A occurs in a string, irrespective of the context (that is, non-terminals and terminals around A).

• Cf. context-sensitive grammar (“declare-use”)

)},,{},{},({ SaSSSaS A

Page 6: Context-free Grammars

cs466(Prasad) L4CFG 6

• One-step Derivation (Defn: 3.1.2)

• w is derivable from v in CFG, if there is a finite sequence of rule applications such that:

From CFG to Language

A vuuAv

wwwv n ...1

Page 7: Context-free Grammars

cs466(Prasad) L4CFG 7

Let G=(V, , P, S) be a CFG.

• is a sentential form, if

.

• is a sentence, if .

• The language of G,

L(G) =

*)V( wwS *

G

*w wS *G

}S|*{ *G ww

}0|{)(

)},,{},,{},{{

nbaGL

SaSbSSbaSGnn

Page 8: Context-free Grammars

cs466(Prasad) L4CFG 8

• Derivation of aabb

• Derivation tree

aabbaaSbbaSbS

S

a bS

a b

Page 9: Context-free Grammars

cs466(Prasad) L4CFG 9

*)*()(

)

},|

,|,{

},,{},,,({

baLGL

S

BbB

aAAABS

baBASG

abaAbAbABbABS

abaBbaBaABABS

:DerivationRightmost

:erivationLeftmost D

Page 10: Context-free Grammars

cs466(Prasad) L4CFG 10

Derivation Tree (abstracts derivation)

S

A B

a A B b

Page 11: Context-free Grammars

cs466(Prasad) L4CFG 11

Examples: CFGs and CFLs

Page 12: Context-free Grammars

cs466(Prasad) L4CFG 12

bbBB

aBaaSaS

|

|

}00|{)(

}0|{)(

mnabaSL

mbBLnmn

m

Page 13: Context-free Grammars

cs466(Prasad) L4CFG 13

||

bBB

BaSaS

}00|{)( mnabaSL nmn

|abScS

}0|){()( ncabSL nn

Note that parenthesesare part of the meta-language.

Page 14: Context-free Grammars

cs466(Prasad) L4CFG 14

||

bBB

aaAA

ABS

)()(

}00|{)(*baLSL

nmbaSL mn

||

bBB

aBaSS

Page 15: Context-free Grammars

cs466(Prasad) L4CFG 15

bbBB

abScBS

|

|

)}01:( 0

|){()(1

i

n

i

mn

mniin

cbabSL i

Page 16: Context-free Grammars

cs466(Prasad) L4CFG 16

*** babaa

|aAA

AbAbAS

||

|

aCC

bCaAA

bAB

BaSS

Left to right generation of string.

Page 17: Context-free Grammars

cs466(Prasad) L4CFG 17

• A regular grammar regular grammar is a CFG in which each of the rules has the form:

(Right Linear Grammar)

Regular Grammar

. and , where

aVBA

A

aBA

aA

Page 18: Context-free Grammars

cs466(Prasad) L4CFG 18

• A regular grammar regular grammar is a CFG in which each of the rules has the form:

(Left Linear Grammar)

Equivalent Regular Grammar

. and , where

aVBA

A

BaA

aA

Page 19: Context-free Grammars

cs466(Prasad) L4CFG 19

} EVENis )(length |}*,{{ wbawL

bbEbaE

abEaaE

E

| |

| |

bEaEO

bOaOE

|

| |

}s' ofnumber EVEN has |}*,{{ bwbawL

bEaOO

bOaEE

|

| |

Page 20: Context-free Grammars

cs466(Prasad) L4CFG 20

| |

| |

|

| |

|

resetb

seenAaseenAB

seenABb

resetcseenAaseenA

seenAa

resetcresetbreset

} containnot does |}*,,{{ abcwcbawL

Page 21: Context-free Grammars

cs466(Prasad) L4CFG 21

Derivations

G. in derivationa has

)(

iffGL

G. in derivationleftmost a has

)(

iffGL

Note that the above result does not hold for sentential forms.

ASAABS

ABS

l

B A