Presentation is loading. Please wait.

Presentation is loading. Please wait.

January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.

Similar presentations


Presentation on theme: "January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015."— Presentation transcript:

1 January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015

2 CS21 Lecture 52 Outline Pushdown Automata Context-Free Grammars and Languages –parse trees –ambiguity –normal form equivalence of NPDAs and CFGs

3 January 14, 2015CS21 Lecture 53 Pushdown Automata 0 11001 1 10 1 00101 q0q0 input tape finite control 0 1 1 0 : (infinite) stack New capabilities: can push symbol onto stack can pop symbol off of stack

4 January 14, 2015CS21 Lecture 54 Pushdown Automata 0 01101 1 10 1 00101 q0q0 input tape finite control $ : (infinite) stack

5 January 14, 2015CS21 Lecture 55 Pushdown Automata 0 01101 1 10 1 00101 q1q1 input tape finite control 0 : (infinite) stack $

6 January 14, 2015CS21 Lecture 56 Pushdown Automata 0 01101 1 10 1 00101 q1q1 input tape finite control 0 : (infinite) stack $ 0

7 January 14, 2015CS21 Lecture 57 Pushdown Automata 0 01101 1 10 1 00101 q2q2 input tape finite control 0 : (infinite) stack $

8 January 14, 2015CS21 Lecture 58 Pushdown Automata 0 01101 1 10 1 00101 q2q2 input tape finite control $ : (infinite) stack Note: often start by pushing $ marker onto stack so that we can detect “empty stack”

9 January 14, 2015CS21 Lecture 59 Pushdown Automata (PDA) We will define nondeterministic pushdown automata immediately –potentially several choices of “next step” Deterministic PDA defined later –weaker than NPDA Two ways to describe NPDA –diagram –formal definition

10 January 14, 2015CS21 Lecture 510 NPDA diagram tape alphabet Σ stack alphabet  ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε states start state accept states transitions transition label: (tape symbol read, stack symbol popped → stack symbol pushed)

11 January 14, 2015CS21 Lecture 511 NPDA operation Taking a transition labeled: a, b → c –a  (Σ  {ε}) –b,c  (   {ε}) –read a from tape, or don’t read from tape if a = ε –pop b from stack, or don’t pop from stack if b = ε –push c onto stack, or don’t push onto stack if c = ε

12 January 14, 2015CS21 Lecture 512 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 1100 Stack contents: $

13 January 14, 2015CS21 Lecture 513 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 1100 Stack contents: 0$

14 January 14, 2015CS21 Lecture 514 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 1100 Stack contents: 0$0

15 January 14, 2015CS21 Lecture 515 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 1100 Stack contents: 0$0

16 January 14, 2015CS21 Lecture 516 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 1100 Stack contents: 0$

17 January 14, 2015CS21 Lecture 517 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 1100 Stack contents: $ accepted

18 January 14, 2015CS21 Lecture 518 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 100 Stack contents: $

19 January 14, 2015CS21 Lecture 519 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 100 Stack contents: 0$

20 January 14, 2015CS21 Lecture 520 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 100 Stack contents: 0$0

21 January 14, 2015CS21 Lecture 521 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 100 Stack contents: 0$0

22 January 14, 2015CS21 Lecture 522 Example NPDA tape: ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $} 100 Stack contents: 0$ not accepted

23 January 14, 2015CS21 Lecture 523 Example NPDA What language does this NPDA accept? ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε Σ = {0, 1}  = {0, 1, $}

24 January 14, 2015CS21 Lecture 524 Formal definition of NPDA A NPDA is a 6-tuple (Q, Σ, , δ, q 0, F) where: –Q is a finite set called the states –Σ is a finite set called the tape alphabet –  is a finite set called the stack alphabet –δ:Q x (Σ  {ε}) x (   {ε}) →  (Q x (   {ε})) is a function called the transition function –q 0 is an element of Q called the start state –F is a subset of Q called the accept states

25 January 14, 2015CS21 Lecture 525 Formal definition of NPDA NPDA M = (Q, Σ, , δ, q 0, F) accepts string w  Σ* if w can be written as w 1 w 2 w 3 …w m  (Σ  {ε})*, and there exist states r 0, r 1, r 2, …, r m, and there exist strings s 0, s 1, …, s m in (  {ε})* –r 0 = q 0 and s 0 = ε –(r i+1, b)  δ(r i, w i+1, a), where s i = at, s i+1 = bt for some t   * –r m  F

