Presentation is loading. Please wait.

Presentation is loading. Please wait.

Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki.

Similar presentations


Presentation on theme: "Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki."— Presentation transcript:

1 Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki

2 » Grammar: G = (V, , R, S)  = { a, b } Terminals V = {A, B, C, a,b, c} Terminals and Nonterminals S S Start Symbol R = S → A Rules

3 » Rules: X  Y, where: » X  V-  » Ex: S  aS » Y : , a, A, aB » G1: R= S  aS | ......? » G2: R= S  aSa | bSb | ....? » G3: R= S  SS | (S) | ......?

4 » Rules: X  Y, where: » X  V-  and |X| = 1 » Ex: S  aS » Y  V* » Ex: S   S  aSb » A  a A  bBBa » A  B S  SS » A  aB S  ABCD

5 » A grammar G is ambiguous iff there is at least one string in L(G) for which G produces more than one parse tree. » Ex: » Bal = { w  {),(}*: the parentheses are balanced} » G = {{S,),(}, {),(}, R, S}, where: » R = S  SS | (S) | 

6 For a string (())(), we have more than one parse trees

7 » Chomsky Normal Form (CNF) » Def.: A context-free grammar G = (V, Σ, R, S) is said to be in Chomsky Normal Form (CNF), iff every rule in R is of one of the following forms: » X  a where a  , or » X  BC where B and C  V-  » Greibach Normal Form (GNF) » Def.: GNF is a context free grammar G = (V, , R, S), where all rules have one of the following forms: » X  a  where a   and   (V-  )*

