I was rereading S5, and the example of grammatical inheritance caught my
eye:

grammar Letter {
    rule greet :w { [Hi|Hey|Yo] $to:=(\S+?) , $$}
    ...
}

grammar FormalLetter is Letter {
    rule greet :w { Dear $to:=(\S+?) , $$}
    ...
}

My first reaction was that we need a bit more factoring here. I assume we
could do:

grammar Letter {
    rule greet :w { <greet_word> $to:=(\S+?) , $$}
    rule greet_word { [Hi|Hey|Yo] }
    ...
}

grammar FormalLetter is Letter {
    rule greet_word{ Dear }
    ...
}

Will the :w do the right thing here?


My second though was that inheritance might be overused here. My refactoring
was the "template method" pattern. How would I use a strategy instead. I
know I can define a rule that accepts a parameter, but if I did that then
I'd need to pass that parameter all the way down the tree. Is it possible to
write something like

  $letter =~ /<(Letter but greet_word(rule :w { Guten Tag })).text>;

?


Dave.


Reply via email to