right linear grammars
play

Right-Linear Grammars Dr. Mattox Beckman University of Illinois at - PowerPoint PPT Presentation

Objectives Conversion to Right-Linear Grammar Right-Linear Grammars Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Objectives Conversion to Right-Linear Grammar Objectives You should be able to


  1. Objectives Conversion to Right-Linear Grammar Right-Linear Grammars Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science

  2. Objectives Conversion to Right-Linear Grammar Objectives You should be able to ... ◮ Convert between a regular expression and a right-linear grammar.

  3. Objectives Conversion to Right-Linear Grammar Right-Linear Grammars A right-linear grammar is one in which every production has the form A → x or A → xB or A → B where A and B are arbitrary (possibly identical) nonterminal symbols, and x is an arbitrary terminal symbol. ◮ “At most one nonterminal symbol in the right-hand side.” ◮ It turns out these are equivalent to NFAs! ◮ Have one nonterminal symbol for each state, one terminal symbol for each production.

  4. d a f s Objectives Conversion to Right-Linear Grammar Example 1 ◮ Regular expression: asdf ◮ State machine: q 0 q 1 q 2 q 3 q 4 start ◮ Grammar: S 0 → a S 1 S 1 → s S 2 S 2 → d S 3 S 3 → f S 4 S 4 → ǫ

  5. f a f d s s d d s Objectives Conversion to Right-Linear Grammar Example 2 ◮ Regular expression: a(s|d)+f S 0 → a S 1 q 2 S 1 → s S 2 d S 3 | S 2 → s S 2 q 0 q 1 q 4 start d S 3 | f S 4 | S 3 → s S 2 q 3 d S 3 | f S 4 |

  6. b a b Objectives Conversion to Right-Linear Grammar Going from Regular Expression to Right-Linear Grammar ◮ One way: regular expression → NFA → DFA → RLG ◮ Aonther way: direct conversion. We’ll use a “bottom up” strategy. Characters To convert a single character a , we make a simple production. S → a where S is the start symbol. Concatenation To concatenate two regular expressions, add the second start symbol to the end of any “accepting” states from the fjrst grammar. Regexp: ab Regexp: a Regexp: b S 1 → a S 2 S 1 → S 2 → S 2 →

  7. b a b a a b Objectives Conversion to Right-Linear Grammar Choice and Repetition Choice To choose between two regular expressions, add a new start symbol that “picks” one of the choices. Regexp: a|b Regexp: a Regexp: b S → S 1 | S 2 S 1 → S 1 → S 2 → S 2 → Kleene Plus If S is the start symbol, then for every rule of the form A → x (“accepting states”) add another rule of the form A → x S . You may have to remove ǫ productions fjrst. Regexp: a|b Regexp: (a|b)+ S → S 1 | S 2 S → S 1 | S 2 S 1 → S 1 → a | a S S 2 → S 2 → b | b S

  8. a b Objectives Conversion to Right-Linear Grammar Choice and Repetition Kleene Star If S is the start symbol, then for every rule of the form A → x (“accepting states”) add another rule of the form A → x S . Also add an ǫ rule. Regexp: a|b Regexp: (a|b)* S → S 1 | S 2 S → S 1 | S 2 | ǫ S 1 → S 1 → a | a S S 2 → S 2 → b | b S

  9. Objectives Conversion to Right-Linear Grammar Credits The algorithm for converting a regular expression to a right-linear grammar is based partly on the discussion here: http://vasy.inria.fr/people/Gordon.Pace/Research/Software/Relic/Transforma- tions/RE/toRG.html

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