26 January 14, 2015CS21 Lecture 526 Example of formal definition Q = {q 0, q 1, q 2, q 3 } Σ = {0,1}  = {0, 1, $} F = {q 0, q 3 } ε, ε → $ ε, $ → ε 1, 0 → ε 0, ε → 0 1, 0 → ε δ(q 0, ε, ε) = {(q 1, $)} δ(q 1, 0, ε) = {(q 1, 0)} δ(q 1, 1, 0) = {(q 2, ε)} δ(q 2, 1, 0) = {(q 2, ε)} δ(q 2, ε, $) = {(q 3, ε)} q0q0 q1q1 q3q3 q2q2 other values of δ(,, ) equal {}

27 January 14, 2015CS21 Lecture 527 Exercise Design a NPDA for the language {a i b j c k : i, j, k ≥ 0 and i = j or i = k}

28 January 14, 2015CS21 Lecture 528 Context-free grammars and languages languages recognized by a (N)FA are exactly the languages described by regular expressions, and they are called the regular languages languages recognized by a NPDA are exactly the languages described by context-free grammars, and they are called the context-free languages

29 January 14, 2015CS21 Lecture 529 Context-Free Grammars A → 0A1 A → B B → # start symbol terminal symbols non-terminal symbols production

30 January 14, 2015CS21 Lecture 530 Context-Free Grammars generate strings by repeated replacement of non-terminals with string of terminals and non-terminals –write down start symbol (non-terminal) –replace a non-terminal with the right-hand- side of a rule that has that non-terminal as its left-hand-side. –repeat above until no more non-terminals

31 January 14, 2015CS21 Lecture 531 Context-Free Grammars Example: A  0A1  00A11  000A111  000B111  000#111 a derivation of the string 000#111 set of all strings generated in this way is the language of the grammar L(G) called a Context-Free Language A → 0A1 A → B B → #

32 January 14, 2015CS21 Lecture 532 Context-Free Grammars Natural languages (e.g. English) have this sort of structure: → → | → → | → a | the → dog | cat | flower → eats | sees → with Generate a string in the language of this grammar. shorthand for multiple rules with same lhs

33 January 14, 2015CS21 Lecture 533 Context-Free Grammars CFGs don’t capture natural languages completely computer languages often defined by CFG –hierarchical structure –slightly different notation often used “Backus- Naur form” –see next slide for example

34 Example CFG → | | | → IF THEN ELSE → WHILE DO → BEGIN END → | ; → := → → | ≤ | ≥ | = → | | ( ) → + | - | * | / → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 → a | b | c | … | x | y | z January 14, 201534CS21 Lecture 5

35 January 14, 2015CS21 Lecture 535 CFG formal definition A context-free grammar is a 4-tuple (V, Σ, R, S) where –V is a finite set called the non-terminals –Σ is a finite set (disjoint from V) called the terminals –R is a finite set of productions where each production is a non-terminal and a string of terminals and non- terminals. –S  V is the start variable (or start non-terminal)

36 January 14, 2015CS21 Lecture 536 CFG formal definition u, v, w are strings of non-terminals and terminals, and A → w is a production: “uAv yields uwv”notation: uAv  uwv also: “yields in 1 step” notation: uAv  1 uwv in general: “yields in k steps” notation: u  k v –meaning: there exists strings u 1,u 2,…u k-1 for which u  u 1  u 2  …  u k-1  v

37 January 14, 2015CS21 Lecture 537 CFG formal definition notation: u  * v –meaning:  k ≥ 0 and strings u 1,…,u k-1 for which u  u 1  u 2  …  u k-1  v if u = start symbol, this is a derivation of v The language of G, denoted L(G) is: {w  Σ* : S  * w}

38 January 14, 2015CS21 Lecture 538 CFG example Balanced parentheses: –( ) –( ( ) ( ( ( ) ( ) ) ) ) a string w in Σ* = { (, ) }* is balanced iff: –# “(”s equals # “)”s, and –for any prefix of w, # “(”s ≥ # “)”s Exercise: design a CFG for balanced parentheses.


Download ppt "January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015."

Similar presentations


Ads by Google