Lecture # 18

29
Lecture # 18 Lecture # 18

description

Lecture # 18. Nonregular languages. Nonregular languages. The language that cannot be expressed by any regular expression is called a Nonregular language . The languages PALINDROME and PRIME are the examples of nonregular languages. - PowerPoint PPT Presentation

Transcript of Lecture # 18

Lecture # 18Lecture # 18

Nonregular languages

Nonregular languages

The language that cannot be expressed by any regular expression is called a Nonregular language.

The languages PALINDROME and PRIME are the examples of nonregular languages.

Note: It is to be noted that a nonregular language, by Kleene’s theorem, can’t be accepted by any FA or TG.

Example

Consider the language L = {Λ, ab, aabb, aaabbb, …} i.e. {an bn : n=0,1,2,3,…}

Suppose, it is required to prove that this language is nonregular. Let, contrary, L be a regular language then by Kleene’s theorem it must be accepted by an FA, say, F. Since every FA has finite number of states then the language L (being infinite) accepted by F must have words of length more than the number of states. Which shows that, F must contain a circuit.

Example Cont…. For the sake of convenience suppose that F

has 10 states. Consider the word a9 b9 from the language L and let the path traced by this word be shown as under.

Example Cont…. But, looping the circuit generated by the

states 3,4,6,5,3 with a-edges once more, F also accepts the word a9+4 b9, while a13 b9 is not a word in L. It may also be observed that, because of the circuit discussed above, F also accepts the words a9 (a4)m b9, m = 1,2,3, …

Moreover, there is another circuit generated by the states 9,10,9. Including the possibility of looping this circuit, F accepts the words a9

(a4)m b9 (b2)n where m,n=0,1,2,3,…(m and n not being 0 simultaneously ).Which shows that F accepts words that are not belonging to L.

Example Cont….

Similarly for finding FAs accepting other words from L, they will also accept the words which do not belong to L.

Thus there is no FA which accepts the language L. which shows, by Kleene’s theorem, that the language L can’t be expressed by any regular expression.

It may be noted that apparently an bn seems to be a regular expression of L, but in fact it is not.

Context Free Grammar Context Free Grammar ((CFGCFG) )

The high level language is converted into The high level language is converted into assembly language codes by a program assembly language codes by a program called compiler. called compiler.

The compiler that takes the user’s programs The compiler that takes the user’s programs as its inputs and prints out an equivalent as its inputs and prints out an equivalent program written in assembly language. program written in assembly language.

Like spoken languages, high level languages Like spoken languages, high level languages for computer have also, certain grammar. But for computer have also, certain grammar. But in case of computers, the grammatical rules, in case of computers, the grammatical rules, don’t involve the meaning of the words.don’t involve the meaning of the words.

CFG continued … CFG continued …

It can be noted that the grammatical rules It can be noted that the grammatical rules which involve the meaning of words are which involve the meaning of words are called called SemanticsSemantics, while those don’t , while those don’t involve the meaning of the words are involve the meaning of the words are called called SyntacticsSyntactics. . e.g.e.g. in English in English language, it can not be written “ language, it can not be written “ Buildings Buildings singsing ”, while in computer language one ”, while in computer language one number is as good as another. number is as good as another. e.g. e.g. X = X = B + B + 1010, X = B + , X = B + 999999

Following is a remarkFollowing is a remark

Remark Remark

In general, the rules of computer language In general, the rules of computer language grammar, are all grammar, are all syntacticsyntactic and and not not semanticsemantic..

A A law of grammarlaw of grammar is in reality a suggestion is in reality a suggestion for for possible substitutionspossible substitutions. .

CFG terminologies CFG terminologies

TerminalsTerminals: The symbols that can’t be : The symbols that can’t be replaced by anything are called terminals. replaced by anything are called terminals.

Non-TerminalsNon-Terminals: The symbols that must : The symbols that must be replaced by other things are called non-be replaced by other things are called non-terminals.terminals.

ProductionsProductions: The grammatical rules are : The grammatical rules are often called productions. often called productions.

CFGCFG

CFG is a collection of the followings CFG is a collection of the followings 1.1. An alphabet An alphabet ∑∑ of letters called of letters called terminalsterminals

from which the strings are formed, that from which the strings are formed, that will be the words of the language. will be the words of the language.

2.2. A set of symbols called non-terminals, A set of symbols called non-terminals, one of which is S, stands for “one of which is S, stands for “start herestart here”. ”.

3.3. A finite set of productions of the form A finite set of productions of the form non-terminal non-terminal finite string of terminals finite string of terminals and /or non-terminalsand /or non-terminals. .

Following is a note in this regard Following is a note in this regard

Note Note

The terminals are designated by small The terminals are designated by small letters, while the non-terminals are letters, while the non-terminals are designated by capital letters. designated by capital letters.

There is at least one production that has There is at least one production that has the non-terminal S as its left side. the non-terminal S as its left side.

Context Free Language Context Free Language (CFL) (CFL)

The language generated by CFG is called The language generated by CFG is called Context Free Language (Context Free Language (CFLCFL). ).

ExampleExample

∑∑ = {a}= {a}

Productions: Productions:

1.1. S S a S a S

2.2. SS ΛΛ Applying production (1) six times and then Applying production (1) six times and then

production (2) once, the word production (2) once, the word aaaaaaaaaaaa is is generated as generated as

Example Cont….Example Cont….

S S a S a S

aa S aa S

aaa S aaa S

aaaa S aaaa S

aaaaa S aaaaa S

aaaaaa S aaaaaa S

aaaaaa S aaaaaa S

= aaaaaa= aaaaaa

Example continued … Example continued …

a can be generated applying prod. (1) a can be generated applying prod. (1) once and then prod. (2), aa can be once and then prod. (2), aa can be generated applying prod. (1) twice and generated applying prod. (1) twice and then prod. (2) and so on. then prod. (2) and so on.

