CSE 105 Theory of Computation

Preview:

DESCRIPTION

CSE 105 Theory of Computation. Alexander Tsiatas Spring 2012. Theory of Computation Lecture Slides by Alexander Tsiatas is licensed under a Creative Commons Attribution- NonCommercial - ShareAlike 3.0 Unported License. Based on a work at http://peerinstruction4cs.org. - PowerPoint PPT Presentation

Citation preview

CSE 105Theory of

Computation

Alexander TsiatasSpring 2012

Theory of Computation Lecture Slides by Alexander Tsiatas is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Based on a work at http://peerinstruction4cs.org.Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org.

From Preliminary Exam 1

• Regular languages are closed under union.

• If L1 and L2 are regular, then is L1 U L2 regular?

– a) true– b) false

From Preliminary Exam 1

• Regular languages are closed under union.

• If L1 U L2 is regular, are L1 and L2 both regular?

– a) true– b) false

From Preliminary Exam 1

• Direction Matters.

• “If P, then Q” does NOT mean “if Q, then P”.– It does mean “If not Q, then not P”, which is how

we use the pumping lemma to prove that languages are not regular.

• Direction Matters even more, later in the course.

CONTEXT-FREE GRAMMARSCFGS

Back to your [English/Spanish/French/…] class

Context-free Grammars (CFG)

• Context-free grammars are one way that scholars have tried to model how language, and our brain’s models of language, might be structured– Key concept: representing and generating infinite

complexity, with just a few simple rules

What is the language of this CFG?

S → 0SS → 0

a) {0}b) {0, 00, 000, …}c) {ε, 0, 00, 000, …}d) None of the above

The LANGUAGE of a CFG

• The set of strings that can be generated following the given production rules.

• Every string generated is in the language, and every string that cannot be generated is not in the language.

What is the language of this CFG?

S → 0SS → 1SS → ε

a) Same as the regular expression 0*1*b) Same as the regular expression 0* U 1*c) Same as the regular expression (0 U 1)*d) None of the above

What is the language of this CFG?S → T0TT → TTT → 0T1T → 1T0T → 0T → ε

a) All strings of 0’s and 1’sb) All strings of 0’s and 1’s with at least one 0 in itc) All strings of 0’s and 1’s with at least two 0’s in itd) All strings of 0’s and 1’s that have the same number of 0’s and 1’se) All strings of 0’s and 1’s that have more 0’s than 1’s

Are CFG’s more powerful than DFA’s?

• In other words: is there a CFG G such that the language of G is not regular?

a) Yesb) No

Demonstrating the power of CFG’s

• The language L = {0n 1n | n ≥ 0} is NOT regular. (Why not?)

• Which of these grammars describes the language L?

a) S → AB A → 0A | ε B → 1B | ε

d) S → 01S01 | ε

b) S → AB A → 0A1 | ε B → 0B1 | ε

c) S → 0S1 | ε

e) There is no CFG that describes L.

Why do we care about CFG’s?

• You write a Java program but forget a semicolon. How does the compiler catch it?

• You type (5+81(*24) in your fancy TI-89 calculator. How does it not blow up?

• You want to recursively define a tree datatype in O’Caml. How do you do this?

• You pull out your iPhone and ask Siri “When is the Sungod festival this year?” How does Siri have any hope of figuring out what you’re asking?

Why do we care about CFG’s?

• All of those situations (and more!) can be handled using CFG’s and parsing a string to see if it can be generated by a CFG.

• Programming languages are often specified as a grammar.

• There are programs (yacc) that can build parsers out of grammars.

• Doesn’t tell you anything about the meaning of the string. This is why they are “context-free” languages.

Sometimes a string can be parsed two different ways.

S → NP VPNP → CN | CN PPVP → CV | CV PPPP → P CNCN → A NCV → V | V NPA → a | theN → boy | girl | flowerV → touches | likes | seesP → with

S = SentenceNP = Noun-PhraseVP = Verb-PhrasePP = Prep-PhraseCN = Complex-NounCV = Complex-VerbA = ArticleN = NounV = VerbP = Preposition

Parse Tree

• “The girl touches the boy with the flower.”(a) (b)

S → NP VPNP → CN | CN PPVP → CV | CV PPPP → P CNCN → A NCV → V | V NPA → a | theN → boy | girl | flowerV → touches | likes | seesP → with

Parse Tree 2

• “The girl touches the boy with the flower.”(a) (b)

S → NP VPNP → CN | CN PPVP → CV | CV PPPP → P CNCN → A NCV → V | V NPA → a | theN → boy | girl | flowerV → touches | likes | seesP → with

Ambiguity

Recommended