homework
play

Homework Homework Context Free Languages Return Homework #2 - PDF document

Homework Homework Context Free Languages Return Homework #2 Homework #3 Due today Grammars Homework Before We Start Homework #4 (due 10/11 or 10/9) Any questions? Exercise 5.1.2 a, b, c (pg 180) Exercise 5.2.1


  1. Homework • Homework Context Free Languages – Return Homework #2 – Homework #3 Due today Grammars Homework Before We Start – Homework #4 (due 10/11 or 10/9) • Any questions? • Exercise 5.1.2 a, b, c (pg 180) • Exercise 5.2.1 a,b,c (pg 191) • Exercise 5.4.2 (pg 214) • Design a CFG for the following languages – Set of odd length strings in {a,b} * with middle symbol a – 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 Plan for today Languages • Context Free Languages • Recall. – Next class of languages in our quest! – What is a language? – What is a class of languages? 1

  2. Regular Languages Last class we discovered • For the past several weeks, we have been • Venn-diagram of languages looking at Regular Languages: Is there – Means of describing: Regular Expression something Regular Languages out here? – Machine for accepting: Finite Automata YES! Finite Languages Context Free Languages Plan for today • Context Free Languages(CFL) is the next • Define and describe context free grammars class of languages outside of Regular Languages: • Questions? – Means for defining: Context Free Grammar – Machine for accepting: Pushdown Automata Introduction Introduction • Grammars • Grammars – Think back to your days of learning English – Rules for constructing a simple sentence • Sentence = noun phrase + verb phrase – Rules for constructing a valid sentence • Noun phrase = • Sentence = noun phrase + verb phrase – Name (Joe) • Noun phrase = – Article + noun (the car) – Name (Joe) • Verb Phrase = – Article + noun (the car) – Verb (runs) • Verb Phrase = – Verb + prepositional phrase – Verb (runs) • Prepositional Phrase = – Verb + prepositional phrase – Preposition + noun phrase (from the car) 2

  3. Introduction Introduction • Look at the sentence. Is this grammatically • Look at the sentence. Is this grammatically correct? correct? – Joe runs from the car. – Joe runs from the car. – Sentence = noun phrase + verb phrase – Sentence = Joe + verb + from + article +noun – = noun + verb phrase – = Name + verb phrase – = Joe + verb + from + the + car – = Joe + verb phrase – = Joe + ran + from + the + car – = Joe + verb + prepositional phrase – = Joe + verb + preposition + noun phrase • Valid sentence! – = Joe + verb + from + noun phrase – = Joe + verb + from + article +noun Introduction Back to CS Theory • Observations • CFLs are defined using Context Free Grammars(CFG) – Our definition of a valid simple sentence includes: – Grammars, like their spoken language • A set of grammar categories (sentence, noun phrase, counterparts, provide a means to recursively verb phrase, noun, verb, etc) “deriving” the strings in a language. • Rules for defining each category • Set of “final strings” (Joe, ran, from, the, car) • Initial category of what we wish to create (sentence) Back to CS Theory Back to CS Theory • Recall our friend: Palindromes • Recursive definition for palindromes (pal) over Σ – A palindrome is a string that is the same read 1. ε ∈ pal left to right or right to left 2.For any a ∈ Σ , a ∈ pal – First half of a palindrome is a “mirror image” of the second half 3.For any x ∈ pal and a ∈ Σ , axa ∈ pal – Examples: 4.No string is in pal unless it can be obtained by • a, b, aba, abba, babbab. rules 1-3 3

  4. Back to CS Theory Back to CS Theory • A CFG for palindromes over {a,b} • Building the palindrome abba using grammar – Base cases 1. P → ε • P ⇒ aPa (Rule 4) • P ⇒ abPba (Rule 5) 2. P → a • P ⇒ ab ε ba (Rule 1) 3. P → b • P ⇒ abba – Recursion 4. P → aPa 5. P → bPb Back to CS Theory Context Free Grammars • What we did: • Let’s redefine grammars for CS Theory use: – At each step, we replaced the symbol P on the 1. Terminals = Set of symbols that form the strings of the language being defined right with one of the definitions of P from our rule set. 2. Variables = Set of symbols representing categories – Relating to our sentence example 3. Start Symbol = variable that represents the “base category” that defines our language • Categories were replaced by Symbols 4. Production rules = set of rules that recursively define • Final strings were replaced by Symbols the language • Start category was replaced by a symbol • We still have our set of rules. Context Free Grammars Context Free Grammars • Production Rules • Let’s formalize this a bit: – Of the form A → B – A context free grammar (CFG) is a 4-tuple: (V, T, P, S) where • A is a variable • B is a string, combining terminals and variables • V is a set of variables • To apply a rule, replace an occurance of A with the • T is a set of terminals string B. • P is a set of production rules • V and T are disjoint (I.e. V ∩ T = ∅ ) • S ∈ V, is your start symbol 4

  5. Context Free Grammars Context Free Grammars • Let’s formalize this a bit: • Let’s formalize this a bit: – Production rules – Production rules • Of the form A → β where • We say that the grammar is context-free since this – A ∈ V substitution can take place regardless of where A is. – β ∈ (V ∪ T) * string with symbols from V and T • We write α ⇒ * γ if γ can be derived from α in zero • We say that γ can be derived from α in one step: or more steps. – A → β is a rule – α = α 1 A α 2 – γ = α 1 β α 2 – α ⇒ γ Context Free Grammars Example • The language generated by a CFG • Find a CFG to describe: – L = {x ∈ {0,1} * | n 0 (x) = n 1 (x)} – Let G = (V, T, P, S) – The language generated by G, L(G) – Basic idea (define recursively) • L(G) = { x ∈ T * | S ⇒ * x} • ε is certainly in the language • For all strings in the language, if we add a 0 and 1 to the string, the result is in the language. – A language L is a Context Free Language • The concatenation of any two strings in the language (CFL) iff there is a CFG G, such that will also be in the language • L = L(G) Example Example • Find a CFG to describe: • Let’s derive a string from L – L = {x ∈ {0,1} * | n 0 (x) = n 1 (x)} – 00110011 • S → ε (1) – S ⇒ SS rule 4 • S → 0S1 (2) ⇒ 0S1 0S1 rule 2 • S → 1S0 (3) ⇒ 00S11 00S11 rule 2 • S → SS (4) ⇒ 00 ε 11 00 ε 11 rule 1 = 00110011 5

  6. Another example Another example • Find a CFG to describe: • Find a CFG to describe: – L = {a i b j c k | i = k} – L = {a i b j c k | i = k} • S → B (1) • Number of a’s equals the number of c’s with any • S → aSc (2) number of b’s between them • B → bB (3) • Use variable B to represent b j • B → ε (4) • Every time you add a to the left of B you need to – Can also write as add c to the right. • S → B | aSc • B → bB | ε Another example One more example • Let’s derive a string from L: aabbbcc • Defining the grammar for algebraic – S ⇒ aSc rule 2 expressions: S ⇒ aaScc rule 2 – Let a = a numeric constant S ⇒ aaBcc rule 1 – Set of binary operators = {+, -, *, /} S ⇒ aabBcc rule 3 – Expressions can be parenthesized S ⇒ aabbBcc rule 3 S ⇒ aabbbBcc rule 3 S ⇒ aabbb ε cc rule 4 = aabbb ε cc One more example One more example • Defining the grammar for algebraic • Defining the grammar for algebraic expressions: expressions – Production rules – S → S + S (1) – G = (V, T, P, S) S → S – S (2) S → S * S (3) – V = {S} S → S / S (4) – T = { a, -, +, *, /, (, ) } S → (S) (5) – S = S S → a (6) – P = see next slide 6

  7. One more example Practical uses for grammars • Show derivation for a + (a * a) / a • How a compiler works – S ⇒ S + S rule 1 S ⇒ a + S rule 6 Stream Parse S ⇒ a + S / S rule 4 lexer parser codegen of tokens Tree S ⇒ a + (S) / S rule 5 S ⇒ a + (S * S) / S rule 3 Object code S ⇒ a + (a * S) / S rule 6 Source S ⇒ a + (a * a) / S rule 6 file S ⇒ a + (a * a) / a rule 6 A real practical example A real practical example • Grammars for programming languages • Grammars for programming languages – <stmt> → … | <for-stmt> | <if-stmt> | … – Keywords and punctuation are terminals – <stmt> → { <stmt> <stmt> } | ε – Program constructs are variables – <if-stmt> → if ( <expr> ) then <stmt> – Production rules define the syntax of the – <for-stmt> → for ( <expr> ; <expr> ; <expr> ) language <stmt> – This is really the second step in building a compiler! Another real practical example Summary • Next class of languages Context Free • Anybody here work with XML? Languages. • DTDs in XML can be expressed as – Described by Context Free Grammars grammars. • G = (V, Σ , S, P) – Use parsing technology to parse / represent XMK DTDs. • Questions? • See Section 5.3.4 for details. • Let’s Break! 7

Recommend


More recommend