Presentation is loading. Please wait.

Presentation is loading. Please wait.

Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]

Similar presentations


Presentation on theme: "Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]"— Presentation transcript:

1 Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]

2 Context-free Grammars Def: A context-free grammar (CFG) is a 4-tuple G = (V, ,S,P) where - V is a finite set of variables (nonterminal symbols) -  is a finite set of terminal symbols (terminals) - S 2 V is the start symbol - P is the finite set of grammar rules (productions) of the form A  , where A 2 V and  2 (V [  ) * (V and  are disjoint.) For example from previous slide : [Section 6.1]

3 Context-free Grammars A derivation of the string “abaaba” : Def : Let G=(V, ,S,P) be a CFG. For ,  2 (V [  ) * we write  ) G  if there is a production C   in P such that  =  1 C  2 and  =  1  2. We say that  is derived from . If  1, …,  n 2 (V [  ) * and  k )  k+1 for k=1,…,n-1, we write  1 ) G *  n. Language generated by G, denoted L(G), is { x 2  * | ______ } [Section 6.1]

4 Context-free Grammars Examples : { a k b k | k ¸ 0 } { x 2 {a,b} * | x has at least 2 b’s } { x 2 {a,b} * | |x| is even } [Section 6.1]

5 Context-free Grammars Example : S   | abS | baS | aSb | bSa | Sab | Sba What is the language generated by this grammar ? (We’ve seen this before…) [Section 6.1]

6 Context-free Grammars Our G : S   | abS | baS | aSb | bSa | Sab | Sba | SS Recall that we proved (using structural induction) that every string generated by G contains the same number of a’s and b’s. Moreover, we can generate every string with equal number of a’s and b’s by the rules of G. [Section 6.1]

7 Closure Properties of CFL’s Given are two CFG’s G 1 = (V 1, ,S 1,P 1 ) and G 2 = (V 2, ,S 2,P 2 ). - Give a CFG G such that L(G) = L(G 1 ) [ L(G 2 ). - Give a CFG G such that L(G) = L(G 1 )L(G 2 ). - Give a CFG G such that L(G) = L(G 1 ) *. Thus, context-free languages (CFL’s) are closed under union, concatenation, and Kleene’s star. [Section 6.2]

8 Derivation Trees & Ambiguity Consider G:S  S + S | S * S | (S) | x Describe the language generated by G: Give all derivations of x + x * x : [Section 6.4]

9 Derivation Trees We will draw derivation trees of x + x * x : A leftmost derivation is a derivation in which each derivation step uses a rule for the leftmost nonterminal symbol. Derivation trees are in 1-1 correspondence with leftmost derivations. [Section 6.4]

10 Ambiguity A CFG G is ambiguous iff there exists a string x 2 L(G) such that x has more than one distinct derivation trees (or, equivalently, more than one leftmost derivation). Is S  S + S | S * S | (S) | x ambiguous ? Can you give an unambiguous grammar for mathematical expressions ? [Section 6.4]

11 Ambiguity in Programming Languages Consider the following production rule :  if ( ) | if ( ) else | What are the terminals and nonterminals in the above ? How to fix the problem ? [Section 6.4]

12 CFL’s vs. Regular Languages Is every context-free language regular ? Is every regular language context-free ? (Recall the definition of regular languages.) [Section 6.5]

13 Parsing & Normal Forms How to determine if a string is in a language generated by a CFG ? (Algorithms that do this are called parsers.) It helps to have the grammar in a normal form (more restrictions than the original definition of CFG’s). Chomsky normal form : Every rule is of the form A   or A  A 1 A 2 where A,A 1,A 2 2 V and  2 . Example : S  S + S | S * S | (S) | x Convert this grammar into Chomsky normal form. [Section 6.6]

14 Parsing & Normal Forms How to determine if a string is in a language generated by a CFG ? (Algorithms that do this are called parsers.) It helps to have the grammar in a normal form (more restrictions than the original definition of CFG’s). Chomsky normal form : Every rule is of the form A   or A  A 1 A 2 where A,A 1,A 2 2 V and  2 . Example : S  S + S | S * S | (S) | x Convert this grammar into Chomsky normal form. [Section 6.6]

15 Parsing & Normal Forms Greibach normal form : Every rule is of the form A   where  2  and  2 V *. Example : S  S + S | S * S | (S) | x Convert this grammar into Greibach normal form. [Section 6.6]

16 Machines for CFG ? Deterministic or nondeterministic ? Should be able to recognize { a k b k | k ¸ 0 }, { w 2 {a,b}  | w is a palindrome }, but not recognize { ww | w 2 {a,b} * }.


Download ppt "Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]"

Similar presentations


Ads by Google