chapter 3 regular languages and regular grammars

72
1 Chapter 3 Chapter 3 Regular Languages and Regular Languages and Regular Grammars Regular Grammars

Upload: laurel-morales

Post on 03-Jan-2016

54 views

Category:

Documents


7 download

DESCRIPTION

Chapter 3 Regular Languages and Regular Grammars. 3.1: Regular Expressions (1). Regular Expression (RE): E is a regular expression over  if E is one of: e a , where a   If r and s are regular expressions (REs) , then the following expressions also regular: r | s  ( r or s ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 3 Regular Languages and Regular Grammars

1

Chapter 3Chapter 3

Regular Languages andRegular Languages and

Regular GrammarsRegular Grammars

Page 2: Chapter 3 Regular Languages and Regular Grammars

2

3.1: Regular Expressions (1)3.1: Regular Expressions (1)

Regular Expression (RE): E is a regular expression over if E is one of:

a, where a If r and s are regular expressions (REs), then

the following expressions also regular: r | s (r or s) rs (r followed by s) r* (r repeated zero or more times)

Each RE has an equivalent regular language (RL)

Page 3: Chapter 3 Regular Languages and Regular Grammars

3

3.1: Regular Expressions (2)3.1: Regular Expressions (2)

Regular Language (RL): L is a regular language over if L is one of:

empty set {} a set that contains empty

string a} where a

If R and S are regular languages (RL), then the following languages also regular: R S = {w | w R or w S} RS = {rs | r R and s S} R* = R0 R1 R2 R3 …

Page 4: Chapter 3 Regular Languages and Regular Grammars

4

Rules for Specifying Regular Expressions:Rules for Specifying Regular Expressions:

1. is a regular expression L =

2. If a is in , a is a regular expression L = {a}, the set containing the string a.

3. Let r and s be regular expressions with languages L(r) and L(s). Then

a. r | s is a RE L(r) L(s)

b. rs is a RE L(r) L(s)

c. r* is a RE (L(r))*

d. (r) is a RE L(r), extra parenthesis

precedence

3.1: Regular Expressions (3)3.1: Regular Expressions (3)

Page 5: Chapter 3 Regular Languages and Regular Grammars

5

3.1: Regular Expressions (4)3.1: Regular Expressions (4)

Examples:Examples: {0, 1}{00, 11} = {000, 011, 100, 111} {0} = {, 0, 00, 000, 0000, …} 11}* = {, 10, 1010, 101010, …,

01, 0101, 010101, …, 1001, 100101, 10010101, …, 0110, 011010, 01101010,

…} Notational shorthand:Notational shorthand:

L0 = Li = LLi-1

L+ = LL*

Page 6: Chapter 3 Regular Languages and Regular Grammars

6

3.1: Regular Expressions (5)3.1: Regular Expressions (5)

Let Let LL be a language over {a, b}, each string in be a language over {a, b}, each string in LL contains the substring contains the substring bbbb L = {a, b}*{bb}{a, b}*

L is regular language (RL). Why?L is regular language (RL). Why? {a} and {b} are RLs {a, b} is RL {a, b}* is RL {b}{b} = {bb} is also RL Then L = {a, b}*{bb}{a, b}* is RL

Page 7: Chapter 3 Regular Languages and Regular Grammars

7

3.1: Regular Expressions (6)3.1: Regular Expressions (6)

Let L be a language over {a, b}, each string in L begins and ends with an a contains at least one b L = {a}{a, b}*{b}{a, b}*{a} L = a*b*a

L is regular language (RL). Why? {a} and {b} are RLs {a, b}* is RL Then L = {a}{a, b}*{b}{a, b}*{a} is RL

Page 8: Chapter 3 Regular Languages and Regular Grammars

8

3.1: Regular Expressions (7)3.1: Regular Expressions (7)

L = {a, b}*{bb}{a, b}* RE = (a|b)*bb(a|b)*

L = {a}{a, b}*{b}{a, b}*{a} RE = a(a|b)*b(a|b)*a

This RE = (a)|((b)*(c)) is equivalent to a|b*c

We say REs r and s are equivalent (r=s), iff r and s represent the same language Example: r = a|b, s = b|a r = s Why? Since L(r) = L(s) = {a, b}

Page 9: Chapter 3 Regular Languages and Regular Grammars

9

