Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a,...

22
Lecture # 19 Lecture # 19

Transcript of Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a,...

Page 1: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Lecture # 19Lecture # 19

Page 2: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Example Example Consider the following CFG Consider the following CFG ∑∑ = {a, b} = {a, b}

1.1. S S aSa | bSb | a | b | aSa | bSb | a | b | ΛΛ

The above CFG generates the language The above CFG generates the language PALINDROMEPALINDROME. .

It may be noted that the CFG It may be noted that the CFG S S aSa | bSb | a | b aSa | bSb | a | b generates the generates the language NON-NULLPALINDROME.language NON-NULLPALINDROME.

Page 3: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Example Example Consider the following CFG Consider the following CFG ∑∑ = {a, b} = {a, b}

1.1. S S aSb | ab | aSb | ab | ΛΛ

It can be observed that the CFG generates It can be observed that the CFG generates the language {the language {aannbbnn: n=: n=00,1,2,3, …}. ,1,2,3, …}.

It may also be noted that the language It may also be noted that the language {{aannbbnn : n= : n=11,2,3, …} can be generated by ,2,3, …} can be generated by the following CFG the following CFG S S aSb|ab aSb|ab

Page 4: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Example Example Consider the following CFG Consider the following CFG ∑∑ = {a, b} = {a, b}

1.1. S S aXb | bXa | aXb | bXa | ΛΛ

2.2. X X aX | bX | aX | bX | ΛΛ

The above CFG generates the language of The above CFG generates the language of strings, defined over strings, defined over ∑∑ ={a,b}, ={a,b}, beginning beginning and ending in different lettersand ending in different letters. .

Page 5: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Trees Trees As in English language any sentence can be As in English language any sentence can be

expressed by parse tree, so any word expressed by parse tree, so any word generated by the given CFG can also be generated by the given CFG can also be expressed by the parse tree, expressed by the parse tree, e.g.e.g.

1.1. S S AA AA

2.2. A A AAA AAA | | bA bA | | Ab Ab ||a a Obviously, Obviously, baabbaab can be generated by the can be generated by the

above CFG. To express the word baab as a above CFG. To express the word baab as a parse tree, start with S. Replace S by the parse tree, start with S. Replace S by the string AA, of nonterminals, drawing the string AA, of nonterminals, drawing the downward lines from S to each character of downward lines from S to each character of this string as follows this string as follows

Page 6: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Trees continued … Trees continued …

Now let the left A be replaced by bA and Now let the left A be replaced by bA and the right one by Ab then the tree will be the right one by Ab then the tree will be

Page 7: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Trees continued … Trees continued …

Replacing both A’s by a, the above tree Replacing both A’s by a, the above tree will bewill be

Page 8: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Trees continued … Trees continued …

Thus the word Thus the word baabbaab is generated. is generated.

The above tree to generate the word baab The above tree to generate the word baab is called is called Syntax treeSyntax tree or or Generation treeGeneration tree or or Derivation treeDerivation tree as well. as well.

Page 9: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Ambiguous CFG Ambiguous CFG

The CFG is said to be ambiguous if there The CFG is said to be ambiguous if there exists atleast one word of it’s language exists atleast one word of it’s language that can be generated by the different that can be generated by the different production trees. production trees.

ExampleExample: Consider the following CFG : Consider the following CFG S S aS aS||SaSa||a a

The word The word aaaaaa can be generated by the can be generated by the following following threethree different trees different trees

Page 10: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Example continued … Example continued …

Thus the above CFG is ambiguous, while the Thus the above CFG is ambiguous, while the CFG S CFG S aS|a is not ambiguous as neither the aS|a is not ambiguous as neither the word aaa nor any other word can be derived word aaa nor any other word can be derived from more than one production trees. from more than one production trees.

Page 11: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Example Example

Consider the following CFG Consider the following CFG

S S aS | bS | aaS | aS | bS | aaS | ΛΛ It can be observed that the word It can be observed that the word aaaaaa can can

be derived from more than one production be derived from more than one production trees. Thus, the above CFG is ambiguous. trees. Thus, the above CFG is ambiguous. This ambiguity can be removed by This ambiguity can be removed by removing the production removing the production S S aaS aaS

Following is another exampleFollowing is another example

Page 12: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Example Example

