Top Banner
Context free languages 1. Equivalence of context free grammars 2. Normal forms
37

Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Dec 14, 2015

Download

Documents

Vernon Waithe
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: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Context free languages

1. Equivalence of context free grammars2. Normal forms

Page 2: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Context-free grammars In a context free grammar, all

productions are of the formA -> w,where A is a nonterminal or the start symbol S, and w is a string from(N T)*

Page 3: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Handles, recursive productions In the production A -> xw,

prefix x, if a single symbol, is called the handle of the production, whether x is in N or T

A productionA -> Awis called left-recursive

The productionA -> wAis called right recursive

Page 4: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Repeated sentential forms

In a derivation, the sentential form wAxS-> … -> wAx -> … -> wAx -> …is called a repeated sentential form. All the intervening steps are wasted steps.

Page 5: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Leftmost derivationsMinimal leftmost derivations A derivation is a leftmost

derivation if at each step only the leftmost nonterminal symbol is replaced using some rule of the grammar.

A leftmost derivation is called minimal if no sentential form is repeated in the derivation

Page 6: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Weak equivalence Two context-free grammars G1

and G2 are called weakly equivalent ifL(G1) = L(G2)

Page 7: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Example of weak equivalence G1: S -> S01; S -> 1

L(G1) = { 1(01)* } G2: S -> S0S; S -> 1

L(G2) = { 1(01)* }

Page 8: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Strong equivalence Two CFGs G1 and G2 are called

strongly equivalent if they are weakly equivalent, and for each string w of terminals in L(G1) = L(G2), and the minimal left-most derivations of w in G1 and the minimal left-most derivations of w in G2 are exactly the same in number, and so can be put into one-to-one correspondence.

Page 9: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Strong equivalence Thus G1 and G2 must both be

unambiguous, or must both be ambiguous in exactly the same number of ways, for each string w in T*

Page 10: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Weakly equivalent but not strongly equivalent G1: Grammar of expressions S:

S -> T | S + T;T -> F | T * F;F -> a | ( S );

G2: Grammar of expressions S:S -> E;E -> E + E | E * E | (E) | a;

L(G1) = L(G2) = valid expressions using a, +, *, (, and ). G1 has operator precedence.

Page 11: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Example: Strong equivalence G1: S->A; A->1B; A->1; B->0A

L(G1) = { (10)*1 }

G2: S->B; B->A1; B->1; A->B0L(G2) = { 1(01)* }

Page 12: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Elementary transformations of context free grammars substitution expansion removal of useless productions removal of non-generative

productions removal of left recursive

productions

Page 13: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Substitution If G has the A-rule, A->uBv,

and all the B-rules are:B->w1, B->w2, . . . , B->wk, then

1. Remove the A-rule A->uBv2. Add the A-rules: A->uw1v, A->uw2v, . . . , A->uwkv3. Keep all the other rules of G, including the B-rules

Page 14: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Example of substitution G1: S->H;

H->TT;T->S; T->aSb; T->c

G2: S->H;H->ST; H->aSbT; H->cT;T->S; T->aSb; T->c;

Page 15: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Strong equivalence after substitution The grammar G, and the grammar

G’ obtained by substitution of B into the A-rule, are strongly equivalent if steps 2 and 3 do not introduce duplicate rules.

Page 16: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Expansion If a grammar has the A-rule, A->uv Remove this A-rule, and replace it

with the two rules A->Xv; X->u; or with A->uY; Y->v

where X (or Y) is a new non-terminal symbol of the grammar.

Page 17: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Strong equivalence after expansion If G is context free, and G’ is

obtained from G by expansion, then G and G’ are strongly equivalent.

Page 18: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Useful production A production A->w of a cfg G is useful

if there is a string x from T* such thatS-> . . -> uAv -> uwv -> . . -> x

Otherwise the production, A->w is useless

Thus, a production that is never used to derive a string of terminals is useless

Page 19: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Removing useless productions T-marking S-marking Productions that are both T-

marked and S-marked are useful. All other productions can be removed.

Page 20: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

T-marking Construct a sequence P0, P1,

