homework and exams
play

Homework and Exams Homework Context Free Languages Return - PDF document

Homework and Exams Homework Context Free Languages Return Homework #2 Homework #3 Due today Grammars Homework and Exams Homework and Exams Homework #4 (due 10/13) and Exams Exercise 5.1.2 b, c (pg 180) Exam


  1. Homework and Exams • Homework Context Free Languages – Return Homework #2 – Homework #3 Due today Grammars Homework and Exams Homework and Exams – Homework #4 (due 10/13) • …and Exams • Exercise 5.1.2 b, c (pg 180) – Exam 1 is Wednesday!! • Exercise 5.1.7 a,b (pg 181) • Exercise 5.2.1 b,c (pg 191) • Design a CFG for the following languages – Set of odd length strings in {a,b} * with middle symbol a – Questions? – Set of even length strings in {a,b} * with the 2 middle symbols equal • Show that the CFG with the productions below is ambiguous: – S → a | Sa | bSS | SSb | SbS Schedule Mangling Schedule Mangling • Today • Perhaps more mangling to come in Week 8 – Chapter 5: CFGs, Parse Trees, Ambiguity – Move exam 2 to Wednesday? • Wednesday – Exam 1 – Problem Session for Chapter 5 – Stay tuned • Monday, Oct 13 th • Watch the schedule page – Homework #4 due – Homework #5 Assigned – Chapter 6: Pushdown Automata • Wednesday, Oct 15 th – Chapter 6 (cont) – Problem Session for Chapter 6

  2. Before We Start Plan for today • Any questions? • Context Free Languages – Next class of languages in our quest! Languages Regular Languages • Recall. • For the past several weeks, we have been looking at Regular Languages: – What is a language? – Means of describing: Regular Expression – What is a class of languages? – Machine for accepting: Finite Automata Last class we discovered Context Free Languages • Venn-diagram of languages • Context Free Languages(CFL) is the next class of languages outside of Regular Is there Languages: something Regular Languages out here? – Means for defining: Context Free Grammar – Machine for accepting: Pushdown Automata YES! Finite Languages

  3. Plan for today Introduction • Grammars • Define and describe context free grammars – Think back to your days of learning English – Rules for constructing a valid sentence • Questions? • Sentence = noun phrase + verb phrase • Noun phrase = – Name (Joe) – Article + noun (the car) • Verb Phrase = – Verb (runs) – Verb + prepositional phrase Introduction Introduction • Look at the sentence. Is this grammatically • Grammars correct? – Rules for constructing a simple sentence – Joe runs from the car. • Sentence = noun phrase + verb phrase • Noun phrase = – Sentence = noun phrase + verb phrase – Name (Joe) – = noun + verb phrase – Article + noun (the car) – = Name + verb phrase • Verb Phrase = – = Joe + verb phrase – Verb (runs) – = Joe + verb + prepositional phrase – Verb + prepositional phrase – = Joe + verb + preposition + noun phrase • Prepositional Phrase = – = Joe + verb + from + noun phrase – Preposition + noun phrase (from the car) – = Joe + verb + from + article +noun Introduction Introduction • Look at the sentence. Is this grammatically • Observations correct? – Our definition of a valid simple sentence – Joe runs from the car. includes: • A set of grammar categories (sentence, noun phrase, – Sentence = Joe + verb + from + article +noun verb phrase, noun, verb, etc) – = Joe + verb + from + the + car • Rules for defining each category – = Joe + ran + from + the + car • Set of “final strings” (Joe, ran, from, the, car) • Initial category of what we wish to create (sentence) • Valid sentence!

  4. Back to CS Theory Back to CS Theory • CFLs are defined using Context Free • Recall our friend: Palindromes Grammars(CFG) – A palindrome is a string that is the same read left to right or right to left – Grammars, like their spoken language counterparts, provide a means to recursively – First half of a palindrome is a “mirror image” “deriving” the strings in a language. of the second half – Examples: • a, b, aba, abba, babbab. Back to CS Theory Back to CS Theory • Recursive definition for palindromes (pal) • A CFG for palindromes over {a,b} over Σ – Base cases 1. ε ∈ pal 1. P → ε 2. P → a 2.For any a ∈ Σ , a ∈ pal 3. P → b 3.For any x ∈ pal and a ∈ Σ , axa ∈ pal – Recursion 4.No string is in pal unless it can be obtained by 4. P → aPa rules 1-3 5. P → bPb Back to CS Theory Back to CS Theory • What we did: • Building the palindrome abba using grammar – At each step, we replaced the symbol P on the right with one of the definitions of P from our • P ⇒ aPa (Rule 4) rule set. • P ⇒ abPba (Rule 5) – Relating to our sentence example • P ⇒ ab ε ba (Rule 1) • Categories were replaced by Symbols • P ⇒ abba • Final strings were replaced by Symbols • Start category was replaced by a symbol • We still have our set of rules.

  5. Context Free Grammars Context Free Grammars • Let’s redefine grammars for CS Theory use: • Production Rules 1. Terminals = Set of symbols that form the strings of – Of the form A → B the language being defined • A is a variable 2. Variables = Set of symbols representing categories • B is a string, combining terminals and variables 3. Start Symbol = variable that represents the “base • To apply a rule, replace an occurance of A with the category” that defines our language string B. 4. Production rules = set of rules that recursively define the language Context Free Grammars Context Free Grammars • Let’s formalize this a bit: • Let’s formalize this a bit: – Production rules – A context free grammar (CFG) is a 4-tuple: (V, • Of the form A → β where T, P, S) where – A ∈ V • V is a set of variables – β ∈ (V ∪ T) * string with symbols from V and T • T is a set of terminals • We say that γ can be derived from α in one step: • P is a set of production rules – A → β is a rule – α = α 1 A α 2 • V and T are disjoint (I.e. V ∩ T = ∅ ) – γ = α 1 β α 2 • S ∈ V, is your start symbol – α ⇒ γ Context Free Grammars Context Free Grammars • Let’s formalize this a bit: • The language generated by a CFG – Production rules – Let G = (V, T, P, S) • We say that the grammar is context-free since this – The language generated by G, L(G) substitution can take place regardless of where A is. • L(G) = { x ∈ T * | S ⇒ * x} • We write α ⇒ * γ if γ can be derived from α in zero or more steps. – A language L is a Context Free Language (CFL) iff there is a CFG G, such that • L = L(G)

  6. Example 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 • S → SS (4) the string, the result is in the language. • The concatenation of any two strings in the language will also be in the language Example Another example • Let’s derive a string from L • Find a CFG to describe: – L = {a i b j c k | i = k} – 00110011 – 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 to ⇒ 00 ε 11 00 ε 11 rule 1 add c to the right. = 00110011 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 | ε = aabbb ε cc

  7. 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 A real practical example Practical uses for grammars • How a compiler works • Grammars for programming languages – <stmt> → … | <for-stmt> | <if-stmt> | … – <stmt> → { <stmt> <stmt> } | ε Stream Parse lexer parser codegen of tokens – <if-stmt> → if ( <expr> ) then <stmt> Tree – <for-stmt> → for ( <expr> ; <expr> ; <expr> ) Object <stmt> code Source file

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