90 likes | 242 Views
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.
E N D
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.
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)
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
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
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
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
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, . . . . }
Thank You THE END!!!