3.1: Regular Expressions (8)3.1: Regular Expressions (8)

Let Let = {a, b}= {a, b}

RE a|b L = {a, b} RE (a|b)(a|b) L = {aa, ab, ba, bb} RE aa|ab|ba|bb same as above RE a* L = {, a , aa, aaa, …} RE (a|b)* L = set of all strings of

a’s and b’s including

RE (a*b*)* same as above RE a|a*b L = {a,b,ab,aab,aaab, …}

Page 10: Chapter 3 Regular Languages and Regular Grammars

10

3.1: Regular Expressions (9)3.1: Regular Expressions (9)

Algebraic Properties of regular ExpressionsAlgebraic Properties of regular Expressions

AXIOM

r | s = s | r

r | (s | t) = (r | s) | t

(r s) t = r (s t)

r = r = r

r* = (r*)* = ( r | )+ = r+ |

r ( s | t ) = r s | r t( s | t ) r = s r | t r

r** = r*

r+ = r r*

Page 11: Chapter 3 Regular Languages and Regular Grammars

11

3.1: Regular Expressions (10)3.1: Regular Expressions (10)

abcabc concatenation (“followed by”)

a | b | ca | b | c alternation (“or”)

** zero or more occurrences

++ one or more occurrences

Page 12: Chapter 3 Regular Languages and Regular Grammars

12

3.1: Regular Expressions (11)3.1: Regular Expressions (11)

All strings of 1s and 0sAll strings of 1s and 0s(0 | 1)*

All strings of 1s and 0s beginning with a 1All strings of 1s and 0s beginning with a 11 (0 | 1)*

Page 13: Chapter 3 Regular Languages and Regular Grammars

13

3.1: Regular Expressions (12)3.1: Regular Expressions (12)

All strings containing two or more All strings containing two or more 00s s (1|0)*0(1|0)*0(1|0)*

All strings containing an even number of All strings containing an even number of 00s s (1*01*01*)* | 1*

Page 14: Chapter 3 Regular Languages and Regular Grammars

14

3.1: Regular Expressions (13)3.1: Regular Expressions (13)

All strings containing an All strings containing an eveneven number of number of 00s and s and even even number of number of 11ss( 0 0 | 1 1 )*(( 0 1 | 1 0 )( 0 0 | 1 1 )*( 0 1 | 1 0 )( 0 0 | 1 1 )*)*

All strings of alternating All strings of alternating 00s and s and 11ss( | 1 ) ( 0 1 )* ( | 0 )

Page 15: Chapter 3 Regular Languages and Regular Grammars

15

3.1: Regular Expressions (14)3.1: Regular Expressions (14) Strings over the alphabet {0, 1} with Strings over the alphabet {0, 1} with nono

consecutive 0'sconsecutive 0's (1 | 01 )* (0 | ) 1*(01+)* (0 | ) 1*(011*)* (0 | )

Strings over the alphabet {a, b} with Strings over the alphabet {a, b} with exactly three exactly three b'sb's a*ba*ba*ba*

Strings over the alphabet {a, b, c} containing (Strings over the alphabet {a, b, c} containing (at at least onceleast once) ) bcbc (a|b|c)*bc(a|b|c)*

Page 16: Chapter 3 Regular Languages and Regular Grammars

16

3.1: Regular Expressions (15)3.1: Regular Expressions (15)

Strings over the alphabet {a, b} in which Strings over the alphabet {a, b} in which substrings substrings abab and and baba occur an unequal number of occur an unequal number of timestimes (a+b+)+ | (b+a+)+

Page 17: Chapter 3 Regular Languages and Regular Grammars

17

3.1: Regular Expressions (16)3.1: Regular Expressions (16) Describe the following in English:Describe the following in English:

(0|1)* all strings over {0, 1}

b*ab*ab*ab* all strings over {a, b} with exactly 3 a’s

Page 18: Chapter 3 Regular Languages and Regular Grammars

18

3.1: Regular Expressions (17)3.1: Regular Expressions (17)

Examples of RE:Examples of RE:

01* {0, 01, 011, 0111, …..}

(01*)(01) {001, 0101, 01101, 011101, …..}

(0 | 1)* = {0, 1, 00, 01, 10, 11, …..} i.e., all strings of 0 and 1

(0 | 1)* 00 (0 | 1)* = {00, 1001, …..} i.e., all 0 and 1 strings containing a “00”

