homework
play

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

Homework Homework #2 returned Context Free Languages Homework #3 returned today (for early submission) Homework #3 due today (for non-early submission) Homework #4 Pg 133 -- Exercise 1 (use structural induction) Pg 133 --


  1. Homework  Homework #2 returned Context Free Languages  Homework #3 returned today (for early submission)  Homework #3 due today (for non-early submission)  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 / 10 Announcements Before We Start  Exam 1  Any questions?  Next class (Oct 5th)  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 something Regular Languages  language: Regular Expression out here?  Grammar: Regular Grammars  Machine for accepting: Finite Automata YES! Finite Languages Context Free Languages Grammars  Context Free Languages(CFL) is the  Let’s formalize this a bit: next class of languages outside of  A grammar is a 4-tuple: (V, T, P, S) where Regular Languages:  V is a set of variables  T is a set of terminals  Language / grammar: Context Free  P is a set of production rules Grammar  V and T are disjoint (I.e. V ∩ T = ∅ )  Machine for accepting: Pushdown  S ∈ V, is your start symbol Automata 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. 2

  3. Context Free Grammars Context Free Grammars  Production Rules  The language generated by a grammar  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. 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 2.  P ⇒ abPba (Rule 5) P → b 3.  P ⇒ ab λ ba (Rule 1) Recursion   P ⇒ abba 4. P → aPa 5. P → bPb 3

  4. 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  S → SS (4) and 1 to the string, the result is in the 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 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 4

  5. 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 → S – S (2) S ⇒ a + S / S rule 4 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 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 | λ 5

  6. 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 b B = aabbcc 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) Parse Tree One more example  Another derivation for a + a * a  S ⇒ S + S S rule 1  S ⇒ S * S rule 3 S ⇒ a + S S + 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 6

  7. 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. Derivations Derivations a + a + a  Leftmost derivations S  A leftmost derivation is one where the leftmost S variable in the current string is always the first to S + S get replaced via a production rule. S + S  A rightmost derivation is one where the rightmost a variable in the current string is always the first to S + S S + S a get replaced via a production rule. a a a a rightmost derivation leftmost derivation 7

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend