Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Formal Grammars Denning, Sections 3.3 to 3.6

2 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  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,  ), where N = { 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. Examples 00A11 is a sentential form 0A100A 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 that w1  w2  w3  …  wn then 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, … } = { 0 k 1 k | 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) = {, 0 k 1, 1 k 0 | k  0 }

13 Types of Grammars T 0 unrestricted T 1 context sensitive T 2 context free T 3 regular W set of all strings

14 T 0 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 T 1 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 T 1 Example   A A  aABC ; A  abC CB  * BC bB  bb bC  bc cC  cc L(G) = {a k b k c k | k  1}

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

18 T 2 Examples Double parentheses language   A ; A  AA ; A  ( ) | (A) | [ ] | [A] ; L(G) = ? Matching pair language   A ; A  aAb | ab ; L(G) = { a k b k | k  1 }

19 T 3 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 T 3 Example Even parity grammar:   A | ; A  0 | 0A | 1B ; B  1 | 0B | 1A ; L(G) = Strings with even number of 1s

21 T 3 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  = w 1  w 2  w 3  …  w n is 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 A01 A01

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 of Unambiguous Grammar N = { A, B } ; T = { a, x, y } ;   A ; A  B | AyB ; B  a | Bxa ;


Download ppt "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."

Similar presentations


Ads by Google