complexity and computability theory i

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

Upload: beatrice-johnson

Post on 31-Dec-2015

51 views

Category:

Documents


0 download

DESCRIPTION

Complexity and Computability Theory I. Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein. Overview. Grammars Example Context-free grammars Examples Ambiguity. Grammar. Another computational model. A member in the family of rewriting systems. - PowerPoint PPT Presentation

TRANSCRIPT

Complexity and Computability Theory I

Lecture #9

Instructor: Rina Zviel-Girshin

Lea Epstein

Rina Zviel-Girshin @ASC 2

Overview

GrammarsExampleContext-free grammars ExamplesAmbiguity

Rina Zviel-Girshin @ASC 3

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 Zviel-Girshin @ASC 4

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 Zviel-Girshin @ASC 5

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 Zviel-Girshin @ASC 6

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 Zviel-Girshin @ASC 7

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 Zviel-Girshin @ASC 8

Example of a grammar

={a,b,c}• The following grammar generates all strings over

.

SaS (add a)

SbS (add b)

ScS (add c)

S (delete S)

Rina Zviel-Girshin @ASC 9

w =aacb production• How can the word w=aacb be produced?• SaS• We used the SaS production because w starts

with a and the only rule that starts with a is SaS.• From S that remains we need to produce w'=acb.• SaSaaS• We used the SaS production because w' also

starts with a and the only rule that starts with a is SaS.

Rina Zviel-Girshin @ASC 10

w =aacb production (cont.)

• From S we need to produce w''=cb.• SaSaaSaacS• We used the ScS production because w'' starts

with c and the only rule that starts with c is ScS.• From S we need to produce b.• SaSaaSaacSaacbS• We used the SbS production to produce b.

Rina Zviel-Girshin @ASC 11

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.

• SaSaaSaacSaacbSaacb

• So we managed to produce w using the rules of the grammar.

Rina Zviel-Girshin @ASC 12

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 Zviel-Girshin @ASC 13

Parsing w=aacbS

Sa

Sa

Sc

Sb

Rina Zviel-Girshin @ASC 14

Parsing w=aacb

• Or a step by step derivation:

S

Sa

S S

Sa

Sa

Rina Zviel-Girshin @ASC 15

Parsing w=aacb (cont.)

S

Sa

Sa

Sc

Sb

S

Sa

Sa

Sc

Sb

S

Sa

Sa

Sb

Rina Zviel-Girshin @ASC 16

Context-free grammar A context-free grammar (CFG) G is a 4-tuple (V, , S,

R), where1. V is a finite set called the variables2. is a finite set, disjoint from V, called the terminals3. S is a start symbol4. R is a finite set of production rules, with each rule

being a variable and a string of variables and terminals: ab, aV and b(VU)*

 

Rina Zviel-Girshin @ASC 17

uAv yields uwv

• If u, v and w are strings of variables and terminals and Aw is a rule of the grammar, we say that uAv yields uwv, written uAvuwv.

• We write u*w if there exists a sequence u1, u2, ..uk, k0 and

uu1u2...w.

Rina Zviel-Girshin @ASC 18

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 Zviel-Girshin @ASC 19

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 Zviel-Girshin @ASC 20

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 Zviel-Girshin @ASC 21

Examples over ={0,1}

• Construct a grammar for the following languageL = {0,00,1}

 • G = (V={S},={0,1},S, R) where R:

S 0 S 00S 1orS 0 | 00 | 1

Rina Zviel-Girshin @ASC 22

Examples over ={0,1}

• Construct a grammar for the following language L = {0n1n |n0}

 • G = (V={S},={0,1},S, R) where R:

S0S1Sor S0S1 |

Rina Zviel-Girshin @ASC 23

Examples over ={0,1}

• Construct a grammar for the following language

L = {0n1n |n1} • G = (V={S},={0,1},S, R) where R:

S 0S1 | 01

Rina Zviel-Girshin @ASC 24

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 | 1BB 1B |

Rina Zviel-Girshin @ASC 25

Examples over ={0,1}

• Construct a grammar for the following language

L = {02i+1 | i0}

 

• G = (V={S},={0,1},S, R) where R:

S 0 | 00S

Rina Zviel-Girshin @ASC 26

Examples over ={0,1}

• Construct a grammar for the following language

L = {0i+11i | i0}

 

• G = (V={S},={0,1},S, R) where R:

S 0 | 0S1

Rina Zviel-Girshin @ASC 27

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 Zviel-Girshin @ASC 28

Examples over ={0,1}

• Construct a grammar for the following language

L = {0n1n |n1} {1n0n | n0}

 •  G = (V={S,A,B},={0,1},S, R) where R:

S A | B

A 0A1 | 01

B 1B0 |

Rina Zviel-Girshin @ASC 29

From a grammar to a CFL

• Give a description of L(G) for the following grammar:

S 0S0 | 1

 

• L(G) = {0n10n|n0}

Rina Zviel-Girshin @ASC 30

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#wR| w*}

Rina Zviel-Girshin @ASC 31

From a grammar to a CFL

• Give a description of L(G) for the following grammar:

S 0A | 0B

A1S

B1

• L(G) = {(01)n |n1 }

Rina Zviel-Girshin @ASC 32

From a grammar to a CFL

• Give a description of L(G) for the following grammar:

S 0S11 | 0

• L(G) = {0 n+112n |n1 }

Rina Zviel-Girshin @ASC 33

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 Zviel-Girshin @ASC 34

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 Zviel-Girshin @ASC 35

Question• Can any finite language be constructed by a grammar?

 Yes.

Proof:

• Let L={wi | in and wi*} be a finite language over .

• We construct the following grammar:Sw1

Sw2

..

Swn

Rina Zviel-Girshin @ASC 36

Question (cont.)

• The grammar derives all n words of L.

• The grammar is finite (n production rules).

• The grammar syntax is correct.

Rina Zviel-Girshin @ASC 37

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 Zviel-Girshin @ASC 38

Example

EE+E | E*E | T

T0|1|2|..|9 E

EE

3

+

EE *

4

E

5

EE *

EE +

43

T

T

5

TT T

T

• The string 3+4*5 can be produced in several ways: 

Rina Zviel-Girshin @ASC 39

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 Zviel-Girshin @ASC 40

The conclusion

• The conclusion:• programming languages should have a

unique interpretationor• the grammar of the programming

language would be unambiguous.

Rina Zviel-Girshin @ASC 41

Any Questions?