Review • Languages and Grammars CS 301 - Lecture 5 – Alphabets, strings, languages • Regular Languages Regular Grammars, Regular – Deterministic Finite Automata Languages, and Properties of – Nondeterministic Finite Automata Regular Languages – Equivalence of NFA and DFA – Regular Expressions Fall 2008 • Today: – Regular Grammars and Regular Languages – Properties of Regular Languages Grammars Grammar Notation • Grammars express languages Production Rules • • Example: the English language sentence noun _ phrase predicate noun cat → → noun dog → noun _ phrase article noun → Variable Terminal predicate verb → 1
Some Terminal Rules A Resulting Sentence article a → sentence noun _ phrase predicate ⇒ article the → noun _ phrase verb ⇒ article noun verb ⇒ noun cat → the noun verb ⇒ noun dog → the dog verb ⇒ the dog walks ⇒ verb runs → verb walks → The Resulting Language Definition of a Grammar G V , T , S , P ( ) = L = { “a cat runs”, “a cat walks”, V : Set of variables “the cat runs”, “the cat walks”, T : Set of terminal symbols “a dog runs”, S : Start variable “a dog walks”, “the dog runs”, P : Set of Production rules “the dog walks” } 2
A Simple Grammar Example Grammar Notation S aSb • Grammar: → S aSb → S → λ S → λ G V , T , S , P ( ) = ab • Derivation of sentence : S aSb ab ⇒ ⇒ V = { S } T = { a , b } S S → aSb → λ P { S aSb , S } = → → λ Deriving Strings in the Grammar Sentential Form • Grammar: • A sentence that contains variables and S aSb → terminals S → λ aabb S aSb aaSbb aaaSbbb aaabbb • Derivation of sentence : ⇒ ⇒ ⇒ ⇒ S aSb aaSbb aabb ⇒ ⇒ ⇒ Sentential Forms sentence S S → aSb → λ 3
General Notation for Derivations Why Notation Is Useful * w w • In general we write: 1 ⇒ n • We can now write: * w w w w ⇒ ⇒ ⇒ ⇒ • If: 1 2 3 n S aaabbb ⇒ * • Instead of: w w • It is always the case that: ⇒ S aSb aaSbb aaaSbbb aaabbb ⇒ ⇒ ⇒ ⇒ Example Language Language of a Grammar S aSb → Grammar can produce some set of strings S → λ Set of strings over an alphabet is a language Consider the set of all strings that can derived from this grammar….. Language of a grammar is all strings produced by the grammar S aSb aaSbb aaaSbbb aaabbb ⇒ ⇒ ⇒ ⇒ ∗ L ( G ) { w : S w } = ⇒ S aSb aaSbb aaaSbbb ⇒ ⇒ ⇒ aaaaSbbbb aaaabbbb ⇒ ⇒ String of terminals What language is being described? 4
The Resulting Language Linear Grammars S aSb → • Grammars with at most one variable at S → λ the right side of a production Always add on a and b on each side resulting in: a’s at the left b’s at the right equal number of a’s and b’s • Examples: S aSb → The image cannot be displayed. S → λ Your computer may not have enough memory to open the image, A Non-Linear Grammar Another Linear Grammar Grammar : G S SS → G Grammar : S A → S → λ A aB | → λ S aSb → B Ab → S bSa → L ( G ) { w : n ( w ) n ( w )} n n = = L ( G ) { a b : n 0 } a b = ≥ Number of in string 5
Left-Linear Grammars Right-Linear Grammars • All productions have form: • All productions have form: A → Bx A → xB or or A → x A → x • Example: S Aab → • Example: string of S abS A Aab | B → → string of terminals S a terminals B a → → Regular Grammars • A regular grammar is any right-linear or Regular Grammars left-linear grammar • Examples: S Aab S abS → → A Aab | B S a → → B a → What languages are generated by these grammars? 6
Languages and Grammars S Aab → S abS → Regular Grammars A Aab | B → S a → Generate B a → Regular Languages L ( 1 = G ) ( ab ) * a L ( G ) aab ( ab ) * = 2 Note both these languages are regular we have regular expressions for these languages (above) we can convert a regular expression into an NFA (how?) we can convert an NFA into a DFA (how?) we can convert a DFA into a regular expression (how?) Do regular grammars also describe regular languages?? Theorem - Part 1 Theorem = Languages Languages Regular Regular Generated by Generated by Languages Languages Regular Grammars Regular Grammars Any regular grammar generates a regular language 7
The case of Right-Linear Proof – Part 1 Grammars Languages Regular Generated by Languages G • Let be a right-linear grammar Regular Grammars • We will prove: is regular L ( G ) The language generated by L ( G ) any regular grammar is regular G • Proof idea: We will construct NFA using the grammar transitions Step 1: Create States for Example Each Variable Given right linear grammar: M • Construct NFA such that every state is a grammar variable: 8
Step 2.1: Step 2.2: Edges for Productions Edges for Productions • Productions of the form • Productions of the form are only slightly harder…. Create row result in of states that derive w and end in Step 2.3: In General Edges for Productions • Productions of the form • Given any right-linear grammar, the previous Create row of states that derive w and procedure produces an NFA end in a final state – We sketched a proof by construction – Result is both a proof and an algorithm – Why doesn’t this work for a non linear grammar? • Since we have an NFA for the language, the right-linear grammar produces a regular language 9
Proof - Part 2 Any regular language is generated L by some regular grammar Languages G Regular Generated by Languages Regular Grammars Proof idea: Let be the NFA with . M L = L ( M ) L Any regular language is generated by some regular grammar G M Construct from a regular grammar G L ( M ) L ( G ) such that = Step 1: Convert Edges to Productions NFA to Grammar Example L • Since is regular there is an NFA b b q aq M → 0 1 a a a a q q q q bq q q → 0 1 2 q 1 1 1 2 0 q aq → b 1 2 λ This transition in the NFA b λ Looks a lot like a production rule q bq → q 2 3 q 3 3 10
Step 2: Step 2: Edges and Final States Edges and Final States λ λ b b q aq q aq → → 0 1 0 1 M M q bq a q bq a → a → a 1 1 1 1 q q q q q q 0 1 2 0 1 2 q aq q aq → → 1 2 1 2 b b λ λ q bq q bq → → 2 3 2 3 q q q q 3 q q 3 → → 3 1 3 1 q q If is a final state, add → λ → λ 3 3 But What About Left-Linear In General Grammars • Given any NFA, the previous procedure produces a • What happens if we reverse a left linear grammar as right linear grammar follows: – We sketched a proof by construction – Result is both a proof and an algorithm • Every regular language has an NFA Reverses to – Can convert that NFA into a right linear grammar – Thus every regular language has a right linear grammar Reverses to • The result is a right linear grammar. • Combined with Part 1, we have shown right linear – If the left linear grammar produced L, then what does the grammars are yet another way to describe regular resulting right linear grammar produce? languages 11
But What About Left-Linear Left-Linear Grammars Grammars Produce Regular Languages • Start with a Left Linear grammar that produces want to show is regular • The previous slide reversed the language! • Can produce a right linear grammar that produces Reverses to • All right linear grammars produce regular languages Reverses to so is a regular language • If the left linear grammar produced language , • The reverse of a regular language is regular so then the resulting right linear grammar produces Claim we just proved left linear grammars is a regular language! produce regular languages? Why? L L For regular languages and We say: Regular languages are closed under 1 2 we will prove that: L ∪ L L ∪ L Union: Union: 1 2 1 2 L 1 L L 1 L Concatenation: Concatenation: 2 2 Are regular Star: L * Star: L * 1 1 Languages R R Reversal: Reversal: L L 1 1 Complement: L Complement: L 1 1 Intersection: Intersection: L ∩ L L ∩ L 1 2 1 2 12
Example L Regular language L Regular language 1 M 2 1 a n L { a b } L M L L M L ( ) ( ) = b = = 1 1 2 2 1 n 0 ≥ M M NFA NFA 1 2 M 2 a b L = ba { } 2 Single final state Single final state Example Union n L L { a b } { ba } NFA for ∪ = ∪ M 1 2 • NFA for 1 n L { a b } = L ∪ L 1 1 2 a λ λ b λ λ M 2 λ λ L = { ba } λ λ 2 a b 13
Concatenation Example n n L L { a b }{ ba } { a bba } = = 1 2 NFA for L 1 L NFA for 2 M M n 1 2 L { a b } = 1 L = { ba } λ λ 2 a a b b λ λ Star Operation Example λ NFA for * L w w w w = NFA for 1 2 k 1 L * n λ ∈ L * { a b } * 1 = w L ∈ 1 i 1 M λ 1 n L { a b } = λ λ 1 a λ b λ λ λ 14
Recommend
More recommend