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 ... ◮ Convert between a regular expression and a right-linear grammar.
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.
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 → ǫ
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 |
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 →
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
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
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