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 Context Free Grammars (CFG) Parse trees and ambiguity CFG Design Push Down Automata Arithmetic


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

  • Context Free Grammars (CFG)
  • Parse trees and ambiguity
  • CFG Design
  • Push Down Automata
slide-3
SLIDE 3

Arithmetic Expressions

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

S → S+S | S*S | S^N | N | (S) 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-4
SLIDE 4

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

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

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

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

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

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

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

Unambiguous Grammar for Exp

  • G = ({E,T,F},{0,…,9,+,*,^,(,)},R,E) where R is the

set of rules:

E → E+T | T T → T*F | F F → F^N | N | (E) N → 0 | … | 9

  • Parse/Evaluate

1+2*3^2+5

E N F + T E 5 + T E 1 * T E F

slide-12
SLIDE 12

Recap: Context-free languages

Context-free grammar One-step derivation Derivation Language generated by grammar

slide-13
SLIDE 13

CFGs and Automata

  • L is a context free language (CFL) if L=L(G) for

some CFG G

  • What is the relation between CFL and regular

languages?

Can any DFA/Regex be transformed into an equivalent CFG?

Not all CFL can be recognized by a DFA/NFA!

How can we extend DFAs/NFAs to make them as powerful as CFGs?

slide-14
SLIDE 14

From Regex to CFG

  • Approach via closure properties:

Show that {a}, {ε} and {} are context free languages

Show that the class of context free languages is closed under union, concatenation and star

See Haskell 3 for details

  • It follows that for any regular expression E, there

is a CFG for L(E)

slide-15
SLIDE 15

From DFA to CFG

  • Claim: For any DFA M=(Q,Σ,δ,s,F) there is a CFG

G=(V,Σ,R,S) such that L(M)=L(G)

  • Proof:

Idea: trace computation using variables to denote state

V=Q

S=s

R={q → a δ(q,a) | q ∊Q, a∊Σ} U {q → ε | q ∊ F}

slide-16
SLIDE 16

Context-free languages Context-free languages

Regular languages vs. CFL

Regular languages Regular languages

slide-17
SLIDE 17

An alternative …

Sipser p. 109

  • NFA + stack
slide-18
SLIDE 18

Pushdown automata

Sipser p. 109

  • NFA + stack

At each step

  • 1. Transition to new state

based on current state, letter read, and top letter of stack.

  • 2. (Possibly) push or pop a

letter to (or from) top of stack

slide-19
SLIDE 19

Pushdown automata

Sipser p. 109

  • NFA + stack

Accept a string if there is some sequence of states and some sequence

  • f stack contents which

processes the entire input string and ends in an accepting state.

slide-20
SLIDE 20

State diagram for PDA

Sipser p. 109

If hand-drawn or in Sipser State transition labelled a, b → c means "when machine reads an a from the input and the top symbol of the stack is a b, it may replace the b with a c." In JFLAP: use ; instead of →

slide-21
SLIDE 21

State diagram for PDA

Sipser p. 109

If hand-drawn or in Sipser State transition labelled a, b → c means "when machine reads an a from the input and the top symbol of the stack is a b, it may replace the b with a c."

What edge label would indicate “Read a 0, don’t pop anything from stack, don’t push anything to the stack”? A) 0, ε→ε B) ε, 0→ε C) ε,ε→0 D) ε→ε,0 E) I don’t know

slide-22
SLIDE 22

Useful trick

  • What would ε,ε → $ mean?

A) Without reading any input or popping any symbol from stack, push $ B) If the input string and stack are both empty, push $ C) At the end of reading the input, push $ D) I don’t know

Why is this useful? Commonly used from initial state (at start of computation) to record bottom of stack with a special

  • symbol. …. Useful to check if stack becomes empty

again.

slide-23
SLIDE 23

Formal definition of PDA

Sipser Def 2.13 p. 111

slide-24
SLIDE 24

Designing a PDA

L = { 0i1i+1 | i ≥ 0 } Informal description of PDA: Read symbols from the input. As each 0 is read, push it onto the

  • stack. As soon as 1s are seen, pop a 0 off the stack for each 1
  • read. If the stack becomes empty and there is exactly one 1 left

to read, read that 1 and accept the input. If the stack becomes empty and there are either zero or more than one 1s left to read,

  • r if the 1s are finished while the stack still contains 0s, or if any

0s appear in the input following 1s, reject the input.

slide-25
SLIDE 25

Designing/Tracing a PDA

L = { 0i1i+1 | i ≥ 0 }

slide-26
SLIDE 26

PDAs and CFGs are equivalently expressive

Theorem 2.20: A language is context-free if and only some nondeterministic PDA recognizes it.

Consequences

  • Quick proof that every regular language is context free
  • To prove closure of class of CFLs under a given operation, can choose two

modes of proof (via CFGs or PDAs) depending on which is easier

slide-27
SLIDE 27

For next time

  • Read Sipser 2.1, 2.2
  • Design a few CFGs and PDAs on your own