context-free grammars (cfg’s) since sets are languages, grammars

9
Context-Free Grammars (CFG’s) Since sets are Languages, grammars can be given for defining sets. FORTRAN was defined by Backus and Naur in 1954 in a version of CFG known as the Backus Naur Form (BNF) and then the first compiler was developed for FORTRAN. Every compiler is based on a CFG. CFG’s will be introduced with examples in the next few slides. If you do not see anything on the screen then just press Enter a few times.

Upload: audrey-tate

Post on 02-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Context-Free Grammars (CFG’s) Since sets are Languages, grammars can be given for the defining sets. FORTRAN was defined by Backus and Naur in 1954 in a version of CFG known as the Backus Naur Form (BNF) and then the first compiler was developed for FORTRAN. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Context-Free Grammars  (CFG’s)  Since  sets  are  Languages,  grammars

Context-Free Grammars (CFG’s)

Since sets are Languages, grammars

can be given for defining sets.

FORTRAN was defined by Backus and Naur in 1954 in a version of CFG known as the Backus Naur Form (BNF) and then the first compiler was developed for FORTRAN. Every compiler is based on a CFG.

CFG’s will be introduced with examples in the next few slides.

If you do not see anything on the screen then just press Enter a few times.

Page 2: Context-Free Grammars  (CFG’s)  Since  sets  are  Languages,  grammars

A CFG is composed of the followings components:

1. Σ, a finite non-empty set of symbols called Terminals

2. N, a finite non-empty set of Non-terminals disjoint from Σ;

S, is a special start symbol from the set N.

3. P, a set of Productions or grammar rules

of the form

Ni (Σ + N)*

where Ni is a Non-terminal

(Often, P, the set of Productions, is explicitly mentioned and Σ, N are left as

implicitly understood)

Page 3: Context-Free Grammars  (CFG’s)  Since  sets  are  Languages,  grammars

1. Σ = { a }

2. N = { S }

3. P = {

S aS

S ^ }

As mentioned before the emphasis is on the set of productions, P.

This CFG generates or derives

a* = { ^, a, aa, aaa, aaaa, aaaaa, . . . }

Derivation of aaa:

S => aS => aaS => aaaS => aaa

A Notational variation of the same CFG grammar:

S aS | ^

An Example CFG

Page 4: Context-Free Grammars  (CFG’s)  Since  sets  are  Languages,  grammars

S aSb | ^A Notational variation of the same CFG: S aSb

S ^

This CFG generates/derives

{anbn : where n >= 0} = {^, ab, aabb, aaabbb, aaaabbbb, . . . } (In String Theory, a0 = ^ , a0b0 = ^^ = ^ ; that is, a0 != 1 )

Derivation of aaabbb: S => aSb => aaSbb => aaaSbbb => aaabbb

The derivation always starts with the start symbol , S , and stops when all Non-terminals are eliminated. => is used in derivation where as is used in grammars.

Page 5: Context-Free Grammars  (CFG’s)  Since  sets  are  Languages,  grammars

CFG:

S aSb | ^

A Parse Tree for aaabbb based on the CFG given above:

S

S S S

a a a ^ b b b

Page 6: Context-Free Grammars  (CFG’s)  Since  sets  are  Languages,  grammars

S NP VP

  NP D N  

VP V NP   D the | a N boy | girl V likes | kissed | saved

This CFG can derive:

{ the boy likes the girl, the girl kissed the boy, a boy saved the girl, . . . }

Derivation of the girl kissed the boy:

S => NP VP => D N VP => the N VP => the girl VP => the girl V NP => the girl kissed NP => the girl kissed D N => the girl kissed the N => the girl kissed the boyTHE ABOBE DERIVATION IS A LEFT-MOST DERIVATION; because

at each step, the LEFT-MOST Non-terminal is expanded.

An Example CFG

Page 7: Context-Free Grammars  (CFG’s)  Since  sets  are  Languages,  grammars

Every Regular Language is a CF Language. Every Regular Grammar(RG) is a CFG.

A Regular Grammar (RG) can be defined as a CFG with the following two types of restrictions on the right hand side of the arrow: (1) Left RG: a Non-terminal followed by any combination of Terminals or any combination of Terminals

(That is, productions are of the form: Ni NjΣ* | Σ* )

(2) Right RG: any combination of Terminals followed by one Non-terminal

(That is, productions are of the form: Ni Σ*Nj | Σ* )

The following Right RG grammar: S aS | ^

Defines L9 = a* = { ^, a, aa, aaa, aaaa, aaaaa, . . . . }

Page 8: Context-Free Grammars  (CFG’s)  Since  sets  are  Languages,  grammars
Page 9: Context-Free Grammars  (CFG’s)  Since  sets  are  Languages,  grammars