Complexity and computability theory i
Download
1 / 41

Complexity and Computability Theory I - PowerPoint PPT Presentation


  • 102 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Complexity and Computability Theory I' - beatrice-johnson


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Complexity and computability theory i

Complexity and Computability Theory I

Lecture #9

Instructor: Rina Zviel-Girshin

Lea Epstein


Overview
Overview

  • Grammars

  • Example

  • Context-free grammars

  • Examples

  • Ambiguity

Rina Zviel-Girshin @ASC


Grammar
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


The origin
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


The grammar computational model
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


Some facts
Some facts

  • The grammar consists of a collection ofrules 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


Notation
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


Example of a grammar
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 Zviel-Girshin @ASC


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


W aacb production cont
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 Zviel-Girshin @ASC


W aacb production cont1
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 Zviel-Girshin @ASC


Parsing
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


Parsing w aacb
Parsing w=aacb

Rina Zviel-Girshin @ASC


Parsing w aacb1
Parsing w=aacb

  • Or a step by step derivation:

Rina Zviel-Girshin @ASC


Parsing w aacb cont
Parsing w=aacb (cont.)

Rina Zviel-Girshin @ASC


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


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


Notation1
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


The language of the grammar
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


Is the following definition correct
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


Examples over 0 1
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 Zviel-Girshin @ASC


Examples over 0 11
Examples over ={0,1}

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

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

    S0S1

    S

    or

    S0S1 | 

Rina Zviel-Girshin @ASC


Examples over 0 12
Examples over ={0,1}

  • Construct a grammar for the following language

    L = {0n1n |n1}

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

    S  0S1 | 01

Rina Zviel-Girshin @ASC


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


Examples over 0 14
Examples over ={0,1}

  • Construct a grammar for the following language

    L = {02i+1 | i0}

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

    S 0 | 00S

Rina Zviel-Girshin @ASC


Examples over 0 15
Examples over ={0,1}

  • Construct a grammar for the following language

    L = {0i+11i | i0}

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

    S 0 | 0S1

Rina Zviel-Girshin @ASC


Examples over 0 16
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


Examples over 0 17
Examples over ={0,1}

  • Construct a grammar for the following language

    L = {0n1n |n1} {1n0n | n0}

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

    S  A | B

    A  0A1 | 01

    B  1B0 | 

Rina Zviel-Girshin @ASC


From a grammar to a cfl
From a grammar to a CFL

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

    S  0S0 | 1

  • L(G) = {0n10n|n0}

Rina Zviel-Girshin @ASC


From a grammar to a cfl1
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


From a grammar to a cfl2
From a grammar to a CFL

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

    S  0A | 0B

    A1S

    B1

  • L(G) = {(01)n |n1 }

Rina Zviel-Girshin @ASC


From a grammar to a cfl3
From a grammar to a CFL

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

    S  0S11 | 0

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

Rina Zviel-Girshin @ASC


From a grammar to a cfl4
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


From a grammar to a cfl5
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


Question
Question

  • Can any finite language be constructed by a grammar?

     Yes.

    Proof:

  • Let L={wi | in and wi*} be a finite language over .

  • We construct the following grammar:

    Sw1

    Sw2

    ..

    Swn

Rina Zviel-Girshin @ASC


Question cont
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


Ambiguity
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


Example
Example

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

EE+E | E*E | T

T0|1|2|..|9 

Rina Zviel-Girshin @ASC


Example cont
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


The conclusion
The conclusion

  • The conclusion:

    • programming languages should have a unique interpretation

      or

    • the grammar of the programming language would be unambiguous.

Rina Zviel-Girshin @ASC


Complexity and computability theory i

Any Questions?

Rina Zviel-Girshin @ASC