Consider the CFG of the language Consider the CFG of the language PALINDROME PALINDROME S S aSa|bSb|a|b|aSa|bSb|a|b|ΛΛ

It may be noted that this CFG is It may be noted that this CFG is unambiguousunambiguous as all the words of the as all the words of the language PALINDROME can only be language PALINDROME can only be generated by a unique production tree.generated by a unique production tree.

It may be noted that if the production It may be noted that if the production S S aaSaaaaSaa is added to the given CFG, the is added to the given CFG, the CFG thus obtained will be no more CFG thus obtained will be no more unambiguous.unambiguous.

Page 13: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Total language tree Total language tree

For a given CFG, a tree with the start For a given CFG, a tree with the start symbol S as its root and whose nodes are symbol S as its root and whose nodes are working strings of terminals and non-working strings of terminals and non-terminals. terminals.

The descendants of each node are all The descendants of each node are all possible results of applying every possible results of applying every production to the working string. This tree production to the working string. This tree is called is called total language treetotal language tree. Following . Following is an example of total language tree is an example of total language tree

Page 14: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Example Example Consider the following CFG Consider the following CFG

S S aa aa||bXbX||aXX aXX

X X ab ab||b, then the total language tree for b, then the total language tree for the given CFG may be the given CFG may be

Page 15: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Example continued … Example continued …

It may be observed from the previous total It may be observed from the previous total language tree that dropping the repeated language tree that dropping the repeated words, the language generated by the words, the language generated by the given CFG is given CFG is

{{aa, bab, bb, aabab, aabb, abab, abbaa, bab, bb, aabab, aabb, abab, abb}}

Page 16: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Example Example Consider the following CFG Consider the following CFG

S S X|b X|b , , X X aX aX

then following will be the total language then following will be the total language tree of the above CFGtree of the above CFG

NoteNote: It is to be noted: It is to be noted

that the only word in that the only word in

this language is this language is bb..

Page 17: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Regular Grammar Regular Grammar

All regular languages can be generated by All regular languages can be generated by CFGs. Some nonregular languages can be CFGs. Some nonregular languages can be generated by CFGs but not all possible generated by CFGs but not all possible languages can be generated by CFG, languages can be generated by CFG, e.g. e.g.

The CFG S The CFG S aSbaSb||ab generates the ab generates the language {language {aannbbnn: n=1,2,3, …}, which is : n=1,2,3, …}, which is nonregularnonregular..

NoteNote: It is to be noted that for every FA, : It is to be noted that for every FA, there exists a CFG that generates the there exists a CFG that generates the language accepted by this FA. Following is language accepted by this FA. Following is an example in this regardan example in this regard

Page 18: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Regular grammar continued Regular grammar continued … …

Example: Example:

Consider the language L expressed by Consider the language L expressed by (a+b)*aa(a+b)*(a+b)*aa(a+b)* i.e.i.e.the language of the language of strings, defined over strings, defined over ∑∑ ={a,b}, ={a,b}, containing aa.containing aa. To construct the CFG To construct the CFG corresponding to L, consider the FA corresponding to L, consider the FA accepting L, as follows accepting L, as follows

Page 19: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Regular grammar continued Regular grammar continued … …

CFG corresponding to the above FA may beCFG corresponding to the above FA may be

S S bS|aA bS|aA

A A aB|bS aB|bS

B B aB|bB| aB|bB| ΛΛ

Page 20: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Regular Grammar continued Regular Grammar continued … …

SemiwordSemiword: A semiword is a string of : A semiword is a string of terminals (may be none) concatenated with terminals (may be none) concatenated with exactly one nonterminal on the right exactly one nonterminal on the right i.e. i.e. a a semiword, in general, is of the following semiword, in general, is of the following form form (terminal)(terminal)… (terminal)(terminal)(terminal)… (terminal)(nonterminal)(nonterminal)

wordword: A word is a : A word is a string of terminalsstring of terminals. . ΛΛ is is also a word. also a word.

Page 21: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Theorem Theorem

If every production in a CFG is one of the If every production in a CFG is one of the following forms following forms

1.1. Nonterminal Nonterminal semiwordsemiword

2.2. Nonterminal Nonterminal wordword

then the language generated by that then the language generated by that CFG is regular.CFG is regular.

Page 22: Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.

Thank You…Thank You…