introduction to the theory of computation

23
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003

Upload: oistin

Post on 12-Jan-2016

24 views

Category:

Documents


2 download

DESCRIPTION

Introduction to the Theory of Computation. John Paxton Montana State University Summer 2003. Humor. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to the Theory of Computation

Introduction to the Theory of Computation

John Paxton

Montana State University

Summer 2003

Page 2: Introduction to the Theory of Computation

Humor

• A foreign visitor was being given a tour of Washington, D.C. one day by an American friend of hers. She was amazed at the size of the Monuments, the Congressional Buildings, and so forth. Finally she gazed upon the White House itself.              "My, that's an incredibly large building!" she remarked.              "Yes, it's pretty big, alright." said her friend.              "Big? It's huge!! About how many people work in there?" she asked.              "Oh... about half."

Page 3: Introduction to the Theory of Computation

Chapter 2: Context-Free Languages

• Applications: Capture most human language syntax, programming language syntax, the parser in a compiler

• More powerful model of computation

Page 4: Introduction to the Theory of Computation

2.1 Context-Free Grammars

• A grammar consists of– production rules– variables– terminals– a starting variable

A => 0A1 | #

Page 5: Introduction to the Theory of Computation

Derivation

• A => 0A1 => 00A11 => 00#11

Page 6: Introduction to the Theory of Computation

Parse Tree

A

0 A 1

0 A 1

#

Page 7: Introduction to the Theory of Computation

Natural Language

• <sentence> => <noun phrase> <verb phrase>

• <noun phrase> => <noun> | <determiner> <noun>

• <verb phrase> => <verb>

• <det> => a | the

• <noun> => boy | girl

• <verb> => runs | plays

Page 8: Introduction to the Theory of Computation

Context Free Grammar Definition

A CFG is a 4-tuple (V, , R, S) where

1. V is a finite set called the variables

2. is a finite set, disjoint from V, called the terminals

3. R is a finite set of rules, with each rule being a variable and a string of variables and terminals

4. S is the start symbol

Page 9: Introduction to the Theory of Computation

Exercise

• Identify (V, , R, S) for A => 0A1 | #

• Describe the language

Page 10: Introduction to the Theory of Computation

More Definitions

• uAv yields uwv if A => w is a rule

• uAv =>* uwv if a sequence of 1 or more steps exist such that A => w

• The language of the grammer is { w * | S =>* w }

Page 11: Introduction to the Theory of Computation

Another Grammar

• <expr> => <expr> + <term> | <term>

• <term> => <term> x <factor> | <factor>

• <factor> => ( <expr> ) | a

• Exercise: Parse tree for a + a x a

• Exercise: Parse tree for (a + a) x a

Page 12: Introduction to the Theory of Computation

Exercises

• Design a context free grammar for the language {0n1n | n >= 0} U {1n0n | n >= 0}

• Design a context free grammar for the below NFA

1

0

Page 13: Introduction to the Theory of Computation

Exercises

• Give a CFG that generates the language {w | w contains at least three 1s} over the alphabet {0,1}.

• Repeat the above question for {w | w contains more 1s than 0s}

• Repeat the above question for {w | w contains twice as many a’s as b’s} over the alphabet {a, b}

Page 14: Introduction to the Theory of Computation

Ambiguity

• <expr> => <expr> + <expr> | <expr> x <expr> | a

• A string w is derived ambiguously in a CFG if it has two or more different leftmost derivations. A CFG is ambiguous if it generates some string ambiguously.

• Exercise: Show a + a x a is ambiguous.

Page 15: Introduction to the Theory of Computation

Chomsky Normal Form

All rules are in one of the following forms:

1. S => is allowed (S is the start symbol)

2. A => B C (B and C aren’t S)

3. A => a

Page 16: Introduction to the Theory of Computation

Theorem

• Any context-free language is generated by a context-free grammar in Chomsky normal form.

Page 17: Introduction to the Theory of Computation

Proof

• Add new start symbol S0

• Add S0 => S • Eliminate an rule, adjust other rules.• Repeat above step until all rules

eliminated except for possible S0 =>

Example: A => , B => aAaAa becomesB => aAaAa | aaAa | aAaa

Page 18: Introduction to the Theory of Computation

Proof

• Remove a unit rule A => B.

Example: A => B, C => AA becomes

C => BB

• Repeat until all unit rules removed.

Page 19: Introduction to the Theory of Computation

Proof

• Convert all remaining rules into proper form.

Example: A => aBcD becomes

A => U1U2 U1 => a

U2 => BU3

U3 => U4D U4 => c

Page 20: Introduction to the Theory of Computation

Example

• S => ASA | b | • A => a

• S0 => S (add new start rule)

• S => ASA | b | • A => a

Page 21: Introduction to the Theory of Computation

Example

• S0 => S |

• S => AA | ASA | b (eliminate S => )• A => a

• S0 => AA | ASA | b | (eliminate S0 => S)

• S => AA | ASA | b

• A => a

Page 22: Introduction to the Theory of Computation

Example

• S0 => AA | AU1 | b | (change S0 => ASA)

• S => AA | AU1 | b (change S => ASA)

• A => a

• U1 => SA

Page 23: Introduction to the Theory of Computation

Exercise

• Convert the following grammar to Chomsky Normal Form:

• A => BAB | B | • B -> 00 |