P2, . . . , of subsets of P, and a sequence N0, N1, N2, . . . of subsets of N as follows: P0 = empty, N0 = empty, j = 0 P[j+1] = { A->w|w in (N[j] + T)* } N[j+1] = { A in N | P[j+1] contains a rule

A->w } Continue until P[j] = P[j+1] = P[T]

Page 21: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

S-marking Construct a sequence Q1, Q2, Q3, .

. . of subsets of P[T] as follows: Q1 = {S->w in P[T]} Q[j+1] = Q[j] + {A->w in P[T] | Q[j]

contains a rule B->uAv } Continue until Q[j] = Q[j+1] = P[S]

P[S] are now the useful productions.

Page 22: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Example: T/S-marking Rule T mark S mark

1. S->H 2 12. H->AB3. H->aH 2 24. H->a 1 25. B->Hb 26. C->aC

Thus only 1,3,4 are useful

Page 23: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Strong equivalence after removal of useless productions

If grammar G’ is obtained from grammar G after removal of useless productions of grammar G, then G and G’ are strongly equivalent.

Page 24: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Removing non-generative productions

Page 25: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Removing left-recursive rules Let all the X-rules of grammar G be:

X->u1 | u2 | . . . | uk

X->Xw1 | Xw2 | . . . | Xwh

Then these rules may be replaced by the following:X->u1 | u2 | . . . | uk

X->u1Z | u2Z | . . . | ukZZ->w1 | w2 | . . . | wh

Z->w1Z | w2Z | . . . | whZwhere Z is a new non-terminal symbol

Page 26: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Example: Removing left-recursive rules S->E; S->E;

E->T | aT | bT; E->T | aT | bT; E->EaT | EbT; E->TG | aTG | bTG; T->F; G->aT | bT;T->TcF | TdF; G->aTG | bTG; F->n | xEy T->F;

T->FH;H->cF | dF;H->cFH | dFH;

F->n | xEy

Page 27: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Strong equivalence after removal of left-recursive rules If grammar G’ is obtained from

grammar G by replacing the left-recursive rules of G by right recursive rules to get G’, then G and G’ are strongly equivalent.

Page 28: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Well-formed grammars A context free grammar

G=(N,T,P,S) is well-formed if each production has one of the forms:S->S->AA->wwhere A N and w (N+T)* - N and each production is useful.

Page 29: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Example of well-formed grammars Parenthesis grammar

S->A;A->AA;A->(A);A->();

Page 30: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Chomsky Normal form A context free grammar G=(N,T,P,S)

is in normal form (Chomsky normal form) if each production has one of the forms:S->S->AA->BCA->awhere A,B,C N and a T.

Page 31: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Example of Chomsky normal form grammar Parenthesis grammar

S->A; S->A; A->AA; A->AA;A->(A); A->BC;

B-> (;C->AD;D->);

A->(); A->BD;

Page 32: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Chomsky Normal Form Theorem From any context free grammar,

one can construct a strongly equivalent grammar in Chomsky normal form.

Page 33: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Greibach normal form(standard form) A context free grammar G=(N,T,P,S)

is in standard form (Greibach normal form) if each production has one of the forms:S->S->AA->awwhere A N, a T, and w (N+T)*.

Page 34: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Example: converting to Greibach standard form First remove left-recursive rules:

S->E; S->E;E->T; E->T;E->EaT; E->TF;T->n; F->aT;T->xEy; F->aTF;

T->n;T->xEy;

Page 35: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Converting to Greibach: then substitute to get nonterminal

handles S->E; S->E;

E->T; E->n | xEy;E->TF; E->nF | xEyF;F->aT; F->aT; F->aTF; F->aTF; T->n; T->n; T->xEy; T->xEy;

Page 36: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Standard Form Theorem From any context free grammar,

one can construct a strongly equivalent grammar in standard form (Greibach normal form).

Page 37: Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Pumping Lemma for context free languages If L is a context free language,

then there exists a positive integer p such that: if w L and |w| > p, thenw = xuyvz, with uv and y nonempty and xukyvkz L for all k 0.