Top Banner
Formal Grammars Denning, Sections 3.3 to 3.6
27

Formal Grammars

Dec 31, 2015

Download

Documents

adele-harding

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 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Formal Grammars

Formal Grammars

Denning, Sections 3.3 to 3.6

Page 2: Formal Grammars

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

Page 3: Formal Grammars

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.

Page 4: Formal Grammars

Example G = (N,T,P,), where

N = { A }T = { 0, 1 }P = { ,

A,A 0A1,A 01

}

Page 5: Formal Grammars

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 .

Page 6: Formal Grammars

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

Page 7: Formal Grammars

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.

Page 8: Formal Grammars

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.

Page 9: Formal Grammars

Example derivation A 0A1 00A11 000111.

Thus the sentential form 000111 is derivable from .

. Thus the empty string is derivable from .

Page 10: Formal Grammars

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.

Page 11: Formal Grammars

Example In the previous example G,

L(G) = { , 01, 0011, 000111, … }= { 0k1k | k 0 }.

Page 12: Formal Grammars

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 }

Page 13: Formal Grammars

Types of Grammars

T0 unrestricted

T1 context sensitive

T2 context free

T3 regular

W set ofall strings

Page 14: Formal Grammars

T0 Unrestricted Grammars

Each production in P has the formA 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.

Page 15: Formal Grammars

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.

Page 16: Formal Grammars

T1 Example

AA aABC ; A abCCB * BCbB bbbC bccC cc

L(G) = {akbkck | k 1}

Page 17: Formal Grammars

T2 Context-Free Grammar

Each production in P has the formA where (N T)* - {}, and A=, or A N.

Page 18: Formal Grammars

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 }

Page 19: Formal Grammars

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

Page 20: Formal Grammars

T3 Example

Even parity grammar: A | ;A 0 | 0A | 1B ;B 1 | 0B | 1A ;

L(G) = Strings with even number of 1s

Page 21: Formal Grammars

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)

Page 22: Formal Grammars

Derivation Trees

The derivation tree T corresponding to a derivation = w1 w2 w3 … wn

is an ordered tree whose root is , and whose leaves are terminal symbols. If a step in the derivation is: 12 … kthen the subtree rooted at will have children 1, 2, … , and k.

Page 23: Formal Grammars

Derivation Tree Example A 0A1 00A11

A

A0 1

A0 1

Page 24: Formal Grammars

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.

Page 25: Formal Grammars

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.

Page 26: Formal Grammars

Example of Ambiguous Grammar N = { A } ;

T = { a, x, y } ; A ;A a | AyA | AxA ;

Page 27: Formal Grammars

Example ofUnambiguous Grammar N = { A, B } ;

T = { a, x, y } ; A ;A B | AyB ;B a | Bxa ;