Context-Free Grammars 2017/09/12 Chapter 2.1 in Sipser Ø Announcement: q Slides for this lecture are here: http://www.public.asu.edu/~yzhan442/teaching/cse355/lectures/cfg.pdf 1
Last time Nonregular languages o Limitations of regular laguages o Pumping lemma o Proving for nonregularity Using pumping lemma FA (DFA & NFA) express Regular Expressions (RE) Using closure properties of RLs Ø Goals: o Learned the limitations of regular languages o Learned how to prove a language is not regular o Learned to use proof by contradiction 2
Summary so far Regular languages o Finite Automata (FA) o Regular expressions (REs) o Equivalence of FA and REs o Nonregular langauges FA (DFA & NFA) express Regular Expressions (RE) RL 3
Summary so far Regular languages Ø Simple computation with FA Ø Intuitive design with FA or RE Ø Decidable computing machine Ø Sometimes too simple e.g., {a n b n } FA (DFA & NFA) express Regular Expressions (RE) RL 4
Outline for today Context free langauges o Context free langauges o Context free grammars q Design CFGs q Ambiguities FA (DFA & NFA) express Regular Expressions (RE) CFL q Chomsky normal form Ø Goals: o Learn context free languages o Learn context free grammars 5
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules FA (DFA & NFA) express Regular Expressions (RE) CFL A! 0A1 B 6
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules Steps to generate a string in the language of a CFG: 1. Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the righthand side of that rule. 3. Repeat step 2 until no variables remain. A! 0A1 B 7
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules Generate 000#111: A 1. Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step 2 until no variables remain. 8
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules Generate 000#111: A => (yields) 0A1 1. Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step 2 until no variables remain. 9
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules Generate 000#111: A => (yields) 0A1 0A1 => 00A11 1. Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step 2 until no variables remain. 10
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules Generate 000#111: A => (yields) 0A1 0A1 => 00A11 00A11 => 000A111 1. Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step 2 until no variables remain. 11
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules Generate 000#111: A => (yields) 0A1 0A1 => 00A11 00A11 => 000A111 000A111 => 000B111 1. Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step 2 until no variables remain. 12
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules Generate 000#111: A => (yields) 0A1 0A1 => 00A11 00A11 => 000A111 000A111 => 000B111 000B111 => 000#111 1. Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step 2 until no variables remain. 13
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules Generate 000#111: A => (yields) 0A1 0A1 => 00A11 00A11 => 000A111 000A111 => 000B111 000B111 => 000#111 A => * (derives) 000#111 1. Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step 2 until no variables remain. 14
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules Generate 000#111: A => * (derives) 000#111 1. Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step 2 until no variables remain. 15
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules FA (DFA & NFA) express Regular Expressions (RE) CFL 16
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules a(b [ c)*c 17
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules a(b c)*c [ [ b c A b c 18
Context-Free Languages Context free grammars (CFGs) start variable variables terminals a(b c)*c [ [ b c A b c (b c)* B AB " [ substituation rules 19
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules a(b c)*c [ b c A b c (b c)* B AB " [ [ [ a(b c)*c C abc which one is the starting symbol? 20
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules a(b c)*c [ b c A b c (b c)* B AB " [ [ [ a(b c)*c C abc What does this tell us? which one is the starting symbol? 21
Context-Free Languages Context free grammars (CFGs) start variable variables terminals substituation rules a(b c)*c [ b c A b c (b c)* B AB " [ [ [ a(b c)*c C abc What does this tell us? CFL RL! which one is the starting symbol? 22
Outline for today Context free langauges o Context free langauges o Context free grammars q Design CFGs q Ambiguities FA (DFA & NFA) express Regular Expressions (RE) CFL q Chomsky normal form Ø Goals: o Learn context free languages o Learn context free grammars 23
Context-Free Human-aware Grammars Robo.cs (CFGs) The language of a CFG G, or L(G) is 24
Context-Free Human-aware Grammars Robo.cs (CFGs) G1: What is R? 25
Context-Free Human-aware Grammars Robo.cs (CFGs) G1: What is R? In exams or homework, you only need to write down the rules 26
Context-Free Human-aware Grammars Robo.cs (CFGs) G1: We can now match parenthesis. Is valid c programs a context free language? 27
Context-Free Human-aware Grammars Robo.cs (CFGs) <statement>!... <if> <for>... <if>! if (<expr>) <statement> if (<expr>) <statement> else <statement> <for>! for (<expr>; <expr>; <expr>) <statement> <expr>! (<expr>) <var> <<num> <var> == <num> <var>++ <num>! <num><num> 0 1... 9 <var>! i j We can now match parenthesis. Is valid c programs a context free language? 28
Context-Free Human-aware Grammars Robo.cs (CFGs) <statement>!... <if> <for>... <if>! if (<expr>) <statement> if (<expr>) <statement> else <statement> <for>! for (<expr>; <expr>; <expr>) <statement> <expr>! (<expr>) <var> <<num> <var> == <num> <var>++ <num>! <num><num> 0 1... 9 <var>! i j We can now match parenthesis. Is valid c programs a context free language? Not really int main() { return 0;} 29
Outline for today Context free langauges o Context free langauges o Context free grammars q Design CFGs q Ambiguities FA (DFA & NFA) express Regular Expressions (RE) CFL q Chomsky normal form Ø Goals: o Learn context free languages o Learn context free grammars 30
Designing CFG Many CFLs are union of simpler CFLs Construct the grammar G i for each simpler language L i with start variable S i Combine the rules with a new rule e.g., Connection between two parts {0 n 1 n } One structure within the another (recursive structures): <statement>!... <if> <for>... <if>! if (<expr>) <statement> if (<expr>) <statement> else <statement> <for>! for (<expr>; <expr>; <expr>) <statement> <expr>! (<expr>) <var> <<num> <var> == <num> <var>++ <num>! <num><num> 0 1... 9 <var>! i j 31
Designing CFG for RL Construct CFG for a regular language 1. Construct a DFA D = (Q,,,q 0,F) for the RL 2. Convert the DFA into CFG R i for each q i 2 Q R i! ar j if (q i,a)=q j R i! " if q i 2 F R 0 is the start variable 32
Designing CFG for RL Construct CFG for a regular language 1. Construct a DFA D = (Q,,,q 0,F) for the RL 2. Convert the DFA into CFG What does this tell us? CFL RL! R i for each q i 2 Q R i! ar j if (q i,a)=q j R i! " if q i 2 F R 0 is the start variable R 1! 0R 1 R 1! 1R 2 R 2! 1R 2 R 2! 0R 3 R 3! 0R 2 R 3! 0R 2 R 2! " 33
Outline for today Context free langauges o Context free langauges o Context free grammars q Design CFGs q Ambiguities FA (DFA & NFA) express Regular Expressions (RE) CFL q Chomsky normal form Ø Goals: o Learn context free languages o Learn context free grammars 34
Ambiguity Different meanings => Different parse trees Consider a + a 35
Ambiguity Different meanings => Different parse trees Consider a + a Even though the substitution orderings are different, the parse trees are the same So are the meanings! 36
Ambiguity Different meanings => Different parse trees Consider a + a A derivation of a string w in a grammar G is a leftmost derivation if at every step the leftmost remaining variable is the one replaced. Certain languages are inherently ambiguous 37
Ambiguity Consider a + a a 38
Ambiguity Consider a + a a Generate a + a x a (using leftmost derivation): 39
Ambiguity Consider a + a a Generate a + a x a (using leftmost derivation): E => E + T E + T => T + T T + T => F + T F + T => a + T a + T => a + T x F a + T x F => a + F x F a + F x F => a + a x F a + a x F => a + a x a 40
Outline for today Context free langauges o Context free langauges o Context free grammars q Design CFGs q Ambiguities q Chomsky normal form FA (DFA & NFA) express Regular Expressions (RE) CFL Ø Goals: o Learn context free languages o Learn context free grammars 41
Chomsky Normal Form 42
Chomsky Normal Form Remove "-rules Remove unit rules (with a single variable on the right hand side) Chomsky normal form Remove rules with more than two compnents (variables or terminals) 43
Chomsky Normal Form 44
Chomsky Normal Form 45
Chomsky Normal Form 46
Chomsky Normal Form 47
Chomsky Normal Form 48
Outline for today Context free langauges o Context free langauges o Context free grammars q Design CFGs q Ambiguities FA (DFA & NFA) express Regular Expressions (RE) CFL q Chomsky normal form Ø Goals: o Learned context free languages o Learned context free grammars Reading assignment for the next class: o Sipser Sec. 2.2 (up to page 118, before the formal proof) Quiz link will be sent out; due date is before the beginning of the next class 49