S5: grammar compositions

2004-09-15 Thread Dave Whipp
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.




Re: S5: grammar compositions

2004-09-15 Thread Luke Palmer
Dave Whipp writes:
 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?

In the new S5 revision, :w changed from static to dynamic.  :w is about
the text now, not the rules.  So, yes, it will.

 My second though was that inheritance might be overused here. 

Perhaps, perhaps not.  In the multimethod paper that's coming soon (it's
turned out to be a lot more than just a multimethod paper; more of an
object model paper), inheritance may become much more useful than it
once was, at least in an abstract sense.  In particular, subtypes can be
a form of inheritance in the system.  And the best part is that we don't
have to change anything from S12 to apply the model.

But that's beside the point.   I just had to give all y'all a little
preview.

It's my suspicion that the template method pattern is often going to be
the right thing to do in rules.

 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;

Hmm... this pattern seems a lot kludgier for this situation.  You could
use a parameterized grammar, I suppose:

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

Which may be a better solution if you're expecting a lot of different
types.

By the way, you can't say:

$letter ~~ /( ...

Because ( starts a conditional assertion.  The usage becomes:

$letter ~~ /Letter[/:w Guten Tag /].greet/

Luke


Re: S5: grammar compositions

2004-09-15 Thread Larry Wall
Grammar roles?

Larry


S5: array interpolation

2004-09-15 Thread John Siracusa
 An interpolated array:
 
 / @cmds /
 
 is matched as if it were an alternation of its elements:
 
 / [ @cmds[0] | @cmds[1] | @cmds[2] | ... ] /
 
 As with a scalar variable, each one is matched as a literal.

Like this?  (Assuming single quotes don't interpolate @foo[...])

@a = ('a', 'b', 'c');

'@a[0]' ~~ m:/ @a /; # true
'@a[2]' ~~ m:/ @a /; # true
'@a[9]' ~~ m:/ @a /; # false

If so, that seems pretty wacky...

-John




Re: S5: array interpolation

2004-09-15 Thread Brent 'Dax' Royal-Gordon
John Siracusa [EMAIL PROTECTED] wrote:
  An interpolated array:
 
  / @cmds /
 
  is matched as if it were an alternation of its elements:
 
  / [ @cmds[0] | @cmds[1] | @cmds[2] | ... ] /
 
  As with a scalar variable, each one is matched as a literal.
 
 Like this?  (Assuming single quotes don't interpolate @foo[...])
 
 @a = ('a', 'b', 'c');
 
 '@a[0]' ~~ m:/ @a /; # true
 '@a[2]' ~~ m:/ @a /; # true
 '@a[9]' ~~ m:/ @a /; # false

I think he means as opposed to a subrule.  In Perl 5 terms, there's
an implicit \Q\E around each value in the array.

-- 
Brent 'Dax' Royal-Gordon [EMAIL PROTECTED]
Perl and Parrot hacker

There is no cabal.
[I currently have a couple Gmail invites--contact me if you're interested.]


[S3, S4, S5]: =~ becomes ~~

2004-09-15 Thread Herbert Snorrason
I know that, you know that ... but the synopses never actually say it.
It's evident from context, but it's never said explicitly. I would
*think* that should be in the Operator renaming section of S3, and
presume this is an oversight?
--
Schwäche zeigen heißt verlieren;
härte heißt regieren.
  - Glas und Tränen, Megaherz


Re: [S3, S4, S5]: =~ becomes ~~

2004-09-15 Thread Luke Palmer
Herbert Snorrason writes:
 I know that, you know that ... but the synopses never actually say it.
 It's evident from context, but it's never said explicitly. I would
 *think* that should be in the Operator renaming section of S3, and
 presume this is an oversight?

Okay, it ought to be there soon.  I added it in the New operators
section, since it's pretty different from =~.

Arguably the ~~ table should go in S3 instead of S4.

Luke


Re: S5: grammar compositions

2004-09-15 Thread chromatic
On Wed, 2004-09-15 at 12:47, Larry Wall wrote:

 Grammar roles?

It seems sensible, having said Here's a better method of type checking
and code re-use and Here's a generalization of pattern matching to
make it more like programming.

Not doing it would be like making closures that can't write to the
closed-over variables.  That's just crazy!

-- c