1 / 27

Formal Grammars

Formal Grammars. Denning, Sections 3.3 to 3.6. Formal Grammar, Defined. A formal grammar G is a four-tuple G = (N,T,P, ), where N is a finite nonempty set of nonterminal symbols T is a finite nonempty set of terminal symbols disjoint from N P is a finite nonempty set of productions

Download Presentation

Formal 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. Formal Grammars Denning, Sections 3.3 to 3.6

  2. Formal Grammar, Defined • A formal grammar G is a four-tupleG = (N,T,P,), where • N is a finite nonempty set of nonterminal symbols • T is a finite nonempty set of terminal symbols disjoint from N • P is a finite nonempty set of productions •  is the sentential symbol or start symbol not in N or T

  3. Formal Grammar, cont. • Each production in P has the formA  where , , and  are in (N  T)* and may possibly be empty, and A=, or A is in N. • Read: If A has  to its left and  to its right, then A can be replaced by , in that context. • If  and  are empty, then the production is A  , and A can be replaced by , context-free.

  4. Example • G = (N,T,P,), whereN = { A }T = { 0, 1 }P = {   ,   A, A  0A1, A  01 }

  5. Some Terminologies • Formal grammar – phrase structure grammar • Generating a sentence – successive rewriting of sentential forms through the use of productions of the grammar, starting with . • Derivation of a sentence – the sequence of sentential forms needed to generate the sentence from .

  6. Sentential Form • Let G be a formal grammar. A string of symbols in (N  T)*  {} is called a sentential form. • Examples00A11 is a sentential form0A100A is another sentential form is a sentential form is a sentential form

  7. Immediately Derived • Let G be a formal grammar. If    is a production of G, and  =  and ’ =  are sentential forms, then we say that ’ is immediately derived from  by the rule   , and we write   . • Example: From 0A1 we can immediately derive 00A11 by the rule A  0A1. We write 0A1  00A11.

  8. Derivation • If w1, w2, w3, … , wn is a sequence of sentential forms such thatw1  w2  w3  …  wnthen we say that wn is derivable from w1, and we write w1 * wn. The above sequence is called a derivation of wn from w1.

  9. Example derivation •   A  0A1  00A11  000111. Thus the sentential form 000111 is derivable from . •   . Thus the empty string is derivable from .

  10. Language of a Grammar • The language L(G) generated by a formal grammar G is the set of terminal strings derivable from .L(G) = { w  T* |  * w } • If w  L(G) we say that w is a string, sentence or word in the language generated by G.

  11. Example • In the previous example G,L(G) = { , 01, 0011, 000111, … }= { 0k1k | k  0 }.

  12. Example 2 • G = (N, T, P, )N = {A, B}T = {0, 1}P = {   | A | B; A  1 | 0A ; B  0 | 1B; } • L(G) = { , 0k1, 1k0 | k  0 }

  13. Types of Grammars T0 unrestricted T1 context sensitive W set ofall strings T2 context free T3 regular

  14. T0 Unrestricted Grammars • Each production in P has the formA  where , , and  are in (N  T)* and may possibly be empty, and A=, or A  N. • If  = , then |A| > ||, and grammar G is called contracting.

  15. T1 Context Sensitive Grammar • Each production in P has the formA    where , ,   (N  T)* and  and  may possibly be empty,   , and A=, or A  N.

  16. T1 Example •   AA  aABC ; A  abCCB * BCbB  bbbC  bccC  cc • L(G) = {akbkck | k  1}

  17. T2 Context-Free Grammar • Each production in P has the formA    where   (N  T)* - {}, and A=, or A  N.

  18. T2 Examples • Double parentheses language  A ;A  AA ;A  ( ) | (A) | [ ] | [A] ;L(G) = ? • Matching pair language  A ;A  aAb | ab ;L(G) = { akbk | k  1 }

  19. T3 Regular Grammar • Left Linear: Each production in P has the form:A  Ba | a ;    • Right Linear: Each production in P has the form:A  aB | a ;    • Here A  N or A=, B  N, a  T

  20. T3 Example • Even parity grammar:  A | ;A  0 | 0A | 1B ;B  1 | 0B | 1A ; • L(G) = Strings with even number of 1s

  21. T3 Example • Strings of 1s followed by zeroes:  A | B | ;A  1 | 1A | 1B ;B  0 | 0B ; • L(G) = Prefix of 1s (possibly empty) followed by suffix of 0s (possibly empty)

  22. Derivation Trees • The derivation tree T corresponding to a derivation = w1 w2  w3  …  wnis an ordered tree whose root is , and whose leaves are terminal symbols. If a step in the derivation is:  12 … kthen the subtree rooted at  will have children 1, 2, … , and k.

  23. Derivation Tree Example •   A  0A1  00A11  A 0 A 1 0 A 1

  24. Leftmost derivations • A leftmost derivation is a derivation in which at each step, only the left-most nonterminal symbol is replaced by a production of the grammar.

  25. Ambiguity • A grammar G is ambiguous if for some string w in L(G), w has more than one distinct leftmost derivation. • If for each string w in L(G), w has exactly one leftmost derivation, then the grammar is called unambiguous.

  26. Example of Ambiguous Grammar • N = { A } ;T = { a, x, y } ;  A ;A  a | AyA | AxA ;

  27. Example ofUnambiguous Grammar • N = { A, B } ;T = { a, x, y } ;  A ;A  B | AyB ;B  a | Bxa ;

More Related