Lec4

Post on 29-Jun-2015

125 Views

Category:

Education

5 Downloads

Preview:

Click to see full reader

DESCRIPTION

Theory of computation

Transcript

Theory of Computation

Lecture 4: Context Free Grammar (CFG)

Cairo UniversityFCI

Dr. Hussien SharafComputer Science Departmentdr.sharaf@from-masr.com

2

p.434: Regular Languages and non-regular languages

Dr. Hussien M. Sharaf

Language Defined by

Corresponding Accepting Machine

Nondeter-minism=Determinism

Regular expression Finite automaton, transition graph

Yes

Context-free grammar

Pushdown automaton No

Type 0 grammar Turing machine, Post machine, Pushdown automaton

Yes

3

CFG A context-free grammar is a notation for

defining context free languages.It is more powerful than finite automata or

RE’s, but still cannot define all possible languages.

Useful for nested structures, e.g., parentheses in programming languages.

Basic idea is to use “variables” to stand for sets of strings.

These variables are defined recursively, in terms of one another.

Dr. Hussien M. Sharaf

4

CFG formal definitionC =(V, Σ, R, S)V: is a finite set of variables.Σ: symbols called terminals of the alphabet of

the language being defined.S V: a special start symbol.R: is a finite set of production rules of the

form A→ where A V, (V Σ)

Dr. Hussien M. Sharaf

5

CFG -1Define the language { anbn | n > 1}.Terminals = {a, b}.Variables = {S}.Start symbol = S.Productions ={

S → ab,S → aSb }Summary

S → abS → aSb

Dr. Hussien M. Sharaf

6

DerivationWe derive strings in the language of a CFG by

starting with the start symbol, and repeatedly replacing some variable A by the right side of one of its productions.

Derivation example for “aabb”Using S→ aSb

generates uncompleted string that still has a non- terminal S.

Then using S→ ab to replace the inner SGenerates “aabb”

S aSb aabb ……[Successful derivation of aabb]

Dr. Hussien M. Sharaf

7

Balanced-parenthesesProd1 S → (S)Prod2 S → ()

Derive the string ((())).S → (S) …..[by prod1]

→ ((S)) …..[by prod1]→ ((())) …..[by prod2]

Dr. Hussien M. Sharaf

8

PalindromeDescribe palindrome of a’s and b’s using CGF1] S → aS 2] S → bS3] S → b 4] S → a

Derive “baab” from the above grammar.S → bS [by 2]

→ baS [by 1]→ baaS [by 1]→ baab [by 3]

Dr. Hussien M. Sharaf

CFG – 2.1Describe anything (a+b)* using CGF1] S → Λ 2] S → Y 3] Y→ aY4] Y → bY 5] Y →a 6] Y→ b

Derive “aab” from the above grammar.S → aY [by 3]

Y → aaY [by 3]Y → aab [by 6]

Dr. Hussien M. Sharaf 9

CFG – 2.21] S → Λ 2] S → Y 3] Y→ aY4] Y → bY 5] Y →a 6] Y→ b

Derive “aa” from the above grammar.S → aY [by 3]

Y → aa [by 5]

Dr. Hussien M. Sharaf 10

11

Remember CFG is about categorizing the grammar of a

language

Dr. Hussien M. Sharaf

12

CFG – 3Describe anything (a+b)* using CGF1] S →aS 2] S → bS 3] S →ΛDerive “aab” from the above grammar.S → aS [by 1]

S → aaS [by 1]S → aabS [by 2]S → aabΛ [by 3] → aab

Dr. Hussien M. Sharaf

13

CFG – 4Describe anything (a+b)*aa(a+b)* using CGF1] S →XaaX 2] X → aX 3] X → bX 4] X →ΛNote: rules 2, 3, 4 represents anything (a+b)*Derive “baabaab” from the above grammar.S → XaaX [by 1] X→ bXaaX [by 3]X→ bΛaaX [by 4] X → baabX [by 3]X → baabaX [by 2] X→ baabaaX [by 2]X→ baabaabX [by 3] X→ baabaabΛ[by 4]

Dr. Hussien M. Sharaf

Anything aa Anything

14

CFG – 5Describe a language that has even number of a’s

and even number of b’s using CGF.i.e. aababbab

1] S →SS 2] S →BALANCED S3] S →S BALANCED 4] S →Λ5] S →UNBALANCED S UNBALANCED6] BALANCED → aa 7] BALANCED → bb8] UNBALANCED → ab9] UNBALANCED → ba

Dr. Hussien M. Sharaf

Even-Even grammar

15

CFG – 5Derive “aababbab” from the above grammar.S → BALANCED S [by 1]BALANCED→ aa UNBALANCED S UNBALANCED

[by 5]UNBALANCED → aa ba S UNBALANCED [by 9]UNBALANCED → aa ba S ab [by 8]S → aa ba BALANCED S ab [by 3]UNBALANCED → aa ba bb S ab [by 7]S → aa ba bb ab [by 4]

→ aababbab

Dr. Hussien M. Sharaf

Even-Even grammar

16

CFG – 6i.e. {Λ, ab, aaabbb,… }S → aSb|Λ Derive “aaaabbbb”S →aSb→aSb→aaSbb →aaaSbbb

→aaaaSbbbb →aaaabbbb

Dr. Hussien M. Sharaf

Balanced a-b grammar

17

CFG – 7i.e. {Λ, ab, abbaabba,… }S → aSa| bSb| Λ Derive “abbaabba” “abba abba”S →aSa→abSba→abbSbba →abbaSabba→ abba abba→ abbaabba

Dr. Hussien M. Sharaf

Even-Plaindrome grammar

18

CFG – 8i.e. {Λ, abb, ababa,… }S → aSa| bSb| a| b| Λ Derive “ababa” “ab a ba”S →aSa→abSba→abSba →abSba→ ababa

Note: anban can be represented by S → aSa| bBut anban bn+1 can not be represented by CFG !

Dr. Hussien M. Sharaf

Plaindrome grammar

i.e. {Λ, ab, abbaabba,… }S → aSa| bSb| Λ Derive abaaba

19

Even-Plaindrome grammar

S

S

Λ

aa

S bb

S aa

Dr. Hussien M. Sharaf

CFG – 9

Deduce CFG of addition and parse the following expression 2+3+5

1] S→S+S|N2] N→1|2|3|4|5|6|7|8|9|0 N1|N2|N3|N4|N5|N6|N7|N8|N9|N0

20

S

S+N

S+

N

5S

+

3

N

2

N

Can u makeanother parsingtree ?

Dr. Hussien M. Sharaf

CFG – 10

CFG – 11.1Deduce CFG of a addition/multiplication and

parse the following expression 2+3*5

1] S→S+S|S*S|N

2] N→1|2|3|4|5|6|7|8|9|0 N1|N2|N3|N4|N5|N6|N7|N8|N9|N0

21

S

S*S

S*

N

5S+

3

N

2

N

Can u makeanother parsingtree ?

Dr. Hussien M. Sharaf

CFG -11.2 without ambiguityDeduce CFG of a addition/multiplication and

parse the following expression 2*3+51] S→ Term|Term + S 2] Term → N|N * Term 3] N→1|2|3|4|5|6|7|8|9|0

N1|N2|N3|N4|N5|N6|N7|N8|N9|N0

22

S

S+N

S+

N

5S

*

3

N

2

N

Can u makeanother parsingtree ?

Dr. Hussien M. Sharaf

23

Thank you

Dr. Hussien M. Sharaf

top related