Page 19: Chapter 3 Regular Languages and Regular Grammars

19

3.1: Regular Expressions (18)3.1: Regular Expressions (18)

More Examples of RE:More Examples of RE:

(1 | 10)* all strings starting with “1” and containing no “00”

(0 | 1)*011 all strings ending with “011”

0*1* all strings with no “0” after “1”

00*11* all strings with at least one “0” and one “1”, and no “0” after

“1”

Page 20: Chapter 3 Regular Languages and Regular Grammars

20

3.1: Regular Expressions (19)3.1: Regular Expressions (19)

What languages do the following RE represent?What languages do the following RE represent?

(1 | 01 | 001)*( | 0 | 00)

((0 | 1)(0 | 1))* | ((0 | 1)(0 | 1)(0 | 1))*

Page 21: Chapter 3 Regular Languages and Regular Grammars

21

3.1: Regular Expressions (20)3.1: Regular Expressions (20)

Home Study:Home Study:

Construct a RE over ={0,1} such that It does not contain any string with two consecutive “0”s It has no prefix with two or more “0”s than “1” nor two or

more “1”s than “0” The set of all strings ending with “00” The set of all strings with 3 consecutive 0’s The set of all strings beginning with “1”, which when

interpreted as a binary no., is divisible by 5 The set of all strings with a “1” at the 5th position from the

right The set of all strings not containing 101 as a sub-string

Page 22: Chapter 3 Regular Languages and Regular Grammars

22

3.1: Regular Expressions (21)3.1: Regular Expressions (21)

Strings over the alphabet {Strings over the alphabet {((, , ))} where the } where the parentheses are balancedparentheses are balanced Not regular  

L = {aL = {annbbnn | | nn 0} 0} L = ab, aabb, aaabbb, aaaabbbb, …} Not regular  

Page 23: Chapter 3 Regular Languages and Regular Grammars

23

3.2:3.2: Connection Between RE & RL (1)Connection Between RE & RL (1)

A language A language LL is called is called regularregular if and only if there if and only if there exists some DFA M such that L = L(M).exists some DFA M such that L = L(M).

Since a DFA has an equivalent NFA, then Since a DFA has an equivalent NFA, then A language L is called regular if and only if

there exists some NFA N such that L = L(N).

If we have a RE If we have a RE rr, we can construct an NFA that , we can construct an NFA that accept L(accept L(rr).).

Page 24: Chapter 3 Regular Languages and Regular Grammars

24

3.2:3.2: Connection Between RE & RL (2)Connection Between RE & RL (2)

2. For a in the regular expression, construct NFA

astartL = {a}

1. For in the regular expression, construct NFA

startL = {}

0. For in the regular expression, construct NFA

start L = { } =

Page 25: Chapter 3 Regular Languages and Regular Grammars

25

3.2:3.2: Connection Between RE & RL (3)Connection Between RE & RL (3)

where i and f are new start / final states, and -moves are introduced from i to the old start states of Ms and Mt as well as from all of their final states to f.

3.(a) If s and t are regular expressions, Ms and Mt are their NFAs. s|t has NFA:

start i f

Ms

Mt

L = {L(Ms) L(Mt)}

Page 26: Chapter 3 Regular Languages and Regular Grammars

26

3.2:3.2: Connection Between RE & RL (4)Connection Between RE & RL (4)

3.(b) If s and t are regular expressions, Ms, Mt their NFAs. st (concatenation) has NFA:

starti fMs Mt

Alternative:

overlap

Msstart i fMt

where i is the start state of Ms (or new under the alternative) and f is the final state of Mt (or new). Overlap maps final states of Ms to start state of Mt

L = {L(Ms)L(Mt)}

Page 27: Chapter 3 Regular Languages and Regular Grammars

27

3.2:3.2: Connection Between RE & RL (5)Connection Between RE & RL (5)

fMsstart i

where : i is new start state and f is new final state

-move i to f (to accept null string)

-moves i to old start, old final(s) to f

-move old final to old start (WHY?)

3.(c) If s is a regular expressions and Ms its NFA,

s* (Kleene star) has NFA:L = {L(Ms)*}

Page 28: Chapter 3 Regular Languages and Regular Grammars

28

3.2:3.2: Connection Between RE & RL (6)Connection Between RE & RL (6)

