context free languages i
play

Context-free languages I In Chapter 1 we consider two ways to - PowerPoint PPT Presentation

Context-free languages I In Chapter 1 we consider two ways to describe languages automata & regular expressions Context-free grammars (CFG) More powerful than automata CFG is used in compilers and interpreters for parsers to read programs


  1. Context-free languages I In Chapter 1 we consider two ways to describe languages automata & regular expressions Context-free grammars (CFG) More powerful than automata CFG is used in compilers and interpreters for parsers to read programs September 26, 2020 1 / 12

  2. Context-free grammars I A grammar G 1 : A → 0 A 1 A → B B → # Each one is called a substitution rule Variables: A , B (capital letters) Terminals: 0, 1, # (lowercase letters, number, special symbols) Start variable: A September 26, 2020 2 / 12

  3. Context-free grammars II A grammar: a collection of substitution rules Derivation: G 1 generates 000#111 A ⇒ 0 A 1 ⇒ 00 A 11 ⇒ 000 A 111 ⇒ 000 B 111 ⇒ 000#111 Parse tree Fig 2.1 September 26, 2020 3 / 12

  4. Context-free grammars III A A A A B 0 0 0 # 1 1 1 September 26, 2020 4 / 12

  5. Context-free grammars IV L ( G ): language of grammar For the CFG example we just discussed, L ( G 1 ) = { 0 n #1 n | n ≥ 0 } CFG is more powerful than regular expressions because we showed earlier that this language is not regular Representation of rules: A → 0 A 1 and A → B is often simplified to September 26, 2020 5 / 12

  6. Context-free grammars V A → 0 A 1 | B Example � S � ⇒ � Noun-Phrase �� Verb-Phrase � ⇒ � Complex-Noun �� Verb-Phrase � ⇒ � Article �� Noun �� Verb-Phrase � ⇒ a � Noun �� Verb-Phrase � ⇒ a boy � Verb-Phrase � ⇒ a boy � Complex-Verb � ⇒ a boy � Verb � ⇒ a boy sees September 26, 2020 6 / 12

  7. Context-free grammars VI Why called “context-free” ? Rules independent of context September 26, 2020 7 / 12

  8. Formal definition of a context-free grammar I ( V , Σ , R , S ) V : variables, finite set Σ: terminals, finite set R : rules variable → strings of variables and terminals (including ǫ ) S ∈ V , start variable September 26, 2020 8 / 12

  9. Formal definition of a context-free grammar II For the example G 1 : A → 0 A 1 A → B B → # V = { A , B } , Σ = { 0 , 1 , # } , S = A , R : the above three rules September 26, 2020 9 / 12

  10. Derivation of strings I If u , v , w are strings and a rule A → w is applied, then we say uAv yields uwv and this is denoted as uAv ⇒ uwv if u = v or u ⇒ u 1 ⇒ · · · ⇒ u k ⇒ v then we say ∗ u ⇒ v September 26, 2020 10 / 12

  11. Derivation of strings II Language of a CFG { w ∈ Σ ∗ | S ∗ ⇒ w } September 26, 2020 11 / 12

  12. Example 2.3 I G 3 = ( { S } , { a , b } , R , S ) R: S → aSb | SS | ǫ What is the language? If we treat a , b respectively as ( and ), then we have all valid nested parentheses September 26, 2020 12 / 12

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