Presentation is loading. Please wait.

Presentation is loading. Please wait.

نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.

Similar presentations


Presentation on theme: "نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88."— Presentation transcript:

1 نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88

2 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Table Of Content Context-Free Grammars Pushdown Automata Non-Context-Free Languages

3 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Context-Free Grammars A collection of substitution rules (production) Each rule → A line in the grammar Each line → A variable, an arrow, a string The string → variables and terminals

4 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Context-Free Grammars (Example) An Example: (G1) Three rules Variables: A,B Start Variable: A Terminals: 0,1,#

5 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Derivation Generating the strings of the language: G1:

6 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Parse Tree

7 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Context-Free Language All Strings generated in this way L(G 1 ) : G2 (describes a fragment of the English language):

8 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف A Derivation Example Derivation of the sentence “ a boy sees ” in G2

9 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Context-Free Language (Formal Definition)

10 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Context-Free Language (Formal Definition) u derives v The language of the grammar:

11 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Examples G3: R (Set of Rules): Strings such as: abab, aaabbb, aababb If: a → ( and b → ), then: L(G3) → language of all strings of properly nested parentheses

12 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Examples G4: V: Σ: R (Set of Rules):

13 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Examples (G4)

14 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Designing Context-Free Grammars Break the CFL into simpler pieces, if possible Construct grammar for each piece Combine the rules and the new rule S →S 1 |S 2 |…|S k, where the S i are the start variables for the individual grammars

15 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example Language: : Add the rule: to give the grammar:

16 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Designing Context-Free Grammars Any DFA can be converted to an equivalent CFG Make a variable R i for each state q i Add the rule, if Add the rule, if q i is an accept state

17 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Ambiguity When a grammar can generate the same string in different ways Undesirable for applications such as programming languages

18 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example This grammar generates the string ambiguously

19 Ambiguity Definition: A string w is ambiguous if it can be derived by at least two derivation (parse) trees. 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

20 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Ambiguity Leftmost (Rightmost) derivation: at every step, the leftmost (rightmost) remaining variable is replaced.

21 Example G: S → aAB A → bBb B → A|ε Leftmost derivation: S aAB abBbB abAbB abbBbbB abbbbB abbbb Rightmost derivation: S aAB aAA aAbBb aAbb abBbbb abbbb 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

22 Example (ctd) 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف S aAB B b bε A bbB ε A b S Ba A B bB ε bε b Tree 1Tree2

23 Example (ctd) Tree1: Rightmost derivation: S aAB aA abBb abAb abbBbb abbbb Tree2 Leftmost derivation: S aAB abBbB abbB abbA abbbBb abbbb 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

24 Ambiguity Definition: A string w is ambiguous in a context-free grammar if there are at least two different leftmost (rightmost) derivations or two different derivation trees for that string in that grammar. A context- free grammar is ambiguous if it can generates at least one ambiguous string. Otherwise it is unambiguous. 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

25 Inherently Ambiguous Languages Definition: A context-free language is (inherently) ambiguous if all context-free grammars generating it will be ambiguous. 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

26 Examples The following context-free languages are inherently ambiguous: L 1 = {a i b j c k | i=j or j=k where i,j,k ≥0} L 2 = {a i b j c k d m | i=j, k=m or i=m, j=k where i,j,k,m ≥1} 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

27 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Simplification of CFG Let G = (V, Σ, R, S) be a CFG. Suppose R contains a rule of the form A → x 1 Bx 2 Assume that A and B are different variables and that B → y 1 | y 2 | … | y n is the set of all rules in R which has B as the left side. Let G’ = (V, Σ, R’, S) be the grammar in which R’ is constructed by deleting A → x 1 Bx 2 from R and adding to it A → x 1 y 1 x 2 | x 1 y 2 x 2 | … | x 1 y n x 2. Then L(G’) = L(G).

28 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example: Consider grammar G: A → a | aaA | abBc, B → abbA | b Using the substitution property, we get G’: A → a | aaA | ababbAc | abbc, B → abbA | b The new grammar G’ is equivalent to G.

29 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Useful Variable Let G = (V, Σ, R, S) be a CFG. A variable A  V is said to be useful if and only if there is at least one w  L(G) such that S * xAy * w with x, y  (V U Σ)*. A variable that is not useful is called useless. A rule is useless if it involves any useless variable.

30 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example: G: S → aS | A | C A → a B → aa C → aCb ------------------------------------------ S → aS | A A → a B → aa ------------------------------------------------- S → aS | A A → a SAB Dependency Graph

31 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Theorem Let G = (V, Σ, R, S) be a CFG. Then there exists an equivalent grammar G’ = (V’, Σ’, R’, S) that does not contain any useless variable or rules.

32 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف ε rules Any rule of a context-free grammar of the form A → ε is called an ε rule. Any variable A for which the derivation A * ε is called nullable.

33 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example: G: S → ABaC A → BC B → b | ε C → D | ε D → d A, B, C are nullable variables above. Then we have: S → ABaC | BaC | AaC | ABa | aC | Aa | Ba | a A → B | C | BC B → b C → D D → d

