CSE 105 THEORY OF COMPUTATION Fall 2016 - - PowerPoint PPT Presentation

cse 105
SMART_READER_LITE
LIVE PREVIEW

CSE 105 THEORY OF COMPUTATION Fall 2016 - - PowerPoint PPT Presentation

CSE 105 THEORY OF COMPUTATION Fall 2016 http://cseweb.ucsd.edu/classes/fa16/cse105-abc/ Today's learning goals Sipser Ch 1.4, 2.1 Non-regular languages Define context-free grammars Test if a specific string can be generated by a


slide-1
SLIDE 1

CSE 105

THEORY OF COMPUTATION

Fall 2016 http://cseweb.ucsd.edu/classes/fa16/cse105-abc/

slide-2
SLIDE 2

Today's learning goals

Sipser Ch 1.4, 2.1

  • Non-regular languages
  • Define context-free grammars
  • Test if a specific string can be generated by a given

context-free grammar

  • CFG Design
slide-3
SLIDE 3

Context-free grammar

Sipser Def 2.2, page 102

(V, Σ, R, S) Variables: finite set of (usually upper case) variables V Terminals: finite set of alphabet symbols Σ Rules / Productions: finite set of allowed moves R Start variable: S

slide-4
SLIDE 4

Notation: S ⇒* w Terminology: sequence of rule applications is derivation Notation: S ⇒* w Terminology: sequence of rule applications is derivation

Context-free language

Sipser p. 104

The language of a CFG (V, Σ, R, S) is { w in Σ* | Starting with the Start variable and applying zero or more rules, can get to w}

slide-5
SLIDE 5

Example CFG

  • G=(V={S},T={0,1},R,S) where the rules R are

1) S → 0S1 2) S → ε

  • Derivation:

– S → 0S1 → 00S11 → 000S111 → 000111

  • L(G)={0n1n}
slide-6
SLIDE 6

An example?

Consider the CFG ({S}, {0}, R, S) where R is the following set of rules S →0S S →0 Is this a well-formed defjnition?

  • A. No: there's more than one rule
  • B. No: the same LHS gets sent to two difgerent strings.
  • C. No: one of the string in the RHS has both variables and literals
  • D. Yes.
  • E. I don't know.
slide-7
SLIDE 7

Context-free language

Sipser p. 104

The language of a CFG (V, Σ, R, S) is { w in Σ* | Starting with the Start variable and applying zero or more rules, can get to w} What is the language of the CFG ({S}, {0}, R, S) with R = {S→0S, S→0} ?

  • A. {0}
  • B. {0, 0S} C. {0, 00, 000, …}
  • D. {ε, 0, 00, 000, …}
  • E. I don't know.
slide-8
SLIDE 8

Context-free language

Sipser p. 104

What is the language of the CFG ({S}, {0,1}, R, S) with R = the set of rules S →0S S→1S S→ε

  • A. L(0*1*)
  • B. L(0* U 1*) C. L( (0 U 1) *)
  • D. L ( (0*1*) )*
  • E. I don't know.

S → 0S | 1S | ε S → 0S | 1S | ε

slide-9
SLIDE 9

Context-free language

Sipser p. 104

What is the language of the CFG ({S}, {0,1}, R, S) with R = the set of rules S → 0S | T T → 1T | ε

  • A. L(0*1*)
  • B. L(0* U 1*) C. L( (0 U 1) *)
  • D. L ( (0*1*) )*
  • E. I don't know.
slide-10
SLIDE 10

Context-free language

Sipser p. 104

What is the language of the CFG ({S}, {0,1}, R, S) with R = the set of rules S → 0S | T T → 1T | S

  • A. L(0*1*)
  • B. L(0* U 1*) C. L( (0 U 1) *)
  • D. L ( (0*1*) )*
  • E. {}
slide-11
SLIDE 11

Questions about CFG

  • What languages can be described by a CFG?
  • Do CFG correspond to some “executable”

computational model?

  • Is the class of languages described by CFGs

closed under union, concatenation, star, intersection, complement?

  • Are there languages that cannot be described by

a CFG?

slide-12
SLIDE 12

Arithmetic Expressions

  • (3+12)*(5+7+(5*63^2))
  • Grammar rules:

S → S+S | S*S | S^N | N N → D | DN D → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

  • Derivations

S → DN → 1N → 1DN→ 12N→ 12D → 123

S → S+S → 1+S → 1+S*S →* 1+2*S →* 1+2*3

slide-13
SLIDE 13

Parse trees and Leftmost derivations

  • 1+2*3
  • S → S+S → S+S*S →* S+2*3 →* 1+2*3
  • S → S+S →* 1+S → 1+S*S →* 1+2*3 (leftmost)

S S + S 1 S * S 2 3

slide-14
SLIDE 14

Parse trees and Leftmost derivations

  • 1+2*3
  • S → S+S → S+S*S →* S+2*3 →* 1+2*3
  • S → S+S →* 1+S → 1+S*S →* 1+2*3 (leftmost)

S S + S 1 S * S 2 3

  • For each derivation, there is a unique parse three
  • For each parse tree, there is a unique leftmost derivation
slide-15
SLIDE 15

Ambiguity

  • S → S+S → S+S+S → 1+2+3
  • S → S+S → 1+S → 1+S+S → 1+2+3

S S + 3 1 + 2 S 1 + S 2 + 3

slide-16
SLIDE 16

Ambiguity

  • S → S+S → S+S+S → 1+2+3
  • S → S+S → 1+S → 1+S+S → 1+2+3

S S + 3 1 + 2 S 1 + S 2 + 3 (1+2)+3 1+(2+3)

slide-17
SLIDE 17

Ambiguity

  • S → S+S → S+S+S → 1+2+3
  • S → S+S → 1+S → 1+S+S → 1+2+3

S S * 3 1 + 2 S 1 + S 2 * 3 (1+2)*3 = 9 1+(2*3) = 7

slide-18
SLIDE 18

Unambiguous Grammars

  • A CFG G is unambiguous if every w in L(G) has a

unique parse tree (or, equivalently, a unique leftmost derivation)

  • A CFG is ambiguous if there exists a w in L(G)

such that w has two difgerent parse trees (or, equivalently, two difgerent leftmost derivations)

slide-19
SLIDE 19

Test time

  • Is the following grammar ambiguous?

S → S+S | S*S | 0 | 1 A) Yes B) No C) It depends on the input string D) There is no answer because the question is ambiguous E) I don’t know

slide-20
SLIDE 20

For next time

  • Can you give an unambiguous grammar for the

language of arithmetic expressions?

  • Closure properties of CFG
  • Push Down Automata