Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Push Down Automata Lecture 6-7 Ref. Handout p29-33, 25-28.

Similar presentations


Presentation on theme: "1 Push Down Automata Lecture 6-7 Ref. Handout p29-33, 25-28."— Presentation transcript:

1 1 Push Down Automata Lecture 6-7 Ref. Handout p29-33, 25-28

2 2 A Stack of Chairs Only the top chair is accessible. If a new chair is added, it can be only added at the top. When removing a chair, we must from it from the top. The last chair added will be the first one removed.

3 3 Stacks – an abstract data type x hidden values top Operations: push(z) - put z on top of stack pop -discard top of stack

4 4 Stacks and Interrupts Watching Video Phone rings Knock at door Toast catches fire Interrupted by something more urgent Another name for stacks: last-in-first-out (LIFO)

5 5 Stacks Stacks are used extensively at every level of a modern computer system. For example, evaluating an expression x = 3*(4/(2+2)); In programming...... x = get_requests(); process(x);...... get_request() { connect(); read(); } connect(){ open(); check()...} Need a stack to remember the back points 1 2 3

6 6 Push Down Automata (PDA) PDA = FA + a stack A State Transition with a Stack a,x / push(y) a,x / pop a – input symbol x – value at the top of the stack A A B B

7 7 Comparing with FA Transitions in FA Transitions in PDA a a,x / op state new state new state a – the input symbol x – the value at the top of stack op – the stack operation read a, move to the new state read a, the top of stack is x, perform op, then move to the new state

8 8 Some extras... a, x / nopdo not perform the stack operation a, _ / opdo the operation whatever is on top of the stack Λ, x / opwhatever the input is... (do not ‘consume’ the input)

9 9 Initialising a Stack We need to know whether a stack is empty This is handled by initialising a special value onto the stack. So as long as the special value is seen at the top of the stack, we know that the stack is empty start Λ,_/ push(Z) real start push(Z) real start or simply

10 10 A Push Down Automata push(Z) a,_/push(a)b,a/pop Λ,Z/pop { a n b n | n >0 } = {ab, aabb, aaabbb, aaaabbbb,...}

11 11 Operation of the Push Down Automata push(Z) a,_/push(a)b,a/pop Λ,Z/pop Input string: aaabbb

12 12 Operation of the Push Down Automata - examples of rejecting a string push(Z) a,_/push(a)b,a/pop Λ,Z/pop Illegal strings: abb aab

13 13 Another Push Down Automata push($) a,_/push(a)b,a/nop Λ,$/pop How about { (a n b 2n ) m | n>0, m>0} ? {a n b 2n | n>0} b,a/pop

14 14 Another Push Down Automata push($) a,_/push(a)b,a/nop Λ,$/pop How about { (a n b 2n ) m | n>0, m>0} ? {a n b 2n | n>0} b,a/pop a,$/push(a)

15 15 Extending an FA to a PDA From {1 n | n>0} 1 1 To { 1,11,111,...(1),1(11),((1)),(1)(1),((11))1,((1)(1)),... }

16 16 Memo for In-class test 6 [ /5] questionsmy answers correct answers comments 1 2 3 4 5

17 17 Determinism v Non-Determinism Deterministic Transitions a,X/op2 a,Y/op1 b,X/op3 Λ, Z/op3

18 18 Determinism v Non-Determinism Non-Deterministic Transitions a,X/op2 a,X/op1 Λ,X/op3

19 19 Non-Determinism PDA Recall FA – every NFA can be converted to a DFA which accepts the same language. NFA == DFA Can we do same for Push Down Automata? The answer is NO. NPDA \== DPDA

20 20 An Example to Show NPDA\==DPDA A palindrome is a string which read the same left to right as right to left. A deterministic PDA cannot recognise palindromes. With a stack we can push symbols onto the stack until we reach the middle of the string, then pop the stack against the rest of the palindrome. However, there is no way we can tell where is the middle!

21 21 A Non-deterministic PDA for Palindromes push(z) b,b/pop a,a/pop a,_/push(a) b,_/push(b) b,b/pop a,a/pop Λ,z/pop Here we assume palindromes involving only two symbols a and b

22 22 Regular Expressions Suppose you want to search for strings Mr Smith or Mr. Smith or Mr Smith or Mr. Smith or Mr J Smith or Mr. J. Smith or...... A very big file

23 23 We can use a regular expression (instead of drawing a FA) Mr “.” ? “ ” ∗ (J|J “.”) ? “ ” ∗ Smith Mr “.” ? optionally a “.” “ ” ∗ 0 or more spaces “ ” ∗ 0 or more spaces (J|J“.”)? optionally J or J. Smith

24 24 A Subset of Regular Expressions symbolmeaningexampleexplanation |Alternatives (logical or) a | ba or b ∗ repeat the last item 0 or more times a ∗ 0 or more occurrences of a +repeat the last item 1 or more times a + 1 or more occurrences of a [ ]any character in [ ][xyz] [a-z] any of x,y or z any from a-z ( )brackets(a|c)cgrouping

25 25 Some Examples 1 ∗ = ab ∗ = 1 ∗ | 0 ∗ = (1|0) ∗ = xy ∗ | yz ∗ = [a-z][a-z0-9] =

26 26 Where are Regular Expressions Used? grep(unix) program for searching files Perlused with programming language editorsstring searching lex program for generating pattern matching programs

27 27 Regular Expressions = NFAs = DFAs abcd ∗ | e ∗ design an FA to accept abcd ∗ e∗e∗ combine them (convert to DFA)

28 28 Regular Expressions, Regular Languages, and Finite Automata Regular expressions have the equal power as finite automata The languages accepted by finite automata are called regular languages FA = Regular Language Examples of regular languages: names, numbers, arithmetic expression without brackets

29 29 Context Free Languages and PDA Languages CNANOT be accepted by FA, but CAN be accepted by PDA are classified as context free languages Context free languages form the basis of all the languages used in computing Regular languages are subset of context free language regular languages (FA) context free (PDA) languages

30 30 Memo for In-class test 7 [ /5] questionsmy answers correct answers comments 1 2 3 4 5


Download ppt "1 Push Down Automata Lecture 6-7 Ref. Handout p29-33, 25-28."

Similar presentations


Ads by Google