a f s d Objectives Conversion to Right-Linear Grammar Objectives Conversion to Right-Linear Grammar Objectives You should be able to ... Right-Linear Grammars Dr. Mattox Beckman ◮ Convert between a regular expression and a right-linear grammar. University of Illinois at Urbana-Champaign Department of Computer Science Objectives Conversion to Right-Linear Grammar Objectives Conversion to Right-Linear Grammar Right-Linear Grammars Example 1 A right-linear grammar is one in which every production has the form ◮ Regular expression: asdf A → x ◮ State machine: or q 0 q 1 q 2 q 3 q 4 start A → xB or ◮ Grammar: S 0 → a S 1 A → B S 1 → s S 2 S 2 → d S 3 where A and B are arbitrary (possibly identical) nonterminal symbols, and x is an arbitrary S 3 → f S 4 terminal symbol. S 4 → ◮ “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.
b a d d a s d f f b a b b a b a b a s s Objectives Conversion to Right-Linear Grammar Objectives Conversion to Right-Linear Grammar Example 2 Going from Regular Expression to Right-Linear Grammar ◮ Regular expression: a(s|d)+f ◮ One way: regular expression → NFA → DFA → RLG ◮ Aonther way: direct conversion. We’ll use a “bottom up” strategy. S 0 → a S 1 Characters To convert a single character a , we make a simple production. q 2 S 1 → s S 2 S → a d S 3 | where S is the start symbol. S 2 → s S 2 q 0 q 1 q 4 start Concatenation To concatenate two regular expressions, add the second start symbol to the d S 3 | f S 4 end of any “accepting” states from the fjrst grammar. | S 3 → s S 2 Regexp: ab Regexp: a Regexp: b q 3 d S 3 | S 1 → a S 2 S 1 → S 2 → f S 4 | S 2 → Objectives Conversion to Right-Linear Grammar Objectives Conversion to Right-Linear Grammar Choice and Repetition Choice and Repetition Choice To choose between two regular expressions, add a new start symbol that “picks” one of the choices. Regexp: a|b Kleene Star If S is the start symbol, then for every rule of the form A → x (“accepting Regexp: a Regexp: b S → S 1 | S 2 states”) add another rule of the form A → x S . Also add an ǫ rule. S 1 → S 1 → S 2 → S 2 → Regexp: a|b Regexp: (a|b)* 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 ǫ S → S 1 | S 2 S → S 1 | S 2 | ǫ productions fjrst. S 1 → S 1 → a | a S S 2 → S 2 → b | b S 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
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
Recommend
More recommend