Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010.

Similar presentations


Presentation on theme: "1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010."— Presentation transcript:

1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010

2 2 Content Midterm again Context-Free Languages Push-Down Automata, PDA NPDA: Non-Deterministic PDA Formal Definitions for NPDAs NPDAs Accept Context-Free Languages Converting NPDA to Context-Free Grammar

3 Midterm 5 out of 13 passed… Many failed on basic definitions Is something wrong with the course format? Scheduled exercises Don’t hesitate to ask for help! Do labs, exercises etc. Review lecture material before lectures Strongly recommended to get a book if you don’t have one It doesn’t get easier… 3

4 4 JFLAP demo Concerning examination in the course: Exercises are voluntary Labs are voluntary Midterms are voluntary Lectures are voluntary… All of them are recommended! http://www.cs.duke.edu/csed/jflap/movies

5 5 Regular Languages Context-Free Languages Non-regular languages

6 6 Context-Free Languages

7 7 Pushdown Automata Context-Free Grammars stack automaton (CF grammars are defined as generalized Regular Grammars)

8 8 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables

9 9 Pushdown Automata PDAs

10 10 Pushdown Automaton - PDA Input String Stack States

11 11 The Stack The stack allows pushdown automata to recognize some non-regular languages. All access to the stack only on the top! (Stack top is written leftmost in the string, e.g. yxz) A PDA can write symbols on stack and read them later on. A stack is valuable as it can hold an unlimited amount of (certain kind of) information. POP reading symbol PUSH writing symbol

12 12 The States Input symbol Pop old - reading stack symbol Push new - writing stack symbol

13 13 top input stack Replace (An alternative is to start and finish with empty stack or a stack bottom symbol such as $)

14 14 input Push top stack

15 15 input Pop top stack

16 16 input No Change top stack

17 17 NPDAs Non-deterministic Push-Down Automata

18 18 Non-Determinism

19 19 A string is accepted if: All the input is consumed The last state is a final state Stack is in the initial condition (either: empty (when we started with empty stack), or: bottom symbol reached, or similar)

20 20 is the language accepted by the NPDA: Example NPDA

21 21 NPDA M (Even-length palindromes) Example NPDA

22 22 Pushing Strings Input symbol Pop symbol Push string

23 23 top input stack Push pushed string Example

24 24 Another NPDA example NPDA M

25 25 Time 0 Input Stack Current state Execution Example

26 26 Input Stack Time 1

27 27 Input Stack Time 2

28 28 Input Stack Time 3

29 29 Input Stack Time 4

30 30 Input Stack Time 5

31 31 Input Stack Time 6

32 32 Input Stack accept Time 7

33 33 Formal Definitions for NPDAs

34 34 Transition function

35 35 Transition function current state current input symbol current stack top new state new stack top An unspecified transition function is to the null set and represents a dead configuration for the NPDA.

36 36 Formal Definition Non-Deterministic Pushdown Automaton NPDA States Input alphabet Stack alphabet Transition function Final states Stack start symbol

37 37 Instantaneous Description Current state Remaining input Current stack contents

38 38 Input Stack Time 4: ExampleInstantaneous Description

39 39 Input Stack Time 5: ExampleInstantaneous Description

40 40 We write Time 4Time 5

41 41 A computation example

42 42 A computation example

43 43 A computation example

44 44 A computation example

45 45 A computation example

46 46 A computation example

47 47 A computation example

48 48 A computation example

49 49 For convenience we write

50 50 Formal Definition Language of NPDA M Initial state Final state

51 51 Example NPDA M

52 52 NPDA M

53 53 Therefore: NPDA M

54 54 NPDAs Accept Context-Free Languages

55 55 Context-Free Languages (Grammars) Languages Accepted by NPDAs Theorem

56 56 Context-Free Languages (Grammars) Languages Accepted by NPDAs Proof - Step 1: Convert any context-free grammar G to a NPDA M with L(G) = L(M)

57 57 Context-Free Languages (Grammars) Languages Accepted by NPDAs Proof - Step 2: Convert any NPDA M to a context-free grammar G with L(M) = L(G)

58 58 Converting Context-Free Grammars to NPDAs

59 59 An example grammar: What is the equivalent NPDA?

60 60 Grammar NPDA

61 61 The NPDA simulates leftmost derivations of the grammar L(Grammar) = L(NPDA)

62 62 Grammar: A leftmost derivation:

63 63 NPDA execution: Input Stack Time 0 Start

64 64 Input Stack Time 1

65 65 Input Stack Time 2

66 66 Input Stack Time 3

67 67 Input Stack Time 4

68 68 Input Stack Time 5

69 69 Input Stack Time 6

70 70 Input Stack Time 7

71 71 Input Stack Time 8

72 72 Input Stack Time 9

73 73 Input Stack Time 10 accept

74 74 In general Given any grammar G we can construct a NPDA M with

75 75 For any productionFor any terminal Constructing NPDA M from grammar G Top-down parser

76 76 Grammar G generates string w if and only if NPDA M accepts w

77 77 For any context-free language there is an NPDA that accepts the same language

78 78 Which means Languages Accepted by NPDAs Context-Free Languages (Grammars)

79 79 Converting NPDAs to Context-Free Grammars

80 80 For any NPDA M we will construct a context-free grammar G with

81 81 in NPDA M Input processedStack contents terminals variables A derivation in Grammar The grammar simulates the machine

82 82 Some Simplifications First we modify the NPDA so that It has a single final state q f and It empties the stack when it accepts the input Original NPDAEmpty Stack

83 83 Second we modify the NPDA transitions. All transitions will have form: or which means that each move increases/decreases stack by a single symbol.

84 84 Those simplifications do not affect generality of our argument. It can be shown that for any NPDA there exists an equivalent one having above two properties i.e. the equivalent NPDA with a single final state which empties its stack when it accepts the input, and which for each move increases/decreases stack by a single symbol.

85 85 Example of a NPDA in an appropriate form

86 86 The Grammar Construction In grammar G Terminals: Input symbols of NPDA states Stack symbol Variables:

87 87 For each transition: we add production:

88 88 For each transition: we add production: for all states q k, q l

89 89 Start Variable Stack bottom symbol Start state (Single) Final state

90 90 Example Grammar production:

91 91 Grammar productions:

92 92 Grammar production:

93 93 Resulting Grammar

94 94 Resulting Grammar, cont.

95 95 Resulting Grammar, cont.

96 96 Derivation of string

97 97 In general, in grammar: if and only if is accepted by the NPDA

98 98 Explanation By construction of Grammar: if and only if in the NPDA going from q i to q j the stack doesn’t change below and A is removed from stack

99 99 Context-Free Languages (Grammars) Languages Accepted by NPDAs We have shown the procedure to convert any NPDA M to a context-free grammar G with L(M) = L(G) which means

100 100 Context-Free Languages (Grammars) Languages Accepted by NPDAs Therefore END OF PROOF

101 101 Example (Sudkamp 8.1.2) Language consisting solely of a’s or an equal number of a´s and b´s.


Download ppt "1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010."

Similar presentations


Ads by Google