Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein.

Similar presentations


Presentation on theme: "Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein."— Presentation transcript:

1 Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein

2 Rina Zviel-Girshin @ASC2 Overview Grammars Example Context-free grammars Examples Ambiguity

3 Rina Zviel-Girshin @ASC3 Grammar Another computational model. A member in the family of rewriting systems. The computation is by rewriting a string. –We start with an empty string and rewrite the string according to the grammar until we have an output. All possible outputs of a grammar is the language of the grammar.

4 Rina Zviel-Girshin @ASC4 The origin The origin of the name grammar for this computational model is in natural languages, where grammar is a collection of rules. This collection defines what is legal in the language and what is not.

5 Rina Zviel-Girshin @ASC5 The grammar computational model In the same manner the grammar computational model is primarily a collection of: –rules of rewriting, –rules how to build strings that are in the language, –structural rules for the language.

6 Rina Zviel-Girshin @ASC6 Some facts The grammar consists of a collection of rules over an alphabet  and a set of variables (usually denoted by capital letters of the Latin alphabet). Every grammar has a start symbol also called a start variable (usually denoted by S). Every grammar has at least one rule.

7 Rina Zviel-Girshin @ASC7  notation We will use the notation  in grammar rules.  What does it mean: (  )? It means : –  can be replaced by  –  constructs  –  produces  –  rewrites to  –  reduces to 

8 Rina Zviel-Girshin @ASC8 Example of a grammar  ={a,b,c} The following grammar generates all strings over . S  aS(add a) S  bS(add b) S  cS(add c) S  (delete S)

9 Rina Zviel-Girshin @ASC9 w =aacb production How can the word w=aacb be produced? S  aS We used the S  aS production because w starts with a and the only rule that starts with a is S  aS. From S that remains we need to produce w'=acb. S  aS  aaS We used the S  aS production because w' also starts with a and the only rule that starts with a is S  aS.

10 Rina Zviel-Girshin @ASC10 w =aacb production (cont.) From S we need to produce w''=cb. S  aS  aaS  aacS We used the S  cS production because w'' starts with c and the only rule that starts with c is S  cS. From S we need to produce b. S  aS  aaS  aacS  aacbS We used the S  bS production to produce b.

11 Rina Zviel-Girshin @ASC11 w =aacb production (cont.) But S is still remaining in final production. We want to delete it. We will use the rule S  to delete S. S  aS  aaS  aacS  aacbS  aacb So we managed to produce w using the rules of the grammar.

12 Rina Zviel-Girshin @ASC12 Parsing What we did is called parsing a word w accordingly to a given grammar. To parse a word or sentence means to break it into parts that confirm to a given grammar. We can represent the same production sequence by a parse tree or derivation tree. Each node in the tree is either letter or variable. Only a variable node can have children.

13 Rina Zviel-Girshin @ASC13 Parsing w=aacb

14 Rina Zviel-Girshin @ASC14 Parsing w=aacb Or a step by step derivation:

15 Rina Zviel-Girshin @ASC15 Parsing w=aacb (cont.)

16 Rina Zviel-Girshin @ASC16 Context-free grammar A context-free grammar (CFG) G is a 4-tuple (V, , S, R), where 1. V is a finite set called the variables 2.  is a finite set, disjoint from V, called the terminals 3. S is a start symbol 4. R is a finite set of production rules, with each rule being a variable and a string of variables and terminals: a  b, a  V and b  (VU  )*

17 Rina Zviel-Girshin @ASC17 uAv yields uwv If u, v and w are strings of variables and terminals and A  w is a rule of the grammar, we say that uAv yields uwv, written uAv  uwv. We write u  *w if there exists a sequence u1, u2,..uk, k  0 and u  u1  u2 ...  w.

18 Rina Zviel-Girshin @ASC18  notation We also use the following notations:  means derives in one step  + means derives in one or more steps  * means derives in zero or more steps

19 Rina Zviel-Girshin @ASC19 The language of the grammar The language of the grammar is L(G) = {w  * | w  * and S  * w} The language generated by CFG is called a context-free language (CFL).

20 Rina Zviel-Girshin @ASC20 Is the following definition correct? The language of the grammar is L(G) = {w  * | w  * and S  + w} Yes. Because a derivation in zero steps derivation produces only S. S is not a string over  *, so can't belong to L.

21 Rina Zviel-Girshin @ASC21 Examples over  ={0,1} Construct a grammar for the following language L = {0,00,1} G = (V={S},  ={0,1},S, R) where R: S  0 S  00 S  1 or S  0 | 00 | 1