Build an Build an NFA-NFA- that accepts that accepts (a|b)(a|b)**babaaa

bb

baba

a|ba|b

astart

abstart q1

bstart

a

b

start

Page 29: Chapter 3 Regular Languages and Regular Grammars

29

3.2:3.2: Connection Between RE & RL (7)Connection Between RE & RL (7)

Build an Build an NFA-NFA- that accepts that accepts (a|b)(a|b)**baba

(a|b)(a|b)**a

b

Page 30: Chapter 3 Regular Languages and Regular Grammars

30

3.2:3.2: Connection Between RE & RL (8)Connection Between RE & RL (8)

Build an Build an NFA-NFA- that accepts that accepts (a|b)(a|b)**baba

ab

a

b

Page 31: Chapter 3 Regular Languages and Regular Grammars

31

3.2:3.2: Connection Between RE & RL (9)Connection Between RE & RL (9)

r13

r12r5

r3 r11r4

r9

r10

r8r7

r6

r0

r1 r2

b

*c

a a

|

( )

b

|

*

c

(ab*c) | (a(b|c*))Decomposition for this regular expression:

What is the NFA? Let’s construct it !

Page 32: Chapter 3 Regular Languages and Regular Grammars

32

3.2:3.2: Connection Between RE & RL (10)Connection Between RE & RL (10)

r3: a

r0: b

r2: c

b

r1:

r4 : r1 r2b

c

r5 : r3 r4

b

a c

Page 33: Chapter 3 Regular Languages and Regular Grammars

33

3.2:3.2: Connection Between RE & RL (11)Connection Between RE & RL (11)

r11: a

r7: b

r6: c

c

r9 : r7 | r8

b

r10 : r9

c

r8:

c

r12 : r11 r10

b

a

Page 34: Chapter 3 Regular Languages and Regular Grammars

34

3.2:3.2: Connection Between RE & RL (12)Connection Between RE & RL (12)

r13 : r5 | r12

b

a c

c

b

a

1

6543

8

2

10

9 12 13 14

11

15

7

16

17

Page 35: Chapter 3 Regular Languages and Regular Grammars

35

3.2:3.2: Connection Between RE & RL (13)Connection Between RE & RL (13)

Let’s try Let’s try aa ( ( bb | | cc ) )**

1. 1. aa, , bb, & , & cc

2. 2. bb | | cc

3. ( 3. ( bb | | cc ) )**

S0 S1 a

S0 S1 b

S0 S1 c

S1 S2 b

S3 S4 c

S0 S5

S2 S3 b

S4 S5 c

S1 S6 S0 S7

Page 36: Chapter 3 Regular Languages and Regular Grammars

36

3.2:3.2: Connection Between RE & RL (14)Connection Between RE & RL (14)

4. 4. aa ( ( bb | | cc ) )**

S0 S1 a

S4 S5 b

S6 S7 c

S3 S8 S2 S9

S0 S1 a

b | c

Page 37: Chapter 3 Regular Languages and Regular Grammars

37

3.2:3.2: Connection Between RE & RL (15)Connection Between RE & RL (15)

Let : a

abb a*b+

3 patterns

NFA’s :

start

start

start

1

b

b

bb

a

a

a

2

3 4 5

87

6

Page 38: Chapter 3 Regular Languages and Regular Grammars

38

3.2:3.2: Connection Between RE & RL (16)Connection Between RE & RL (16)

NFA for : a | abb | a*b+

0

b

b

bb

a

a

a

2

3 4 5

87

6

1

start

Page 39: Chapter 3 Regular Languages and Regular Grammars

39

Regular Expression to NFA-Regular Expression to NFA-

(a | ba)*a

Page 40: Chapter 3 Regular Languages and Regular Grammars

40

First Parsing StepFirst Parsing Step

concatenate

(a|ba)* a

Page 41: Chapter 3 Regular Languages and Regular Grammars

41

Second Parsing StepSecond Parsing Step

concatenate

* a

a|ba

Page 42: Chapter 3 Regular Languages and Regular Grammars

42

Third Parsing StepThird Parsing Step

concatenate

* a

|

a ba

Page 43: Chapter 3 Regular Languages and Regular Grammars

43

Fourth Parsing StepFourth Parsing Step

concatenate

* a

|

a concatenate

b a

