Download presentation
Presentation is loading. Please wait.
Published byDella Moore Modified over 9 years ago
1
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010
2
2 Content Midterm results Regular vs. Nonregular Languages Context-Free Languages Context-Free Grammars Derivation Trees. Ambiguity Push-Down Automata, PDA Applications
3
Midterm 1 Solutions Problem 1. (4 points) Circle true statements among the following: The number of outgoing arcs from a state of a DFA is always equal to | |. (True) The number of outgoing arcs from a state of a NFA is always equal to | |. (False) Not all finite languages are regular. (False) If L is regular language, then L R is also a regular language. (True) If L is regular language, then L 2 may not be a regular language. (False) The family of regular languages is closed under intersection. (True) The grammar is not regular. (True) The grammar is regular. (True) 3
4
Problem 2. (6 points) Convert the following NFA into regular expression: 4
5
Solution: Create a new initial state and a new, unique final state, neither of which is part of a loop. 5
6
Removed state 3: 6
7
Removed state 2: 7 Removed state 1:
8
b) Let M be the following DFA. 8 Minimize M by set partitioning.
9
b) Let M be the following DFA. 9 Minimize M by set partitioning.
10
10 Step 0: Partition the states into two groups accepting and non-accepting. State Reduction by Set Partitioning { 1, 3 }{ 2, 4, 5, 6 } P1P2
11
11 Step 1: Get the response of each state for each input symbol. P 1 P 2 p 2 p 2 p 1 p 2 p 2 p 2 a a {1, 3 } {2, 4, 5, 6 } b b p 2 p 2 p 2 p 1 p 2 p 1 State Reduction by Set Partitioning
12
12 P 11 P 21 P 22 P 23 p 21 p 21 p 11 p 23 p 23 p 22 a a {1, 3} {2} {4, 6} {5} b b p 22 p 22 p 23 p 11 p 11 p 21 Step 2: Partition the sets according to the responses, and go to Step 1 until no partition occurs. So the final partition results are as follows: {1, 3} {2} {4, 6} {5}
13
13 {1, 3} {2} {5} {4, 6} Minimized DFA consists of four states of the final partition, and the transitions are the one corresponding to the starting DFA. Minimized DFA Starting DFA 1,32 54,6
14
Problem 3 (4 points) Regular or not? If regular, construct an automaton, regular expression or a grammar. If not regular, use pumping lemma for regular languages. a) Set of all strings of form over alphabet = {0, 1}. Solution: Regular. A simple finite state machine with finite number of states which takes care of the order of symbols and counts the total number of characters. 14
15
15 0 1 1 1111 1 1 0 0000 0 0 1 111111 Solution by John E.
16
16 Solution by Otto N. 1818 1717 0 0 0 0 00 0 01616 1515 1414 1313 1212 1
17
17 Solution by Fredrik P. 08100810 07110711 06120612 05130513 04140414 03150315 02160216 01170117 00180018
18
18 b) Set of all strings of form over alphabet = {0, 1}. Solution: Not regular. Language L consists of all strings of the form 0*1* where the number of 0’s is eight more than the number of 1’s. We can show that L is not regular by applying pumping lemma. Let w = 0 m+8 1 m. Since |xy| m, y must equal 0 k for some k > 0. We can pump y out once, which will generate the string 0 m+8-k 1 m, which is not in L because the number of 0’s is is less than 8 more than the number of 1’s.
19
19 A Regular Language also There is a cycle in the graph The pumped strings
20
20 The Pumping Lemma Given an infinite regular language there exists an integer for any string with length we can write with and such that:
21
21 The Pumping Lemma States the property of regular language Cannot be used to prove that a language is regular! An example: If something is a square it always has four edges (a property of square) But: having proved that something has four edges does not necessarily mean that the object is a square.
22
22 The Pumping Lemma So we use pumping lemma not to prove that a language is regular, but to show that language (not obeying pumping lemma) can not be regular. It is usually done using contrapositive (proof by contradiction)
23
23 Applying Pumping Lemma If for arbitrary there exists a string with length such that for all decompositions with and there existssuch that: then the language is NOT REGULAR
24
24 Formal Definition Grammar Set of variables Set of terminal symbols Start variable Set of production rules
25
25 Regular Grammars A regular grammar is any right-linear or left-linear grammar Examples
26
26 A Nonregular Language DFA must have infinite number of states. Statesare distinct for each
27
27 Regular Languages Context-Free Languages Non-regular languages
28
28 Automata theory: formal languages and formal grammars
29
29 Context-Free Languages
30
30 Context-Free Languages Pushdown Automata Context-Free Grammars
31
31 Context-Free Grammars
32
32 A context-free grammar A derivation Example
33
33 A context-free grammar Another derivation
34
34
35
35 A context-free grammar A derivation Example
36
36 A context-free grammar Another derivation
37
37
38
38 A context-free grammar A derivation Example
39
39 A context-free grammar A derivation
40
40
41
41 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables
42
42 Definition: Context-Free Languages A language is context-free if and only if there is a grammar with
43
43 Derivation Order Leftmost derivation
44
44 Derivation Order Rightmost derivation
45
45 Leftmost derivation
46
46 Rightmost derivation
47
47 Derivation Trees
48
48
49
49
50
50
51
51
52
52 Derivation Tree
53
53 yield Derivation Tree
54
54 Partial Derivation Trees Partial derivation tree
55
55 Partial derivation tree
56
56 Partial derivation tree sentential form yield
57
57 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
58
58 Ambiguity
59
59 leftmost derivation derivation (* denotes multiplication)
60
60 derivation leftmost derivation
61
61
62
62 Two derivation trees
63
63 The grammar is ambiguous! Stringhas two derivation trees
64
64 stringhas two leftmost derivations The grammar is ambiguous:
65
65 Definition A context-free grammar is ambiguous if some string has two or more derivation trees (two or more leftmost/rightmost derivations)
66
66 Why do we care about ambiguity?
67
67 Why do we care about ambiguity?
68
68 Why do we care about ambiguity?
69
69 Correct result:
70
70 Ambiguity is bad for programming languages We want to remove ambiguity!
71
71 We fix the ambiguous grammar… …by introducing parentheses () to indicate grouping, (precedence) Non-ambiguous grammar
72
72
73
73 Unique derivation tree
74
74 The grammar : is non-ambiguous Every string has a unique derivation tree
75
75 Inherent Ambiguity Some context free languages have only ambiguous grammars! Example:
76
76 The string has two derivation trees
77
77 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables
78
78 Definition: Regular Grammars Grammar Right or Left Linear Grammars. Productions of the form: is string of terminals VariablesTerminal symbols Start variables or
79
79 Regular Languages Context-Free Languages Non-regular languages
80
80 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
81
81 Pushdown Automata PDAs
82
82 Pushdown Automaton - PDA Input String Stack States
83
83 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 information. POP reading symbol PUSH writing symbol
84
84 The States Input symbol Pop old reading stack symbol Push new writing stack symbol
85
85 top input stack Replace (An alternative is to start and finish with empty stack)
86
86 input Push top stack
87
87 input Pop top stack
88
88 input No Change top stack
89
89 Input Stack Example 3.7 Salling: A PDA for simple nested parenthesis strings Time 0
90
90 Input Stack Example 3.7 Time 1
91
91 Input Stack Example 3.7 Time 2
92
92 Input Stack Example 3.7 Time 3
93
93 Input Stack Example 3.7 Time 4
94
94 Input Stack Example 3.7 Time 5
95
95 Input Stack Example 3.7 Time 6
96
96 Input Example 3.7 Time 7 Stack
97
97 Applications: Compilers
98
98 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; }...... Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3... Machine Code
99
99 Lexical analyzer parser Compiler program machine code input output
100
100 A parser “knows” the grammar of the programming language
101
101 Parser PROGRAM STMT_LIST STMT_LIST STMT; STMT_LIST | STMT; STMT EXPR | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR EXPR + EXPR | EXPR - EXPR | ID IF_STMT if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT while (EXPR) do STMT
102
102 The parser finds the derivation of a particular input 10 + 2 * 5 Parser E E + E | E * E | INT E E + E E + E * E 10 + E*E 10 + 2 * E 10 + 2 * 5 input derivation
103
103 derivation derivation tree E E + E E + E * E 10 + E*E 10 + 2 * E 10 + 2 * 5 10 E 2 E 5 E E + E *
104
104 derivation tree mult a, 2, 5 add b, 10, a machine code 10 E 2 E 5 E E + E *
105
105 Parsing
106
106 grammar Parser input string derivation
107
107 Example: Parser derivation input ?
108
108 Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of
109
109
110
110 Phase 2 Phase 1
111
111 Phase 2 Phase 3
112
112 Final result of exhaustive search Parser derivation input (top-down parsing)
113
113 Context Free Art http://www.contextfreeart.org/wiki/index.php?page=AboutPage http://www.contextfreeart.org/wiki/index.php?page=AboutPage
114
114 Context Free Art http://www.contextfreeart.org/wiki/index.php?page=AboutPage http://www.contextfreeart.org/wiki/index.php?page=AboutPage
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.