34 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Theorem: Let G be any context-free grammar with ε not in L(G). Then there exists an equivalent grammar G’ having no ε rules.

35 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Unit rules Any rule of a context-free grammar of the form A → B where A,B  V is called a unit rule.

36 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example: G: S → Aa | B B → A | bb A → a | bc | B S * A, S * B, B * A, A * B ------------------------ S → Aa B → bb A → a | bc ------------------------------ S → a | bc | bb | Aa B → a | bb | bc A → a | bb | bc SAB Dependency graph for unit rules

37 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Theorem: Let G be any context-free grammar without any ε rule. Then there exists a context-free grammar G’ that does not have any unit rules and that is equivalent to G.

38 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Theorem: Let L be a context-free language that does not contain ε. Then there exists a context-free grammar that generates L and does not have any useless rules, ε rules, or unit rules. Solution: Remove ε rules Remove unit rules Remove useless rules.

39 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف CHOMSKY NORMAL FORM Any context-free language is generated by a context-free grammar in Chomsky normal form.

40 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example Step 1: (make a new start variable) Step 2: Remove ε rules B → ε, shown on the left and A → ε, shown on the right

41 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example (cont.) Step 3a: Remove unit rules S →S, shown on the left and S 0 →S, shown on the right Step 3b: Remove unit rules A→B and A→S

42 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example (cont.) Step 4: convert the remaining rules into the proper form by adding variables and rules

43 CYK Algorithm (J. Cooke, D.H. Younger, and T. Kasami) Let G = (V, Σ, R, S) be a context-free grammar (in Chomsky normal form) and w a string of length n w = a 1 a 2 …a n. The membership problem is the problem of finding if w L(G). 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

44 CYK Algorithm (ctd.) Define w ij = a i … a j and subsets of V We can show 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

45 CYK Algorithm (ctd.) Then, the algorithm to compute the problem is as follows with the time complexity O(n 3 ): 1.Compute V 11, V 22, …, V nn 2.Compute V 12, V 23, …, V n-1,n 3.Compute V 13, V 24, …, V n-2,n and so on. 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

46 Example Determine whether the string w = aabbb is in the language generated by the grammar G below S → AB A → BB|a B → AB|b We have: V 11 = V 22 = {A}, V 33 = V 44 = V 55 = {B}, V 12 = Ø, V 23 = {S, B}, V 34 = V 45 = {A}, V 13 = = V 35 = {S, B}, V 24 = {A}, V 14 = {A}, V 25 = {S, B}, V 15 = {S, B}, so that w L(G). 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

47 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف PUSHDOWN AUTOMATA Like nondeterministic finite automata, with an extra component: stack The stack provides additional memory Pushdown automata are equivalent in power to context-free

48 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف PUSHDOWN AUTOMATA

49 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف PUSHDOWN AUTOMATA Push: Writing a symbol on the stack Pop: Removing a symbol Recognizes {0 n 1 n |n≥0} As each 0 is read from the input, push it onto the stack. Pop a 0 off the stack for each 1 read. Reading the input is finished exactly when the stack becomes empty →Accept

50 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف PUSHDOWN AUTOMATA ( formal definition)

51 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example 1 L={0 n 1 n |n≥0} Q={q 1,q 2,q 3,q 4 } Σ={0,1} Γ={0,$} F={q 1,q 4 } δ:

52 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example (cont.)

53 Example2 Give a pushdown automaton for the language: {a i b j c k | i,j,k ≥ 0 and i = j or i = k} 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

54 Example3 Give a pushdown automaton for the language: {ww R | w {0, 1}*} 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

55 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Equivalence with context-free grammars A language is context free if and only if some pushdown automaton recognizes it. Proof Idea: –Only if part : Let A be a CFL. Show how to convert the A’s CFG, G into a PDF P that accepts all strings that G generates. – If part : Let P be a PDA. Show how to convert P into a CFG G that generates all strings that P accepts.

56 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof of “ only if ” part Any terminal symbols appearing before the first variable are matched with input.

57 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof

58 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof (cont.) (r,u) ∈ δ(q,a,s) means that when q is the state of the automaton, a is the next input symbol, and s is on the top of the stack, push the string u onto the stack and go on to the state r.

59 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof (cont.) Q={q start,q loop,q accept } E where E is the set of states needed for implementing the shorthand described before. Step 1: δ(q start,ε,ε)={(q loop,S$)} Step 2: –(a) δ(q loop,ε,A)={(q loop,w)|where A →w is a rule} –(b) δ(q loop,a,a)={(q loop,ε)} –(c) δ(q loop,ε,$)={(q accept,ε)}

60 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof (cont.)

61 Example Construct a PDA for the CFG grammar: S → aTb | b T → Ta | ε 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

