1 / 16

Understanding Context-free Grammars: Rules and Language Generation

Learn about context-free grammars (CFGs) and how they generate languages through production rules. See examples and understand how to work with CFGs.

trudyl
Download Presentation

Understanding Context-free Grammars: Rules and Language Generation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. [Section 6.1] 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 :

  3. [Section 6.1] 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  = 1C2 and  = 12. We say that  is derived from . If 1, …, n2 (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* | ______ }

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

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

  6. [Section 6.1] 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.

  7. [Section 6.2] Closure Properties of CFL’s • Given are two CFG’s G1 = (V1,,S1,P1) and G2 = (V2,,S2,P2). • Give a CFG G such that L(G) = L(G1) [ L(G2). • Give a CFG G such that L(G) = L(G1)L(G2). • Give a CFG G such that L(G) = L(G1)*. • Thus, context-free languages (CFL’s) are closed under union, concatenation, and Kleene’s star.

  8. [Section 6.4] 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 :

  9. [Section 6.4] 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.

  10. [Section 6.4] 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 ?

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

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

  13. [Section 6.6] 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  A1A2 where A,A1,A22 V and 2. Example : S  S + S | S * S | (S) | x Convert this grammar into Chomsky normal form.

  14. [Section 6.6] 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  A1A2 where A,A1,A22 V and 2. Example : S  S + S | S * S | (S) | x Convert this grammar into Chomsky normal form.

  15. [Section 6.6] 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.

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

More Related