1 / 9

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

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.

audrey-tate
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Context-Free Grammar (CFG) A CFG is composed of the followings components: • Σ, a finite non-empty set of symbols called Terminals • 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)

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

  4. 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. Another Example CFG

  5. CFG: S  aSb | ^ A Parse Tree for aaabbb based on the CFG given above: S S S S a a a ^ b b b Another Example CFG

  6. 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 boy THE ABOBE DERIVATION IS A LEFT-MOST DERIVATION; because at each step, the LEFT-MOST Non-terminal is expanded. An Example CFG

  7. 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, . . . . }

  8. Thank You THE END!!!

More Related