This shows that the grammar defines the This shows that the grammar defines the languagelanguage expressed by expressed by a*a*..

ExampleExample ∑∑ = {a}= {a}

Productions: Productions:

1.1. SSSS SS

2.2. SSa a

3.3. SSΛΛ

This grammar also defines the language This grammar also defines the language expressed by expressed by a*a*..

NoteNote: : ΛΛ has a special status. If for a certain has a special status. If for a certain non-terminal N, there may be a production non-terminal N, there may be a production NN ΛΛ. This simply means that N can be . This simply means that N can be deleted when it comes in the working deleted when it comes in the working string.string.

Example Example ∑∑ = {a, b}= {a, b}

Productions: Productions:

1.1. SSXX

2.2. SSYY

3.3. XX ΛΛ

4.4. YYaY aY

5.5. YYbY bY

6.6. YYa a

7.7. YYbb

Example continued … Example continued …

All words of this language are of either X-All words of this language are of either X-type or of Y-type. type or of Y-type. i.e. i.e. while generating a while generating a word the first production used is Sword the first production used is SX or X or SSY. Y.

The words of The words of X-typeX-type give only give only ΛΛ, while the , while the words of words of Y-typeY-type are words of are words of finite strings finite strings of a’sof a’s or or b’sb’s or or bothboth i.e. i.e. (a+b)(a+b)++..

Thus the language defined is expressed by Thus the language defined is expressed by (a+b)*(a+b)*. .

Example Example ∑∑ = {a, b}= {a, b} Productions: Productions:

1.1. SSaS aS 2.2. SSbS bS 3.3. SSa a 4.4. SSb b 5.5. SS ΛΛ

This grammar also defines the This grammar also defines the language expressed by language expressed by (a+b)*(a+b)*. .

Example Example

∑∑ = {a, b}= {a, b}

Productions: Productions:

1.1. SSXaaX XaaX

2.2. XXaX aX

3.3. XXbX bX

4.4. XXΛΛ

This grammar defines the language This grammar defines the language expressed by expressed by (a+b)*aa(a+b)*(a+b)*aa(a+b)*. .

Example Example ∑∑ = {a, b}= {a, b}

Productions: Productions:

1.1. S S SS SS

2.2. S S XS XS

3.3. S S ΛΛ

4.4. S S YSY YSY

5.5. X X aa aa

6.6. X X bb bb

7.7. Y Y ab ab

8.8. Y Y ba ba

This grammar generates This grammar generates EVEN-EVENEVEN-EVEN language language

Example Example ∑∑ = {a, b}= {a, b}

1.1. S S aB aB 2.2. S S bA bA 3.3. A A a a 4.4. A A aS aS 5.5. A A bAA bAA 6.6. B B b b 7.7. B B bS bS 8.8. B B aBB aBBThis grammar generates the languageThis grammar generates the language

EQUALEQUAL(The language of strings, with (The language of strings, with number of a’s equal to number of b’s). number of a’s equal to number of b’s).

Note Note It is to be noted that if the same non-terminal It is to be noted that if the same non-terminal

have more than one productions, it can be have more than one productions, it can be written in single line written in single line e.g. e.g. S S aS, S aS, S bS bS, , S S ΛΛ can be written as can be written as S S aS|bS| aS|bS| ΛΛ

It may also be noted that the productions It may also be noted that the productions S S SS| SS| ΛΛ always defines the language which always defines the language which is closed w.r.t. is closed w.r.t. concatenationconcatenation i.e. i.e. the language the language expressed by expressed by RERE of type of type r*r*. .

It may also be noted that the production It may also be noted that the production S S SS SS defines the language expressed by defines the language expressed by rr++..

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

Productions: Productions: 1.1. S S YXY YXY 2.2. Y Y aY | bY | aY | bY | ΛΛ3.3. X X bbb bbb

It can be observed that, using prod.2, Y It can be observed that, using prod.2, Y generates generates ΛΛ. Y generates a. Y generates b. Y . Y generates a. Y generates b. Y also generates all the combinations of a and also generates all the combinations of a and b. thus Y generates the strings generated by b. thus Y generates the strings generated by (a+b)*. It may also be observed that the (a+b)*. It may also be observed that the above CFG generates the language above CFG generates the language expressed by expressed by (a+b)*bbb(a+b)*(a+b)*bbb(a+b)*. Following . Following are four words generated by given CFGare four words generated by given CFG

Example continued … Example continued …

S S YXY YXY aYbbbaYbbbΛΛ abYbbb abYbbb ababΛΛbbb bbb = abbbb= abbbb

S S YXY YXY

bYbbbaY bYbbbaY

bbΛΛbbbabY bbbabY

bbbbabbY bbbbabbY

bbbbabbaY bbbbabbaY

bbbbabbabbbbabbaΛΛ

= bbbbabba= bbbbabba

S S YXY YXY aYbbbaYbbbΛΛ abYbbb abYbbb ababΛΛbbb bbb = abbbb= abbbb

S S YXY YXY

bYbbbaY bYbbbaY

bbΛΛbbbabbbaΛΛ

= bbbba= bbbba

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

1.1. S S SS | XaXaX | SS | XaXaX | ΛΛ

2.2. X X bX| bX| ΛΛ

It can be observed that, using prod.2, X It can be observed that, using prod.2, X generates generates ΛΛ. X generates any number of . X generates any number of b’s. Thus X generates the strings b’s. Thus X generates the strings generated by b*. It may also be observed generated by b*. It may also be observed that the above CFG generates the that the above CFG generates the language expressed by language expressed by (b*ab*ab*)*(b*ab*ab*)*. .

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.

Thank You…Thank You…