, , }Grammar A grammar G is defined as quadruple G =(V,7,S,P) ve + T--- Finite and non-empty set of terminal symbols + §--- Start variable, S eV + P --- Finite set of Production rules inite and non-empty set of variables (non-terminals) Note: + Vand T are disjoint sets Note: + Every production rule must contain at least one non-terminal on its left side. + Form of production rule x— ey xe (VUT)*V(VUT)" ye(VUT)* * Production rules- — Specify how to generate a string. — Specify how the grammar transforms one sentential form into another sentential form/string. — Define the language associated with grammar.Production Rules Production rule Pi: xX— > y Ww =uxy, Z=uyv, 5x, y, u, ve(T U V)* Production rule P, is applicable to string w — x can be replaced with y, to form a new string This can be written as: w — z w derives zGrammar-Example Grammar: G What strings/sentences can be generated with this grammar? t T,S, P) (What is the language generated by WN this grammar ?) VE{S} T={a,b} P={SaSb, SA} Sentential Form: * A finite sequence of variables and terminals. Sentence/String: * A finite sequence of terminals. Derivation of string: S=>aSb=> aasbb => aaaSbbb => aaabbb to. Sentential Fornis Sentence/StringMore Notation We write: S => aaabbb Instead of |§ => aSb => aaSbb => aaaSbbb => aaabbb The * indicates an unspecified number of steps (Zero or more steps). if wow >w3>:->MW, Then, we canwrite w, => w, By default: w>wExample Derivations Grammar S — aSb * a S>A SrA 2 S=>ab * S—=>aabb Derivation 1 * s—>aaSbh S => aaabbb aaSbb=> aaaaaSbbbbbAnother Grammar Example Grammar G: S —> Ab A—aAb A>A Derivations: S=Ab=>b S=Ab=>aAbb=>abb S = Ab=>aAbb=>aaAbbb=>aabbb Derivations: §' => Ab => aAbb => aaAbbb => aaaAbbbb => aaaaAbbbbb => aaaabbbbb A> aAb > aaAbb > aabb ???Language of a Grammar + Let G=(V, T, S, P) be a grammar. The language generated by G, denoted by L(G), is the set of all strings that can be derived from the starting variable S. L(G)= {w e T* | S=>*w} That is, L(G) is simply the set of strings of terminals that can be derived from the start symbol. + Example- G=(V,T,S,P); V=iA, St, T={a, b}, Sis a start symbol P={S— aA, S— b, A— aa}. T* = {e, a, b, aa, bb, ab, ba, aaa, aab, aba, abb, baa, bab,...... } + The language of this grammar is given by L (G) = {b, aaa} 1. we can derive aA from S using S > aA, and then derive aaa using A > aa, 2. Wecan also derive b using S > b.Language of a Given Grammar: How to prove ? * L-Agiven language * L(G) — Language of a grammar * Every string generated by Gis in Li.e., L(G) & L. * Every string in L can be generated by Gi.e., L & L(G). G=(V,T,S, P) Vv ={S} S — aSb T = {a, b} SrA P={S>aSb]A} L={a"b” :n=0O}Language of a Given Grammar S > aSb What’s the language of the grammar G ? SoA S => aSb => aaSbb => aaaSbbb => aaabbb S => aSb = aaSbb = aaaSbbb => aaaaSbbbb = aaaabbbb L={a"b” :n=0O} Find languages of the following grammars- © Gl: S> aaA|A, A DbS —~ L={(aaby": n>=0} * G28 Aa, A> B, BD Aa~L={} + G3:S> a|bjaSa| bSb|% —-L= { wis in {a, b}* : wis palindrome}Equivalent Grammars Two grammars G, and G, are equivalent if they generate the same language, that is, if L(G,) = L(G,) First grammar G, S — aSb L(G1) = {a"b" : n >= 0} SrA Second grammarG, § —aAb | A L(G2) = {a"b" : n >= 0} A> adblA The language generated by both grammarsis ZL ={a"b”" :n=0} Grammars G, and G, are equivalent.Grammar for a given Language Procedure- = Write grammar rules to generate all strings in the language. Write production rules that generate the strings in the language. 1. L={we {a}*}={e, a, aa, aaa, aaa, } seas 2. L= {we {a, b}*} = (e, a, b, aa, ab, ba, bb, ...... } S>e|aS| bs 3. L={we {a, b}*: w has exactly one a} = {a, ab, ba, abb, bab, bba S>Xax X>bX Je 4. L={w € {a, b}*: w has at least one a} = {a, aab, bab, aa, aaa, .....aaaabbaaa.... } S > Xax,X > aX | bX ]e 5. L={w € {a, b}*: w has no more than three a’s} = {e, a, b, aab, ab, bba, babab, bbbb,... } Se] X | Xax | XaXax | XaXaXax, XD bX | e 6. L={w e {a, b}*: n,(w) = ny(w)} = {e, ab, aabb, ba, aabaabbb, aabbab, Se | aSb | bSa| SS Thank You