Page 44: Chapter 3 Regular Languages and Regular Grammars

44

Identify Leaf NodesIdentify Leaf Nodes

concatenate

+ a

|

a concatenate

b a

Page 45: Chapter 3 Regular Languages and Regular Grammars

45

Convert Leaf NodesConvert Leaf Nodes

concatenate

*

|

concatenate

a

a

a

b

Page 46: Chapter 3 Regular Languages and Regular Grammars

46

Identify Convertible Node(s)Identify Convertible Node(s)

concatenate

*

|

concatenate

a

a

a

b

Page 47: Chapter 3 Regular Languages and Regular Grammars

47

Convert NodeConvert Node

concatenate

*

|

a

a ab

Page 48: Chapter 3 Regular Languages and Regular Grammars

48

Identify Convertible NodeIdentify Convertible Node

concatenate

*

|

a

a ab

Page 49: Chapter 3 Regular Languages and Regular Grammars

49

Convert NodeConvert Node

concatenate

* a

a

ab

Page 50: Chapter 3 Regular Languages and Regular Grammars

50

Identify Convertible NodeIdentify Convertible Node

concatenate

* a

a

ab

Page 51: Chapter 3 Regular Languages and Regular Grammars

51

Convert NodeConvert Node

concatenate

a

a

ab

Page 52: Chapter 3 Regular Languages and Regular Grammars

52

Identify Convertible NodeIdentify Convertible Node

concatenate

a

a

ab

Page 53: Chapter 3 Regular Languages and Regular Grammars

53

Convert Final NodeConvert Final Node

a

a

ab

Page 54: Chapter 3 Regular Languages and Regular Grammars

54

3.2:3.2: Expression Graphs (1)Expression Graphs (1)

NFA to RENFA to RE

If L is accepted by some NFA-, then L is represented by some regular expression

An expression graph is like a state diagram but it can have regular expressions as labels on arcs

An NFA- is an expression graph An expression graph can be reduced to one with

just two states If we reduce an NFA- in this way, the arc label

then corresponds to the regular expression representing it

Page 55: Chapter 3 Regular Languages and Regular Grammars

55

3.2:3.2: Expression Graphs (2)Expression Graphs (2)

wik kwjij i

wji wik kj

wik kwjij i

wji (wii)* wik

kjwii

w

w*start

w1

(w1)*w2(w3 w4(w1)*w2)*start w2

w3

w4

Page 56: Chapter 3 Regular Languages and Regular Grammars

56

3.2:3.2: Expression Graphs (3)Expression Graphs (3)

Merge Edges :

c

b

a

a | b | c

p

ac*b

ba c

Replace state by Edges

q0 q1

q0q1

Page 57: Chapter 3 Regular Languages and Regular Grammars

57

3.2:3.2: Expression Graphs (4)Expression Graphs (4)

Let Let GG be the state diagram of a finite automata be the state diagram of a finite automata Let Let mm be the number of final states of be the number of final states of GG Make Make mm copies of copies of GG, each of which has one final , each of which has one final

state.state. Call these graphsCall these graphs G G11, , GG22, …, , …, GGmm For each For each GGtt

Repeat Do the steps in the previous slide

Until the only states in Gt are the start state and the single final state

Determine the RE of Gt The RE of The RE of GG is obtained by joining RE’s of each is obtained by joining RE’s of each

GGtt by by oror

Page 58: Chapter 3 Regular Languages and Regular Grammars

58

3.2:3.2: Expression Graphs (5)Expression Graphs (5)

c3

c1 2

bG: start

b

c3

c1 2

bG1: start

b

c3

c1 2

bG2: start

b

Page 59: Chapter 3 Regular Languages and Regular Grammars

59

3.2:3.2: Expression Graphs (6)Expression Graphs (6)

c3

c1 2

bG1: start

b

3cc

1

b b

1

b

b*

Page 60: Chapter 3 Regular Languages and Regular Grammars

60

3.2:3.2: Expression Graphs (7)Expression Graphs (7)

c3

c1 2

bG2: start

b

3cc

1

b b

b*ccb*

RE for G b* | b*ccb*

Page 61: Chapter 3 Regular Languages and Regular Grammars

61

3.3 Regular Grammars (1)

A grammar is said to be right-linear if all production rules are of the form: A xB A x Where A, B NT, and x T*

