27
Formal Grammars Denning, Sections 3.3 to 3.6

Formal Grammars

Embed Size (px)

DESCRIPTION

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

Citation preview

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 is the sentential symbol or start

symbol not in N or T

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.

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

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

A,A 0A1,A 01

}

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 .

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

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.

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.

Example derivation A 0A1 00A11 000111.

Thus the sentential form 000111 is derivable from .

. Thus the empty string is derivable from .

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.

Example In the previous example G,

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

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 }

Types of Grammars

T0 unrestricted

T1 context sensitive

T2 context free

T3 regular

W set ofall strings

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.

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.

T1 Example

AA aABC ; A abCCB * BCbB bbbC bccC cc

L(G) = {akbkck | k 1}

T2 Context-Free Grammar

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

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 }

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

T3 Example

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

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

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)

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.

Derivation Tree Example A 0A1 00A11

A

A0 1

A0 1

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.

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.

Example of Ambiguous Grammar N = { A } ;

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

Example ofUnambiguous Grammar N = { A, B } ;

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