Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein
Rina Overview Grammars Example Context-free grammars Examples Ambiguity
Rina 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.
Rina 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.
Rina 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.
Rina 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.
Rina 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
Rina 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)
Rina 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.
Rina 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.
Rina 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.
Rina 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.
Rina Parsing w=aacb
Rina Parsing w=aacb Or a step by step derivation:
Rina Parsing w=aacb (cont.)
Rina 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 )*
Rina 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.
Rina 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
Rina 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).
Rina 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.
Rina 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
Rina 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 |
Rina 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
Rina 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 |
Rina 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
Rina 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
Rina 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
Rina 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 |
Rina 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}
Rina 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 *}
Rina From a grammar to a CFL Give a description of L(G) for the following grammar: S 0A | 0B A 1S B1B1 L(G) = {(01) n |n 1 }
Rina 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 }
Rina 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 }
Rina 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) }
Rina 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: Sw1Sw1 Sw2Sw2.. SwnSwn
Rina Question (cont.) The grammar derives all n words of L. The grammar is finite (n production rules). The grammar syntax is correct.
Rina 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.
Rina Example E E+E | E*E | T T 0|1|2|..|9 The string 3+4*5 can be produced in several ways:
Rina 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.
Rina The conclusion The conclusion: programming languages should have a unique interpretation or the grammar of the programming language would be unambiguous.
Rina Any Questions?