Presentation is loading. Please wait.

Presentation is loading. Please wait.

Context-Free Grammars

Similar presentations


Presentation on theme: "Context-Free Grammars"— Presentation transcript:

1 Context-Free Grammars
CSC 4170/CSC 8510 Theory of Computation Context-Free Grammars Chapter 2

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

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

4 N  men | women | children V  like | hate | respect
A more complex CFG 2.1.c1 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 N  men | women | children V  like | hate | respect
A more complex CFG 2.1.c2 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 A context-free grammar is a 4-tuple (V,,R,S), where
Formal definitions 2.1.d A context-free grammar is a 4-tuple (V,,R,S), where 1. V is a finite set called the variables; 2.  is a finite set, disjoint from V, called the terminals; 3. R is a finite set of rules, with each rule being a pair of a variable and a string of variables and terminals; 4. S is an element of V called the start 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. The language produced (defined, described) by the grammar is {w | S * w and w is a string of (only) terminals}. A context-free language is a language produced by some CFG.

7 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 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 is ambiguous iff it has two different parse trees for the same string

9 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 A more complex unambiguous grammar
2.1.h <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 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 Converting a DFA into a CFG
2.1.j 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 011001 Testing in work Q1  0 Q1 Q1  1 Q2 Q2  0 Q2 Q2  1 Q1 Q2  
2.1.j 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 languages correspond pushdown automata.
2.2 Giorgi Japaridze Theory of Computability 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 Components of a pushdown automaton (PDA)
Stack Input a a b a c … x y 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 0 0 0 1 1 1 Stack Input How a PDA works , $ 0, 0 q1 q2 1,0  
2.2.b1 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   Stack Input

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

42 Converting NFA into PDA
Giorgi Japaridze Theory of Computability 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, 3 2 3 2

43 Theorem 2.20: A language is context-free iff
Main theorems 2.2.e Giorgi Japaridze Theory of Computability 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 The pumping lemma for context-free languages
Giorgi Japaridze Theory of Computability 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 The pumping lemma in work: example
2.3.b Giorgi Japaridze Theory of Computability 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 Using the pumping lemma for proving that certain languages are not CF
Giorgi Japaridze Theory of Computability 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 and y 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 B aaaababbbbccccc 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 Regular vs context-free vs computer-recognizable languages
2.3.d Giorgi Japaridze Theory of Computability Computer-recognizable languages Context-free languages Regular languages {anbn | n0} {anbncn | n0}


Download ppt "Context-Free Grammars"

Similar presentations


Ads by Google