8 » There exists 5-steps algorithm to convert a CFG G into a new grammar Gc such that: L(G) = L(Gc) – {  } » convertChomsky(G:CFG) = » 1- G ' = removeEps(G:CFG) S   » 2- G '' = removeUnits(G':CFG) A  B » 3- G ''' = removeMixed(G '' :CFG) A  aB » 4- G' v = removeLong(G''' :CFG) S  ABCD » 5- G' v : L(G) = L(G' v ) – {  } G v = atmostoneEps(G' v :CFG) S*  S, S  

9 » Find the set N of nullable variables in G. » X is nullable iff either X   or » (X  A, A   ) : X   » Ex1: G: S  aACa » A  B | a » B  C | c » C  cC |  » Now, since C  , C is nullable » since B  C, B is nullable » since A  B, A is nullable » Therefore N = { A,B,C}

10 » According to N, the rules will be: » S  aACa » A  B | a |  » B  C | c |  » C  cC |  » Now, removeEps returns G' : » S  aACa | aAa | aCa | aa » A  B | a » B  C | c » C  cC | c

11 » Def.: unit production is a rule whose right hand side consists of a single nonterminal symbol. Ex: A  B » Remove any unit production from G '. » Consider the remain rules » Ex1(continue), G‘: » S  aACa | aAa | aCa | aa » A  B | a » B  C | c » C  cC | c

12 » Now by apply removeUnits(G':CFG) : » Remove A  B But B  C | c, so Add A  C | c » Remove B  C Add B  cC (B  c, already there) » Remove A  C Add A  cC (A  c, already there) » So removeUnits returns G'' : » S  aACa | aAa | aCa | aa » A  a | c | cC » B  c | cC » C  cC | c

13 » Def.: mixed is a rule whose right hand side consists of combination of terminals or terminals with nonterminal symbol. » Create a new nonterminal T a for each terminal a   » For each T a, add the rule T a  a » Ex1(continue), G'' : » S  aACa | aAa | aCa | aa » A  a | c | cC » B  c | cC » C  cC | c

14 » Now, by apply removeMixed(G '' :CFG), G ''' : » S  T a ACT a | T a AT a | T a CT a | T a T a » A  a | c | T c C » B  c | T c C » C  T c C | c » T a  a » T c  c

15 » Def.: long is a rule whose right hand side consists of more than two nonterminal symbol. » R: A  BCDE » By remove long, it will be: » A  BM 2 » M 2  CM 3 » M 3  DE » Ex1(continue), G'' : » S  aACa | aAa | aCa | aa » A  a | c | cC » B  c | cC » C  cC | c

16 » Ex1(continue), G''' : » S  T a ACT a | T a AT a | T a CT a | T a T a » A  a | c | T c C » B  c | T c C » C  T c C | c » T a  a » T c  c

17 » Now, by apply removeLong(G''' :CFG), G' v : » S  T a S 1 |T a S 3 |T a S 4 |T a T a » S 1  AS 2 S 2  CT a S 3  AT a S 4  CT a » A  a | c | T c C » B  c | T c C » C  T c C | c » T a  a » T c  c

18 » Finally, atmostoneEps(G' v :CFG) does not apply in Ex1, G v = G' v since L does not contain  (S is not nullable). » S  T a S 1 |T a S 3 |T a S 4 |T a T a » S 1  AS 2 S 2  CT a S 3  AT a S 4  CT a » A  a | c | T c C » B  c | T c C » C  T c C | c » T a  a » T c  c

19 » Convert the following CFG to CNF: » S  ABC » A  aC | D » B  bB |  | A » C  Ac |  | Cc » D  aa

20 » N = {A,B,C} So » add S  AB|BC|AC, A  a, B  b, C  c » delete B  , C   » The result is: » S  ABC| AB|BC|AC » A  aC | D|a » B  bB | A|b » C  Ac | Cc| c » D  aa

21 » S  ABC| AB|BC|AC » A  aC | D |a (A  D, D  aa) remove A  D add A  aa » B  bB | A|b (B  A, A  aC|aa|a) remove B  A add B  aC|aa|a » C  Ac | Cc|c » D  aa

22 » S  ABC| AB|BC|AC » A  aC | aa |a » B  bB | aC|aa|a|b » C  Ac | Cc|c » D  aa

23 » S  ABC| AB|BC|AC » A  T a C | T a T a |a » B  T b B | T a C| T a T a |a|b » C  AT c | CT c |c » D  T a T a

24 » S  AM 2 |M 2 | AC|AB » M 2  BC » A  T a C | T a T a |a » B  T b B | T a C| T a T a |a |b » C  AT c | CT c |c » D  T a T a » T a  a » T b  b » T c  c

25 » S  AM 2 |M 2 | AC|AB » M 2  BC » A  T a C | T a T a |a » B  T b B | T a C| T a T a |a |b » C  AT c | CT c |c » D  T a T a » T a  a » T b  b » T c  c

26 » Convert the following CFG to CNF: » A → BAB | B | ε » B → 00 | ε » 1- remove epsilon: » A → BAB | B | BB | AB | BA » B → 00 » 2- remove units: » A → BAB | 00 | BB | AB | BA » B → 00

27 » 3- remove mixed » A → BC | T 0 T 0 | BB | AB | BA » C → AB, B → T 0 T 0, T 0 → 0 » 4- Add epsilon » A → BC | T 0 T 0 | BB | AB | BA |  » C → AB » B → T 0 T 0 » T 0 → 0

28 » Which one is Chomsky Normal Form ? Why?

29 Not Chomsky Normal Form Chomsky Normal Form

30 » Convert the following CFG to CNF:

31 Introduce new variables for the terminals:

32 Introduce new intermediate variable to break first production:

33 Introduce intermediate variable:

34 Final grammar in Chomsky Normal Form: Initial grammar

35 » Convert the following CFG to CNF: » E  E + T » E  T » T  T  F » T  F » F  (E) » F  id

36 » Remove E  T, add E  T  F |F » Remove E  F, add E  (E) | id » Remove T  F, add T  (E) | id » The result: » E  E + T | T  F | (E) | id » T  T  F | (E) | id » F  (E) | id

37 » E  E T + T | T T  F | T ( E T ) | id » T  T T  F | T ( E T ) | id » F  T ( E T ) | id » T (  ( » T )  ) » T +  + » T *  *

38 » E  E M 2 | T M 3 | T ( M 4 | id » T  T M 3 | T ( M 4 | id » F  T ( M 4 | id » M 2  T + T » M 3  T * F » M 4  E T ) » T (  ( » T )  ) » T +  + » T *  *

39 » E  E M 2 | T M 3 | T ( M 4 | id » T  T M 3 | T ( M 4 | id » F  T ( M 4 | id » M 2  T + T » M 3  T * F » M 4  E T ) » T (  ( » T )  ) » T +  + » T *  *

40 » Elaine A. Rich (2008) Automata, Computability, and Complexity: Theory and Applications, Pearson Prentice Hall. » https://cs.wmich.edu/~elise/courses/cs680/pres entations.htm https://cs.wmich.edu/~elise/courses/cs680/pres entations.htm

41


Download ppt "Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki."

Similar presentations


Ads by Google