A grammar is said to be left-linear if all production rules are of the form: A Bx A x Where A, B NT, and x T*

A regular grammar is one that either right-linear or left-linear, but not both

Page 62: Chapter 3 Regular Languages and Regular Grammars

62

3.3 Regular Grammars (2)

The grammars G1 is right-linear G1=({S}, {a, b}, S, {S abS | a} L(G1) = {{ab}*a} r = (ab)*a

The grammars G2 is left-linear G2=({S, A, B}, {a, b}, S, { S Aab

A Aab |

B a } L(G2) = {a{ab}+} r = a(ab)+

Both grammars G1 and G2 are regular

Page 63: Chapter 3 Regular Languages and Regular Grammars

63

3.3 Regular Grammars (3)

The grammars G is not regular G=({S, A, B}, {a, b}, S, { S A

A aB |

B Ab }

Every production rule is either in right-linear or left-linear from

The grammar itself is neither right-linear nor left-linear

Page 64: Chapter 3 Regular Languages and Regular Grammars

64

3.3 Regular Grammars (4)

A language is said to be regular if it is generated by some regular grammar.

A regular language may be generated by both regular and non-regular grammars.

Derivations in regular grammars (right-linear) have a nice form There is at most one non-terminal present in a

sentential form and that non-terminal, if present, is the rightmost symbol in the string.

ab...cD ab...cdE D dE

Page 65: Chapter 3 Regular Languages and Regular Grammars

65

3.3 Regular Grammars (5)

The grammars G1 & G2 generate the language a+b*

The grammar G1 is not a regular, because of the rule S AB does not have the specified form

G2 is a regular grammar (right-linear)

G1=({S,A,B}, {a,b}, S, {S AB A aA | aB bB | }

G2=({S,B}, {a,b}, S, {S aS | aB B bB | }

Page 66: Chapter 3 Regular Languages and Regular Grammars

66

3.3 Regular Grammars (6)

The following grammars are regular grammars (right-linear)

G=({S,O}, {a,b}, S, {S aO | bO | O aS | bS }

G=({S,B}, {a,b}, S, {S aS | bB | B aB | bS}

G=({S,B,C}, {a,b,c}, S, {S bS | cS | aB | B aB | cS |

bC | C aB | bS | }

Page 67: Chapter 3 Regular Languages and Regular Grammars

67

3.3 Regular Grammars (7)

Non-regular grammar S abSA |

A Aa |

Its equivalent regular grammar S aB |

B bS | bA A aA |

Both grammars generate the language | (ab)+a*

Page 68: Chapter 3 Regular Languages and Regular Grammars

68

3.3 Regular Grammars (8)

The language a+b+ is generated by the grammar G G: S aS | aA

A bA | b And also accepted by NFA M

Derivation of aabb using CFG G S aS aaA aabA aabb

Accepting aabb using NFA M [S, aabb] ├ [S, abb] ├ [A, bb] ├ [A, b] ├ [Z, ]

S A Zba

a b

Page 69: Chapter 3 Regular Languages and Regular Grammars

69

3.3 Regular Grammars (9)

The NFA M = (Q, , , S, F) can be constructed from the rules of a grammar G = (NT, , P, S) as follows: Q = NT {Z}, where Z NT if A a P

Q = NT otherwise

(A, a) = B whenever A aB P(A, a) = Z whenever A a P

F = {A | A P} {Z} if Z QF = {A | A P} otherwise

Page 70: Chapter 3 Regular Languages and Regular Grammars

70

3.3 Regular Grammars (10)

The the grammar G generates and the NFA M accepts the language a*(a|b+) G: S aS | bB | a

B bB |

M

S B

Za

b

a b

Page 71: Chapter 3 Regular Languages and Regular Grammars

71

3.3 Regular Grammars (11)

Given the following grammar G that generates the language over {a, b, c} that do not contain the substring abc G: S bS | cS | aB |

B aB | cS | bC | C aB | bS |

What is NFA M that accepts L(G)?

S B Cba

b, c a

ac

b

Page 72: Chapter 3 Regular Languages and Regular Grammars

72

3.3 Regular Grammars (12)

The DFA M that accepts the language over {a, b} with even number of a’s and odd number of b’s is given

Give grammar G for M G: S aA | bB

A aS | bCB bS | aC | C aB | bA

S B

a

A Cb

a aa

b

b

b