1 / 47

Context-Free Grammars

CSC 4170/CSC 8510 Theory of Computation. Context-Free Grammars. Chapter 2. 2.1.a. What is a CFG. A  B A   B  0A1. Terminals: 0,1. Variables: A,B. Productions:. Start variable: A. A  B  0A1  0B1  00A11  0011. Derivation:. A. B. Parse tree:. 0 1.

mshirley
Download Presentation

Context-Free Grammars

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. CSC 4170/CSC 8510 Theory of Computation Context-Free Grammars Chapter 2

  2. 2.1.a What is a CFG A  B A   B  0A1 Terminals: 0,1 Variables: A,B Productions: Start variable: A A  B  0A1  0B1  00A11  0011 Derivation: A B Parse tree: 0 1 A B 0 1 A 

  3. 2.1.b Our grammar simplified A  0A1 A    0A1  00A11  000A111  0000A1111  00001111 A What language does this grammar produce?

  4. 2.1.c1 A more complex CFG S  N’_V_N’ N’  N |N_who_V_N’ N  men | women | children V  like | hate | respect SN’_V_N’N’_like_N’N_like_N’N_like_Nwomen_like_Nwomen_like_children S N’ _ V _ N’ N like N women children

  5. 2.1.c2 A more complex CFG S  N’_V_N’ N’  N |N_who_V_N’ N  men | women | children V  like | hate | respect S N’ _ V _ N’ N respect N _ who _ V _ N’ men women hate N children

  6. 2.1.d Formal definitions Acontext-free grammaris a 4-tuple (V,,R,S), where 1. V is a finite set called thevariables; 2. is a finite set, disjoint from V, called theterminals; 3. R is a finite set ofrules, with each rule being a pair of a variable and a string of variables and terminals; 4. S is an element of V called thestart variable. If u,v, and w are strings of variables and terminals and A w is a rule, we say that uAv yields uwv, written uAv  uwv. x * y means that x=y, or x y, or there are z1,…,zn such that x z1 … zn y. Thelanguage produced(defined, described) by the grammar is {w | S * w and w is a string of (only) terminals}. Acontext-free languageis a language produced by some CFG.

  7. 2.1.e Ambiguity: An informal example the girl touches the boy with the flower Does this mean the girl touches (the boy with the flower) or the girl touches the boy with the flower (the girl touches the boy) with the flower ? with the flower the girl touches the boy

  8. 2.1.f An example of an ambiguous CFG <EXPR>  <EXPR> + <EXPR> | <EXPR>  <EXPR> | a a + a  a <EXPR> <EXPR> <EXPR> + <EXPR> <EXPR>  <EXPR> a <EXPR>  <EXPR> <EXPR> + <EXPR> a a a a a A grammar isambiguous iff it has two different parse trees for the same string

  9. 2.1.g An equivalent but unambiguous grammar <EXPR>  <EXPR> + <TERM> | <TERM> <TERM>  <TERM>  a | a <EXPR> <EXPR> + <TERM> <TERM> <TERM>  a a a a + a  a

  10. 2.1.h A more complex unambiguous grammar <EXPR>  <EXPR> + <TERM> | <TERM> <TERM>  <TERM>  <FACTOR> | <FACTOR> <FACTOR>  (<EXPR>) | a <EXPR> <TERM> <EXPR> <TERM>  <FACTOR> <EXPR> + <TERM> <FACTOR> a <TERM> <TERM>  <FACTOR> ( <EXPR> ) <FACTOR> <FACTOR> a <EXPR> + <TERM> <TERM> <FACTOR> a a <FACTOR> a a + a  a a (a + a)  a

  11. 2.1.i Designing context-free grammars Design a CFG that produces all regular expressions over the alphabet {0,1}: <RE>  Design a CFG G that produces the union of the languages produced by two given CFGs G1 and G2. G1: A1 w1 … An  wn G2: B1 u1 … Bm  um

  12. 2.1.j Converting a DFA into a CFG 0 0 Variables:The states of the DFA 1 Q1 Q2 1 Start variable:The start state of the DFA Productions: 1. Qi  a Qj, whenever there is an a-arrow from Qi to Qj; 2. Qi  , whenever Qi is an accept state.

  13. 2.1.j Testing in work 0 0 Q1  0 Q1 Q1  1 Q2 Q2  0 Q2 Q2  1 Q1 Q2   1 Q1 Q2 1 Q1 0Q1 01Q2 011Q1 0110Q1 01100Q1 011001Q2 011001 011001

  14. 2.2 Giorgi JaparidzeTheory of Computability Pushdown automata Just as to regular languages correspond finite automata, to context-free languages correspond pushdown automata. This is a new model of computation. What makes a PDA different from NFA is that it has potentially infinite, stack-type (LIFO) memory, where unlimited number of things can be remembered (pushed) and read (popped). A given transition depends on not only the current symbol in the input, but also the symbol currently at the top of the stack. PDAs are still not the most powerful machines. This is because of their limited (LIFO) access to memory.

  15. 2.2.a Components of a pushdown automaton (PDA) Stack Input a a b a c … x y x z . . . Push: write a symbol on the top of the stack Pop: delete a symbol from the top of the stack a,xy (Q,,,,s,F) q1 q2 If the input symbol is a and the top stack symbol is x, go from q1 to q2, pop x and push y Q is the set of states  is the input alphabet  is the stack alphabet  is the transition function s is the start state FQ is the set of accept states If a=, the read head is not advanced If x=, nothing is popped If y=, nothing is pushed

  16. 2.2.b1 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 0 0 1 1 1 Stack Input

  17. 2.2.b2 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 0 0 1 1 1 $ Stack Input

  18. 2.2.b3 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 $ 0 0 0 1 1 1 Stack Input

  19. 2.2.b4 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 0 $ 0 0 0 1 1 1 Stack Input

  20. 2.2.b5 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 0 0 $ 0 0 0 1 1 1 Stack Input

  21. 2.2.b6 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 0 $ 0 0 0 1 1 1 Stack Input

  22. 2.2.b7 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 $ 0 0 0 1 1 1 Stack Input

  23. 2.2.b8 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 0 0 1 1 1 $ Stack Input

  24. 2.2.b9 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  Accept 0 0 0 1 1 1 Stack Input

  25. 2.2.b10 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  What language does this automaton recognize?

  26. 2.2.b11 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 0 1 Stack Input

  27. 2.2.b12 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 0 1 $ Stack Input

  28. 2.2.b13 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 $ 0 0 1 Stack Input

  29. 2.2.b14 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 0 $ 0 0 1 Stack Input

  30. 2.2.b15 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  Reject 0 $ 0 0 1 Stack Input

  31. 2.2.b16 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 1 1 Stack Input

  32. 2.2.b17 How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  $ 0 1 1 Stack Input

  33. 2.2.b18 Giorgi JaparidzeTheory of Computability How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 $ 0 1 1 Stack Input

  34. 2.2.b19 Giorgi JaparidzeTheory of Computability How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  $ 0 1 1 Stack Input

  35. 2.2.b20 Giorgi JaparidzeTheory of Computability How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  Reject 0 1 1 Stack Input

  36. 2.2.b21 Giorgi JaparidzeTheory of Computability How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 1 0 Stack Input

  37. 2.2.b22 Giorgi JaparidzeTheory of Computability How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  $ 0 1 0 Stack Input

  38. 2.2.b23 Giorgi JaparidzeTheory of Computability How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  0 $ 0 1 0 Stack Input

  39. 2.2.b24 Giorgi JaparidzeTheory of Computability How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  $ 0 1 0 Stack Input

  40. 2.2.b25 Giorgi JaparidzeTheory of Computability How a PDA works ,$ 0,0 q1 q2 1,0   1,0  q3 q4 ,$  Reject 0 1 0 Stack Input

  41. 2.2.c Giorgi JaparidzeTheory of Computability Designing pushdown automata Design a pushdown automaton that recognizes the language {w | w has an equal number of 0s and 1s} 0 = s 1

  42. 2.2.d Giorgi JaparidzeTheory of Computability Converting NFA into PDA Every NFA can be understood as a PDA that never pushes or pops. Just replace every label a of the NFA by a, 1 1 b b,  a , a, a a, a b a, b, 2 3 2 3

  43. 2.2.e Giorgi JaparidzeTheory of Computability Main theorems Theorem 2.20:A language is context-free iff some pushdown automaton recognizes it. Theorem:Not every nondeterministic PDA has an equivalent deterministic PDA. Example 2.18:There is a nondeterministic PDA recognizing {wwR | w{0,1}* } (wR means w reversed), but no deterministic PDA can recognize this language. Proofs omitted.

  44. 2.3.a Giorgi JaparidzeTheory of Computability The pumping lemma for context-free languages Theorem 2.34(Pumping lemma for context-free languages) If L is a context-free language, then there is a number p (the pumping length) where, if s is any string in L of length at least p, then s may be divided into five pieces s = uvxyz satisfying the conditions: 1. For each i0, uvixyizL; 2. |vy| > 0; 3. |vxy| p. uxz uvxyz uvvxyyz uvvvxyyyz uvvvvxyyyyz uvvvvvxyyyyyz

  45. 2.3.b Giorgi JaparidzeTheory of Computability The pumping lemma in work: example S  “R” is a regular expression R  0 | ( R )* “0” is a regular expression “(0)*” is a regular expression “((0)*)*” is a regular expression “(((0)*)*)*” is a regular expression … u = “( v = ( x = 0 y = )* z = )*” is a regular expression “((0)*)*” is a regular expression uv0xy0z: “(0)*” is a regular expression uv1xy1z: “((0)*)*” is a regular expression uv2xy2z: “(((0)*)*)*” is a regular expression uv3xy3z: “((((0)*)*)*)*” is a regular expression

  46. 2.3.c Giorgi JaparidzeTheory of Computability Using the pumping lemma for proving that certain languages are not CF Example 2.36: Show that the following language is not CF: B = {anbncn | n0} Proof by contradiction: Assume B is CF. Let then p be its pumping length. Select wB with |w| p. By the pumping lemma, w=uvxyz and v andy can be pumped. If either v or y contain more than one type of symbols, then pumping would intermix these symbols in a wrong way. aaaabbbbccccBaaaababbbbcccccB Thus, one of the three symbols should be neither in neither v, nor in y. aaaabbbbccccB But then, after pumping, the number of that symbol will not change, while the number of the other symbols will increase. aaaaaabbbbbbbbcccc B

  47. 2.3.d Giorgi JaparidzeTheory of Computability Regular vs context-free vs computer-recognizable languages Computer-recognizable languages Context-free languages Regular languages {anbn | n0} {anbncn | n0}

More Related