Loading in 5 sec....

Complexity and Computability Theory IPowerPoint Presentation

Complexity and Computability Theory I

- 101 Views
- Uploaded on

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

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

- 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

- 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

- 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

- ={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

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

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

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

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

Rina Zviel-Girshin @ASC

Parsing w=aacb (cont.)

Rina Zviel-Girshin @ASC

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:

ab, aV and b(VU)*

Rina Zviel-Girshin @ASC

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

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 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?

- 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}

- 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,1}

- Construct a grammar for the following language L = {0n1n |n0}
- G = (V={S},={0,1},S, R) where R:
S0S1

S

or

S0S1 |

Rina Zviel-Girshin @ASC

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

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,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

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

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,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

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

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

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

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

- 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

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

- 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

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

EE+E | E*E | T

T0|1|2|..|9

Rina Zviel-Girshin @ASC

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:
- programming languages should have a unique interpretation
or

- the grammar of the programming language would be unambiguous.

- programming languages should have a unique interpretation

Rina Zviel-Girshin @ASC

Rina Zviel-Girshin @ASC

Download Presentation

Connecting to Server..