your questions
play

Your Questions? Previous class days' Your material (and - PowerPoint PPT Presentation

4/3/2018 Your Questions? Previous class days' Your material (and exercises) parents MA/CSSE 474 Reading Assignments will Theory of Computation HW 9 problems probably Exam 2 concur! Anything else Intro to


  1. 4/3/2018 Your Questions? • Previous class days' Your material (and exercises) parents MA/CSSE 474 • Reading Assignments will Theory of Computation • HW 9 problems probably • Exam 2 concur!… • Anything else Intro to Context-free Grammars 1

  2. 4/3/2018 Context-free Grammars, Languages, Shorthand notation and PDAs S   S  a T Context-free L S  b T Language T  a T  b Context-free T  a S Accepts Grammar T  b S Can be abbreviated by S   | a T | b T T  a | b | a S | b S PDA 2

  3. 4/3/2018 Context-free Grammar Formal Definition Formal Definitions: Derivations, Context-free Languages A CFG G=(V,  , R, S) (Each part is finite) x  G y iff x =  A   is the terminal alphabet ; it contains the set of symbols that make up the strings in L ( G ), and and A   is in R N (our textbook does not use this name, but I will) is the y =    nonterminal alphabet : a set of working symbols that G w 0  G w 1  G w 2  G . . .  G w n is a derivation in G . uses to structure the language. These symbols disappear by the time the grammar finishes its job and Let  G * be the reflexive, transitive closure of  G .  ∩ N =  . generates a string. Note: Then the language generated by G , denoted L ( G ), is: Rule alphabet (vocabulary): V =  ∪ N { w   * : S  G * w }. • R : A finite set of productions of the form A  β , where A language L is context-free if there is some A ∊ N and β ∊ V* Rules are also known as productions . context-free grammar G such that L = L ( G ) . G has a unique start symbol , S ∊ N 3

  4. 4/3/2018 Regular Grammars In a regular grammar, every rule (production) in R Regular Grammars must have a right-hand side that is: ●  , or ● a single terminal, or ● a single terminal followed by a single nonterminal. Regular: S  a , S   , and T  a S Not regular: S  a S a and S  T A brief side-trip into Chapter 7 4

  5. 4/3/2018 Regular Languages and Regular Grammars Regular Grammar Example L = { w  { a , b }* : | w | is even} (( aa )  ( ab )  ( ba )  ( bb ))* Theorem: A language is regular iff it can be defined by a regular grammar. S   Proof: By two constructions. S  a T Derive abbb S  b T from this T  a grammar T  b T  a S T  b S 5

  6. 4/3/2018 Regular Languages and Regular Grammars Recursive Grammar Rules Regular grammar  FSM: grammartofsm ( G = ( V ,  , R , S)) = • A rule is recursive iff it is X  w 1 Yw 2 , where: 1. Create in M a separate state for each nonterminal in V . Y  * w 3 Xw 4 for some w 1 , w 2 , w 3 , and w 4 in V *. 2. Start state is the state corresponding to S . 3. If there are any rules in R of the form X  a , for some • A grammar G is recursive iff G contains at least one a   , create a new state labeled #. recursive rule. 4. For each rule of the form X  a Y , add a transition from X to Y labeled a . S  ( S ) S  ( T ) • Examples: 5. For each rule of the form X  a , add a transition from X T  ( S ) to # labeled a . 6. For each rule of the form X   , mark state X as accepting. S  bS, S  aT In general, non-recursive 7. Mark state # as accepting. T  aS, T  b, T  ε grammars are boring! FSM  Regular grammar: Similar. Essentially reverses this procedure. 6

  7. 4/3/2018 Self-Embedding Grammar Rules • A rule in a grammar G is self-embedding iff it is : Where Context-Free Grammars X  w 1 Yw 2 , where Y  * w 3 Xw 4 and What is the Get Their Power difference between both w 1 w 3 and w 2 w 4 are in  + . self-embedding and recursive ? • If a CFG G is not self-embedding then L ( G ) is regular. • A grammar is self-embedding iff it contains at least one self-embedding rule. • If a language L has the property • Examples: S  a S a self-embedding that every grammar that defines it is self-embedding, then L is not S  a S recursive but not self-embedding regular. S  a T T  S b self-embedding 7

  8. 4/3/2018 Derivation Tree Structure • Consider our grammar for Bal: Context free languages: S  (S) | ε | SS We care about structure. E • Draw a derivation tree (a.k.a. Parse tree) E + E for the string (())(()()) E * E id 3 id id 5 7 8

  9. 4/3/2018 Hints for designing context- L = {a n b n c m : n , m  0} free grammars • Generate concatenated regions: L = { : k  0 ∧  i ≤ k (n i  0)} n n n n n n a b a b ... a b 1 1 2 2 k k A  BC L = { a n b m : n  m } • Generate outside in: A  aAb L = { w  { a , b }*: # a ( w ) = # b ( w )} • Union of two sets: A  B | C 9

  10. 4/3/2018 BNF CFG for Simple Arithmetic Expressions G = ( V ,  , R , E ), where A notation for writing practical context-free grammars V = {+, *, (, ), id , E },  = {+, *, (, ), id }, • The symbol | should be read as “or”. R = { E  E + E Example: S  a S b | b S a | SS |  E  E  E E  ( E ) • Allow a nonterminal symbol to be any sequence E  id of characters surrounded by angle brackets. } Examples of nonterminals: <program> Derive id + id * id <variable> 10

  11. 4/3/2018 Spam Generation BNF for a Java Fragment <block> ::= {<stmt-list>} | {} <stmt-list> ::= <stmt> | <stmt-list> <stmt> <stmt> ::= <block> | while (<cond>) <stmt> | if (<cond>) <stmt> | do <stmt> while (<cond>); | <assignment-stmt>; | These production rules yield 1,843,200 possible spellings. return | How Many Ways Can You Spell V1@gra? By Brian Hayes return <expression> | American Scientist , July-August 2007 <method-invocation>; http://www.americanscientist.org/template/AssetDetail/assetid/55592 11

  12. 4/3/2018 HTML English S  NP VP NP  the Nominal | a Nominal | Nominal | <ul> <li> Item 1, which will include a sublist </li> ProperNoun | NP PP <ul> Nominal  N | Adjs N <li> First item in sublist </li> <li> Second item in sublist </li> N  cat | dogs | bear | girl | chocolate | rifle </ul> ProperNoun  Chris | Fluffy <li> Item 2 </li> </ul> Adjs  Adj Adjs | Adj A grammar: Adj  young | older | smart /* Text is a sequence of elements. VP  V | V NP | VP PP HTMLtext  Element HTMLtext |  V  like | likes | thinks | shoots | smells Element  UL | LI | … (and other kinds of elements that are allowed in the body of an HTML document) PP  Prep NP /* The <ul> and </ul> tags must match. Prep  with UL  <ul> HTMLtext </ul> /* The <li> and </li> tags must match. LI  <li> HTMLtext </li> 12

  13. 4/3/2018 Prove the Correctness of a Grammar A n B n = { a n b n : n  0} G = ({ S , a , b }, { a , b }, R , S ), R = { S  a S b S   } ● Prove that G generates only strings in L. ● Prove that G generates all the strings in L . 13

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