62 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof of “ if ” part First, we simplify our task by modifying P slightly to give it the following three features. –It has a single accept state, q accept. –It empties its stack before accepting. –Each transition either pushes a symbol onto a stack (a push move) or pops one off the stack (a pop move), but does not do both at the same time. Next, for each pair of states p and q in P the grammar will have a variable A pq. This variable generates all strings that can take P from p with an empty stack to q with an empty stack. Observe that such strings can also take P from p to q, regardless of the stack contents at p, leaving the stack at q in the same condition as it was at p.

63 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof Suppose P = (Q, Σ, Γ, δ, q 0, {q accept }). We can construct CFG G. The variables of G are {A pq | p,q Q}. The start variable is A q 0,q accept. Now we describe G’s rules: –For each p,q,r,s Q, t Γ, and a,b Σ ε, if δ(p, a, ε) contains (r,t) and δ(s, b, t) contains (q, ε), put rule A pq → aA rs b in G. –For each p,q,r Q, put rule A pq →A pr A rq in G. –Finally, for each p Q, put the rule A pp → ε in G.

64 PDA computation corresponding to the rule A pq →A pr A rq PDA computation corresponding to the rule A pq → aA rs b 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

65 Claims Claim 1: If A pq generates x, then x can bring P from p with empty stack to q with empty stack. – The proof is done by induction on the number of steps in the derivation of x from A pq. Claim 2: If x can bring P from p with empty stack to q with empty stack, A pq generates x. –The proof is done by induction on the number of steps in the computation of P that goes from p to q with empty stacks on input x. 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

66 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Relationship of the regular and context-free languages Every regular language is context-free.

67 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Deterministic PDAs A pushdown automaton M = (Q, Σ, Γ, δ, q 0, F) is said to be deterministic if it has the following properties:  q Q,  A Γ ε,  a Σ, δ(q, ε, A) ≠ Φ → δ(q, a, A) = Φ  q Q,  A Γ,  a Σ ε, δ(q, a, ε) ≠ Φ → δ(q, a, A) = Φ  q Q,  A Γ ε,  a Σ, δ(q, ε, ε) ≠ Φ → δ(q, a, A) = Φ  q Q,  A Γ ε,  a Σ ε, |δ(q, a, A)|  1

68 Deterministic Context-free Languages Definition: A context-free language is said to be deterministic if it can be accepted by some deterministic pushdown automaton. 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

69 Example 1 L = {a n b n : n≥0} is a deterministic language because it can be accepted by the following deterministic automaton: 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

70 Example 2 It can be shown that the language L 1 = {a n b n : n≥0} U {a n b 2n : n≥0} is nondeterministic. 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

71 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف NON-CONTEXT-FREE LANGUAGES Pumping Lemma for context-free languages: –If A is a context-free language, then there is a number p (the pumping length) where, if s is any string in A of length at least p, then s may be divided into five pieces s=uvxyz satisfying the conditions: For each i≥0, uv i xy i z ∈ A |vy|>0 |vxy|≤p

72 Proof Let G be a CFG for CFL A. Let b be the maximum number of symbols in the right-hand side of a rule (assume at least 2). Then, if the height of the parse tree is at most h, the length of the string generated is at most b h. Conversely, if a generated string is at least b h +1 long, each of its parse tree must be at least h+1 high. Let |V| be the number of variables in G. Then, we set p to be b |V|+1. Now if s is a string in A and its length is p or more, any of its parse trees must be at least |V| +1 high, because b |V|+1 ≥ b |v| +1. We choose the smallest of such parse trees. 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

73 Proof (cont.) Surgery on parse trees 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

74 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example 1 B={a n b n c n |n≥0} Assume that B is a CFL and p is the pumping length. s=a p b p c p : a member of B and of length at least p We show that it can not be pumped

75 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example 1 (cont.) s=uvxyz Condition1 –v and y contain one type pf symbol → uv 2 xy 2 z cannot contain equal number of a’s, b’s and c’s. –Either v or y contain more than one type of symbol→ uv 2 xy 2 z cannot contain a’s, b’s and c’s in the correct order.

76 Example 2 Let D = {ww | w {0,1}*} Assume that D is a CFL and p is the pumping length. s= 0 p 10 p 1: a member of D and of length at least p; it will not work! 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

77 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example 2 (cont.) Let us choose s = 0 p 1 p 0 p 1 p it will work! We show that it cannot be pumped Note that s=uvxyz = 0 p 1 p 0 p 1 p

78 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Deterministic PDAs A pushdown automaton M = (Q, Σ, Γ, δ, q 0, F) is said to be deterministic if it has the following properties:  q Q,  A Γ ε,  a Σ, δ(q, ε, A) ≠ Φ → δ(q, a, A) = Φ  q Q,  A Γ,  a Σ ε, δ(q, a, ε) ≠ Φ → δ(q, a, A) = Φ  q Q,  A Γ ε,  a Σ, δ(q, ε, ε) ≠ Φ → δ(q, a, A) = Φ  q Q,  A Γ ε,  a Σ ε, |δ(q, a, A)|  1

79 88 بهارنظریه زبان ها و ماشین ها دانشگاه صنعتی شریف End of chapter 2 End of chapter 2


Download ppt "نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88."

Similar presentations


Ads by Google