homework
play

Homework Homework #2 returned Context Free Languages Homework #3 - PDF document

Homework Homework #2 returned Context Free Languages Homework #3 due today Homework #4 Pg 133 -- Exercise 1 (use structural induction) Pg 133 -- Exercise 3 Pg 134 -- Exercise 8b,c,d Pg 135 -- Exercise 22 Pg 145 --


  1. Homework  Homework #2 returned Context Free Languages  Homework #3 due today  Homework #4  Pg 133 -- Exercise 1 (use structural induction)  Pg 133 -- Exercise 3  Pg 134 -- Exercise 8b,c,d  Pg 135 -- Exercise 22  Pg 145 -- Exercise 13  Due 10 / 7 Announcements Before We Start  Exam 1  Any questions?  Next class (Oct 2 nd )  1 hour  Cover Regular Languages (up to and including today’s lecture)  Closed book (1 sheet study guide okay)  Remainder of next class will be a problem session Plan for today Languages  1st half  Recall.  Context Free Grammars and Languages  What is a language?  2nd half  What is a class of languages?  Regular Languages and Context Free Languages / L-Systems 1

  2. Regular Languages Last class we discovered  For the past several weeks, we have  Venn-diagram of languages been looking at Regular Languages: Is there  language: Regular Expression something Regular Languages out here?  Machine for accepting: Finite Automata YES! Finite Languages Context Free Languages Grammars  Context Free Languages(CFL) is the  Wikipedia says: next class of languages outside of  languages can be described as a system of Regular Languages: symbols and the grammars (rules) by which the symbols are manipulated  Language / grammar: Context Free  Grammar is the study of rules governing Grammar the use of language.  Machine for accepting: Pushdown Automata Grammars Grammars Let’s redefine grammars for CS Theory use:  Let’s formalize this a bit:  Terminals = Set of symbols that form the strings of the 1.  A grammar is a 4-tuple: (V, T, P, S) where language being defined Variables = Set of symbols representing categories  V is a set of variables 2. Start Symbol = variable that represents the “base  T is a set of terminals 3. category” that defines our language  P is a set of production rules Production rules = set of rules that recursively define the 4.  V and T are disjoint (I.e. V ∩ T = ∅ ) language  S ∈ V, is your start symbol 2

  3. General Grammars Grammars  Production Rules  Let’s formalize this a bit:  Production rules  Of the form A → B  We say that γ can be derived from α in one step:  A is a string of terminals and variables  A → β is a rule  α = α 1 A α 2  B is a string of terminals and variables  γ = α 1 β α 2  To apply a rule, replace any occurrence of A  α ⇒ γ with the string B.  We write α ⇒ * γ if γ can be derived from α in zero or more steps. Context Free Grammars Context Free Grammars  The language generated by a grammar  Production Rules  Of the form A → B  Let G = (V, T, P, S)  A is a variable  The language generated by G, L(G)  B is a string, combining terminals and variables  L(G) = { x ∈ T * | S ⇒ * x}  To apply a rule, replace an occurrence of A with the string B.  A language L is a Context Free  We say that the grammar is context-free since this Language (CFL) iff there is a CFG G, substitution can take place regardless of where A is. such that  L = L(G) Example Example  Recursive definition for palindromes  Recall our friend: Palindromes (pal) over Σ  A palindrome is a string that is the same 1. λ ∈ pal read left to right or right to left 2. For any a ∈ Σ , a ∈ pal  First half of a palindrome is a “mirror 3. For any x ∈ pal and a ∈ Σ , axa ∈ pal image” of the second half 4. No string is in pal unless it can be obtained  Examples: by rules 1-3  a, b, aba, abba, babbab. 3

  4. Example Back to CS Theory A CFG for palindromes over {a,b}  Building the palindrome abba using  Base cases grammar  P → λ  P ⇒ aPa (Rule 4) 1. P → a  P ⇒ abPba (Rule 5) 2. P → b 3.  P ⇒ ab λ ba (Rule 1) Recursion   P ⇒ abba 4. P → aPa 5. P → bPb Another Example Another Example  Find a CFG to describe:  Find a CFG to describe:  L = {x ∈ {0,1} * | n 0 (x) = n 1 (x)}  L = {x ∈ {0,1} * | n 0 (x) = n 1 (x)}  S → λ (1)  Basic idea (define recursively)  S → 0S1 (2)  λ is certainly in the language  S → 1S0 (3)  For all strings in the language, if we add a 0 and 1 to the string, the result is in the  S → SS (4) language.  The concatenation of any two strings in the language will also be in the language Another Example Yet Another example  Let’s derive a string from L  Find a CFG to describe:  00110011  L = {a i b j c k | i = k}  S ⇒ SS rule 4  Number of a’s equals the number of c’s with any number of b’s between them ⇒ 0S1 0S1 rule 2  Use variable B to represent b j ⇒ 00S11 00S11 rule 2  Every time you add a to the left of B you need ⇒ 00 λ 11 00 λ 11 rule 1 to add c to the right. = 00110011 4

  5. Yet Another example Another example  Find a CFG to describe:  Let’s derive a string from L: aabbbcc  L = {a i b j c k | i = k}  S ⇒ aSc rule 2  S → B (1) S ⇒ aaScc rule 2  S → aSc (2) S ⇒ aaBcc rule 1  B → bB (3) S ⇒ aabBcc rule 3  B → λ (4) S ⇒ aabbBcc rule 3  Can also write as S ⇒ aabbbBcc rule 3  S → B | aSc S ⇒ aabbb λ cc rule 4  B → bB | λ = aabbbcc One more example One more example  Defining the grammar for algebraic  Defining the grammar for algebraic expressions: expressions:  G = (V, T, P, S)  Let a = a numeric constant  Set of binary operators = {+, -, *, /}  V = {S}  Expressions can be parenthesized  T = { a, -, +, *, /, (, ) }  S = S  P = see next slide One more example One more example  Defining the grammar for algebraic  Show derivation for a + (a * a) / a expressions – Production rules  S ⇒ S + S rule 1  S → S + S (1) S ⇒ a + S rule 6 S ⇒ a + S / S rule 4 S → S – S (2) S ⇒ a + (S) / S rule 5 S → S * S (3) S ⇒ a + (S * S) / S rule 3 S → S / S (4) S ⇒ a + (a * S) / S rule 6 S → (S) (5) S ⇒ a + (a * a) / S rule 6 S → a (6) S ⇒ a + (a * a) / a rule 6 5

  6. Parse Trees Another example  Find a CFG to describe:  Graphical means to illustrate a derivation of a string from a grammar  L = {a i b j c k | i = k}  S → B (1)  Root of the tree = start variable  S → aSc (2)  Interior nodes = other variables  B → bB (3)  Children of nodes = application of a production  B → λ (4) rule  Can also write as  Leaf nodes = Terminal symbols  S → B | aSc  B → bB | λ Another example Parse Tree S  Let’s derive a string from L: aabbcc  An inorder traversal Rule 2 of the tree will give  S ⇒ aSc rule 2 a S c the the string S ⇒ aaScc rule 2 Rule 2 derived. a S c S ⇒ aaBcc rule 1 B Rule 1 S ⇒ aabBcc rule 3 S ⇒ aabbBcc rule 3 b B Rule 3 S ⇒ aabb λ cc rule 4 Rule 3 = aabbcc b B Rule 4 λ Recall our example from last time One more example  Defining the grammar for algebraic  Show derivation for a + a * a expressions – Production rules  S ⇒ S + S rule 1  S → S + S (1) S ⇒ a + S rule 6 S → S – S (2) S ⇒ a + S * S rule 3 S → S * S (3) S ⇒ a + a * S rule 6 S → S / S (4) S ⇒ a + a * a rule 6 S → (S) (5) S → a (6) 6

  7. Parse Tree One more example  Another derivation for a + a * a  S ⇒ S + S S rule 1  S ⇒ S * S rule 3 S + S S ⇒ a + S S ⇒ S * a rule 6 rule 6 S ⇒ S + S * a rule 1 S ⇒ a + S * S S * S a S ⇒ a + S * a rule 6 rule 3 S ⇒ a + a * a rule 6 S ⇒ a + a * S a a rule 6 S ⇒ a + a * a rule 6 Parse Tree Parse trees  S ⇒ S * S S rule 3 S S S ⇒ S * a S * S S + S S * S rule 6 a S + S S ⇒ S + S * a a S + S rule 1 S * S a a a S ⇒ a + S * a a a rule 6 a a S ⇒ a + a * a Same string, 2 derivations rule 6 Ambiguity Ambiguity  Showing a grammar is ambiguous is  A CFG is said to be ambiguous if there easy is at least 1 string in L(G) having two or  Find a string x in the L(G) that has two more distinct derivations. derivations  Showing a particular grammar is not  Some grammars are inherently ambiguous is usually difficult. ambiguous.  Showing that any grammar is not ambiguous is not possible. 7

Recommend


More recommend