22 Rina Zviel-Girshin @ASC22 Examples over  ={0,1} Construct a grammar for the following language L = {0 n 1 n |n  0} G = (V={S},  ={0,1},S, R) where R: S  0S1 S  or S  0S1 | 

23 Rina Zviel-Girshin @ASC23 Examples over  ={0,1} Construct a grammar for the following language L = {0 n 1 n |n  1} G = (V={S},  ={0,1},S, R) where R: S  0S1 | 01

24 Rina Zviel-Girshin @ASC24 Examples over  ={0,1} Construct a grammar for the following language L = {0 * 1 + } G = (V={S,B},  ={0,1},S, R) where R: S  0S | 1B B  1B | 

25 Rina Zviel-Girshin @ASC25 Examples over  ={0,1} Construct a grammar for the following language L = {0 2i+1 | i  0} G = (V={S},  ={0,1},S, R) where R: S  0 | 00S

26 Rina Zviel-Girshin @ASC26 Examples over  ={0,1} Construct a grammar for the following language L = {0 i+1 1 i | i  0} G = (V={S},  ={0,1},S, R) where R: S  0 | 0S1

27 Rina Zviel-Girshin @ASC27 Examples over  ={0,1} Construct a grammar for the following language L = {w| w  * and |w|mod 2=1} G = (V={S},  ={0,1},S, R) where R: S  0 | 1| 1S1| 0S0 |1S0 | 0S1

28 Rina Zviel-Girshin @ASC28 Examples over  ={0,1} Construct a grammar for the following language L = {0 n 1 n |n  1}  {1 n 0 n | n  0} G = (V={S,A,B},  ={0,1},S, R) where R: S  A | B A  0A1 | 01 B  1B0 | 

29 Rina Zviel-Girshin @ASC29 From a grammar to a CFL Give a description of L(G) for the following grammar: S  0S0 | 1 L(G) = {0 n 10 n |n  0}

30 Rina Zviel-Girshin @ASC30 From a grammar to a CFL Give a description of L(G) for the following grammar: S  0S0 | 1S1 | # L(G) = {The subset of all palindromes over  ={0,1} with # in the middle} or L(G) = {w#w R | w  *}

31 Rina Zviel-Girshin @ASC31 From a grammar to a CFL Give a description of L(G) for the following grammar: S  0A | 0B A  1S B1B1 L(G) = {(01) n |n  1 }

32 Rina Zviel-Girshin @ASC32 From a grammar to a CFL Give a description of L(G) for the following grammar: S  0S11 | 0 L(G) = {0 n+1 1 2n |n  1 }

33 Rina Zviel-Girshin @ASC33 From a grammar to a CFL Give a description of L(G) for the following grammar: S  E | NE N  D | DN D  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 E  0 | 2 | 4 | 6 L(G) = {w | w represents an even octal number }

34 Rina Zviel-Girshin @ASC34 From a grammar to a CFL Give a description of L(G) for the following grammar: S  N.N | -N.N N  D | DN D  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 L(G) = {w | w represents a rational number (that has a finite representation) }

35 Rina Zviel-Girshin @ASC35 Question Can any finite language be constructed by a grammar? Yes. Proof: Let L={w i | i  n and w i  *} be a finite language over . We construct the following grammar: Sw1Sw1 Sw2Sw2.. SwnSwn

36 Rina Zviel-Girshin @ASC36 Question (cont.) The grammar derives all n words of L. The grammar is finite (n production rules). The grammar syntax is correct.

37 Rina Zviel-Girshin @ASC37 Ambiguity The ability of grammar to generate the same string in several ways is called ambiguity. That means that the string have different parse trees and may have different meanings. A grammar is ambiguous if there exists a string w that has at least two different parse trees.

38 Rina Zviel-Girshin @ASC38 Example E  E+E | E*E | T T  0|1|2|..|9 The string 3+4*5 can be produced in several ways:

39 Rina Zviel-Girshin @ASC39 Example (cont.) So if we use this grammar to produce a programming language then we will have several computations of 3+4*5. There is no precedence of * over the +. This language will be impossible to use because the user won't know which computation compiler uses. Two possible results: 35 or 23.

40 Rina Zviel-Girshin @ASC40 The conclusion The conclusion: programming languages should have a unique interpretation or the grammar of the programming language would be unambiguous.

41 Rina Zviel-Girshin @ASC41 Any Questions?


Download ppt "Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein."

Similar presentations


Ads by Google