Download presentation
Presentation is loading. Please wait.
1
Context-Free Grammars (CFG’s)
Since sets are Languages, grammars can be given for defining sets. FORTRAN was defined by Backus and Naur in 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.
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. P, a set of Productions or grammar rules of the form Ni (Σ + N)* where Ni is a Non-terminal Some books use T for terminals instead of Σ (Often, P, the set of Productions, is explicitly given in a CFG, and Σ, N are left as implicitly understood)
3
An Example CFG 1. Σ = { a } 1. Σ = { a } 2. N = { S } 2. N = { S }
S aS S ^ } This CFG generates or derives a* = { ^, a, aa, aaa, aaaa, aaaaa, } Derivation for the string aa: S => aS => aaS => aa Derivation for the string aaa: S => aS => aaS => aaaS => aaa In CFG, the emphasis is on the set of productions, P; therefore, the above CFG can be given as: S aS S ^ A Notational variation of the same CFG grammar: S aS | ^ An Example CFG 1. Σ = { a } 2. N = { S } 3. P = { S aS S ^ }
4
Another 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 != ) 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.
5
Another Example CFG S aSb | ^
A Parse Tree for aaabbb based on the CFG given above: S a a a ^ b b b
6
An Example CFG S NP VP NP D N VP V NP D the | a N boy | girl
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. Many supplies were needed such as meat, fruit, vegetables, water ,and medicine to treat diseases such as cholera and scurvy. You always needed to take some worthy currency and a tiny bit of supplies at least(just in case) for buying mining supplies from California. Or you could optionally bring your own tools from home.
7
S NP VP D N V NP the girl kissed the boy
According to the CFG given in the previous section, a parse tree for “the girl kissed the boy” is given below: S NP VP D N V NP the girl kissed the boy
8
Every Regular Language is a CF Language
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: either a Non-terminal followed by any combination of Terminals, or any combination of Terminals on right side of the arrow. (That is, productions are of the form: Ni NjΣ* | Σ* ) (2) Right RG: any combination of Terminals followed by one Non-terminal, or else any combination of Terminals on right side of the arrow. (That is, productions are of the form: Ni Σ*Nj | Σ* ) The following Right RG (also known as Right Linear Grammar): S aS | ^ defines L9 = a* = { ^, a, aa, aaa, aaaa, aaaaa, } The following Left RG (also known as Left Linear Grammar) defines the same language: S Sa | ^
9
Thank You THE END!!! Thank You. I hope you enjoyed my slide show!!!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.