Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 2 Content 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 3 Regular Languages Context-Free Languages Non-regular languages

4 4 Context-Free Languages

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

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

7 7 Definition: Regular Grammars Grammar Right or Left Linear Grammars. Productions of the form: is string of terminals VariablesTerminal symbols Start variables or

8 8 Pushdown Automata PDAs

9 9 Pushdown Automaton - PDA Input String Stack States

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

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

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

13 13 input Push top stack

14 14 input Pop top stack

15 15 input No Change top stack

16 16 Input Stack Example 3.7 Salling: A PDA for simple nested parenthesis strings Time 0

17 17 Input Stack Example 3.7 Time 1

18 18 Input Stack Example 3.7 Time 2

19 19 Input Stack Example 3.7 Time 3

20 20 Input Stack Example 3.7 Time 4

21 21 Input Stack Example 3.7 Time 5

22 22 Input Stack Example 3.7 Time 6

23 23 Input Example 3.7 Time 7 Stack

24 24 NPDAs Non-deterministic Push-Down Automata

25 25 Non-Determinism

26 26 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)

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

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

29 29 Pushing Strings Input symbol Pop symbol Push string

30 30 top input stack Push pushed string Example

31 31 Another NPDA example NPDA M

32 32 Time 0 Input Stack Current state Execution Example

33 33 Input Stack Time 1

34 34 Input Stack Time 2

35 35 Input Stack Time 3

36 36 Input Stack Time 4

37 37 Input Stack Time 5

38 38 Input Stack Time 6

39 39 Input Stack accept Time 7

40 40 Formal Definitions for NPDAs

41 41 Transition function

42 42 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.

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

44 44 Instantaneous Description Current state Remaining input Current stack contents

45 45 Input Stack Time 4: ExampleInstantaneous Description

46 46 Input Stack Time 5: ExampleInstantaneous Description

47 47 We write Time 4Time 5

48 48 A computation example

49 49 A computation example

50 50 A computation example

51 51 A computation example

52 52 A computation example

53 53 A computation example

54 54 A computation example

55 55 A computation example

56 56 For convenience we write

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

58 58 Example NPDA M

59 59 NPDA M

60 60 Therefore: NPDA M

61 61 NPDAs Accept Context-Free Languages

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

63 63 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)

64 64 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)

65 65 Converting Context-Free Grammars to NPDAs

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

67 67 Grammar NPDA

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

69 69 Grammar: A leftmost derivation:

70 70 NPDA execution: Input Stack Time 0 Start

71 71 Input Stack Time 1

72 72 Input Stack Time 2

73 73 Input Stack Time 3

74 74 Input Stack Time 4

75 75 Input Stack Time 5

76 76 Input Stack Time 6

77 77 Input Stack Time 7

78 78 Input Stack Time 8

79 79 Input Stack Time 9

80 80 Input Stack Time 10 accept

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

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

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

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

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

86 86 Converting NPDAs to Context-Free Grammars

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

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

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

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

91 91 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.

92 92 Example of a NPDA in an appropriate form

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

94 94 For each transition: we add production:

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

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

97 97 Example Grammar production:

98 98 Grammar productions:

99 99 Grammar production:

100 100 Resulting Grammar

101 101 Resulting Grammar, cont.

102 102 Resulting Grammar, cont.

103 103 Derivation of string

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

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

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

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

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


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

Similar presentations


Ads by Google