1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005
2 Midterm Exam 2 Context-Free Languages Place: the LAMBDA lecture hall Time: Thursday , 13:15-15:00 It is OPEN BOOK. (This means you are allowed to bring in one book of your choice.) It will cover Context-free Languages. You will have the complete 2 hours to do the test.
3 RECAPITULATION: CONTEXT-FREE LANGUAGES Context-Free Languages, CFL Pushdown Automata, PDA Pumping Lemma for CFL Selected CFL Problems
4 Context-Free Languages
5 Pushdown Automata Context-Free Grammars stack automaton
6 Context-Free Grammars
7 A context-free grammar A derivation Example
8 A context-free grammar Another derivation
9
10 A context-free grammar A derivation Example
11 A context-free grammar Another derivation
12
13 A context-free grammar A derivation Example
14 A context-free grammar A derivation
15
16 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables
17 Definition: Context-Free Language A language is context-free if and only if there is a grammar with
18 Derivation Order Leftmost derivation
19 Derivation Order Rightmost derivation
20 Leftmost derivation
21 Rightmost derivation
22 Derivation Trees
23
24
25
26
27 Derivation Tree
28 yield Derivation Tree
29 Ambiguity
30 leftmost derivation derivation (* denotes multiplication)
31 derivation leftmost derivation
32 Two derivation trees
33 The grammar is ambiguous! Stringhas two derivation trees
34 stringhas two leftmost derivations The grammar is ambiguous:
35 Definition A context-free grammar is ambiguous if some string has two or more derivation trees (two or more leftmost/rightmost derivations)
36 Why do we care about ambiguity?
37 Why do we care about ambiguity?
38 Why do we care about ambiguity?
39 Correct result:
40 Ambiguity is bad for programming languages We want to remove ambiguity!
41 We fix the ambiguous grammar… …by introducing parentheses () to indicate grouping, (precedence) Non-ambiguous grammar (E expression; T term; F factor)
42
43 Unique derivation tree
44 The grammar is non-ambiguous Every string has a unique derivation tree.
45 Inherent Ambiguity Some context free languages have only ambiguous grammars! Example:
46 The string has two derivation trees
47 Pushdown Automata PDAs
48 Pushdown Automaton - PDA Input String Stack States
49 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. PUSH writing symbol POP reading symbol
50 The States Input symbol Pop symbol Push symbol
51 top input stack Replace (An alternative is to start and finish with empty stack.)
52 input Push top stack
53 input Pop top stack
54 input No Change top stack
55 Input Stack Example 3.7 Salling: A PDA for simple nested parenthesis strings
56 Input Stack Example 3.7
57 Input Stack Example 3.7
58 Input Stack Example 3.7
59 Input Stack Example 3.7
60 Input Stack Example 3.7
61 Input Stack Example 3.7
62 Input Stack Example 3.7
63 NPDAs
64 Non-Determinism
65 A string is accepted if: All the input is consumed The last state is a final state Stack in the initial condition (either: empty (when we started with empty stack), or: bottom symbol reached)
66 is the language accepted by the NPDA: Example NPDA
67 NPDA (Even-length palindromes) Example NPDA
68 Pushing Strings Input symbol Pop symbol Push string
69 top input stack Push pushed string Example:
70 Another NPDA example NPDA
71 Formal Definitions for NPDAs
72 Transition function:
73 Transition function: current state current input symbol current stack top new state new stack top
74 An unspecified transition function is to the null set and represents A dead configuration for the NPDA.
75 Formal Definition of NPDA Non-Deterministic Pushdown Automaton States Input alphabet Stack alphabet Transition function Final states Stack start symbol
76 Formal Definition Language of NPDA Initial state Final state
77 NPDAs Accept Context-Free Languages
78 Context-Free Languages (Grammars) Languages Accepted by NPDAs Theorem
79 Context-Free Languages (Grammars) Languages Accepted by NPDAs Proof - Step 1: Convert any context-free grammar to a NPDA with
80 Context-Free Languages (Grammars) Languages Accepted by NPDAs Proof - Step 2: Convert any NPDA to a context-free grammar with:
81 Converting Context-Free Grammars to NPDAs
82 An example grammar: What is the equivalent NPDA?
83 Grammar NPDA
84 The NPDA simulates leftmost derivations of the grammar L(Grammar) = L(NPDA)
85 In general Given any grammar We can construct a NPDA With
86 For any production For any terminal Constructing NPDA from grammar Top-down parser
87 Grammar generates string if and only if NPDA accepts
88 Therefore: For any context-free language there is an NPDA that accepts the same language
89 Converting NPDAs to Context-Free Grammars
90 For any NPDA we will construct a context-free grammar with
91 in NPDA Input processedStack contents terminalsvariables A derivation in Grammar The grammar simulates the machine
92 Some Simplifications First we modify the NPDA so that It has a single final state It empties the stack when it accepts the input Original NPDA Empty Stack
93 Second we modify the NPDA transitions. All transitions will have form: or which means that each move increases/decreases stack by a single symbol.
94 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.
95 For each transition: we add production:
96 For each transition: we add production: for all states
97 Start Variable Stack bottom symbol Start state (Single) Final state
98 Language Hierarchy
99 Regular Languages Context-Free Languages Non-regular languages
100 Deterministic PDAs (DPDAs)
101 Allowed DPDAs
102 Not allowed
103 Allowed Something must be matched from the stack
104 Not allowed
105 NPDAs Have More Power than DPDAs
106 Positive Properties of Context-Free Languages
107 Context-free languages are closed under Union is context free is context-free Union
108 In general: For context-free languages with context-free grammars and start variables The grammar of the union has new start variable and additional production
109 Context-free languages are closed under Concatenation is context free is context-free Concatenation
110 In general: For context-free languages with context-free grammars and start variables The grammar of the concatenation has new start variable and additional production
111 Context-free languages are closed under star-operation is context free Star Operation is context free
112 In general: For context-free language with context-free grammar and start variable The grammar of the star operation has new start variable and additional production
113 Negative Properties of Context-Free Languages
114 Context-free languages are not closed under intersection is context free not necessarily context-free Intersection
115 Context-free languages are not closed under complement is context free not necessarily context-free Complement
116 Intersection of CFL and RL (Regular Closure)
117 The intersection of a context-free language and a regular language is a context-free language context free regular context-free
118 The Pumping Lemma for Context-Free Languages
119 The Pumping Lemma for CFL there exists an integer such that for any string we can write For infinite context-free language with lengths and
120 Applications of The Pumping Lemma for CFL
121 Context-free languages Restriction-free languages
122 Theorem The language is not context free Proof Use the Pumping Lemma for context-free languages
123 Assume for contradiction that is context-free Since is context-free and infinite we can apply the pumping lemma
124 Pumping Lemma gives a number such that: Pick any string with length We pick:
125 We can write: with lengths and
126 Pumping Lemma says: for all
127 We examine all the possible locations of string in
128 Case 1: is within
129 Case 5: Similar analysis to case 4 …ETC….WE GO THROUGH ALL CASES…
130 There are no other cases to consider (since, string cannot overlap, and at the same time)
131 In all cases we obtained a contradiction Therefore: The original assumption that is context-free must be wrong Conclusion:is not context-free END OF PROOF
132 Regular Languages Context-free languages Unrestricted grammar languages
133 Selected Examples of CF Language Problems
134 Let G be the grammar with productions: Claim: L(G) = L Find a CFG for the following language Example
135 Proof: Consider the following derivation: S * a n Sc n a n Bc n * a n b m Bc m c n a n b m c (n + m) (where the first * applies S aSc n times, the second B bBc m times) Since all words in L(G) must follow this pattern in their derivations, it is clear that L(G) L Find a CFG for the following language
136 Consider w L, w = a n b m c (n + m) for some n, m 0 The derivation S * a n Sc n a n Bc n * a n b m Bc m c n a n b m c (n + m) clearly produces w for any n, m. L L(G) L L(G) G is a CFG for L Find a CFG for the following language END OF PROOF
137 Find a PDA and CFG for the following language Example Is the automaton deterministic? Yes. It acts in a unique way in each state.
138 CFG :
139 Find a PDA and CFG for the following language Example PDA
140 CFG :
141 Consider the following two languages: L 1 ={w : w is made from a’s and b’s and the length of w is a multiple of 10} L 2 = {a n b n : n 0} Prove that the language L is context-free Example
142 L 1 ={w : w is made from a’s and b’s and the length of w is a multiple of ten} L 2 = {a n b n : n 0} Let L 1 denote the complement of L 1. We have that L = L 1 L 2. L 1 is a regular language, since we can easily build a finite automaton with 10 states that accepts any string in this language. L 1 is regular too, since regular languages are closed under complement.
143 The language L 2 is context-free. The grammar is: S aSb | Therefore, the language L = L 1 L 2 is also context-free, since context-free languages are closed under regular intersection (Regular Closure). END O PROOF
144 Find a PDA and CFG for the following language Example CFG Production ex.
145 PDA
146 Find a PDA and CFG for the following language Example PDA
147 CFG, direct construction Strings start and finish with different symbols Strings contain at least one more a than b (we must have AA here as only one A just balances b)