Re: Using Rules Today

2006-07-07 Thread Flavio S. Glock

2006/7/5, Joshua Gatcomb <[EMAIL PROTECTED]>:


I have not had a chance to look at Flavio's links yet.  Since no one who
actually knows rules seemed to be inspired to write an example for me - I
will *eventually* figure it out on my own and post back to the list as an
FYI.


Here is a simple one that handles '+' and '*' - it could be simplified
a bit, but this one works for me:
---
use Data::Dumper;
use strict;
use Pugs::Compiler::Token;
use Pugs::Runtime::Match::Ratchet;
use base 'Pugs::Grammar::Base';

Pugs::Compiler::Token->install( 'mul','
   (+)
   [   \\*   { return ( $/[0]()*$/{mul}() ) }
   |   <\'\'>
   ]
' );
Pugs::Compiler::Token->install( 'sum','
   
   [   \\+   { return $/{mul}()+$/{sum}() }
   |   <\'\'> { return $/{mul}() }
   ]
' );

my $s = shift;
my $match = main->sum( $s );
print $match->();
---

- Flavio S. Glock


Re: Using Rules Today

2006-07-05 Thread Joshua Gatcomb

On 7/3/06, Paul Seamons <[EMAIL PROTECTED]> wrote:



It isn't specifically a parser designed for general language parsing, but
CGI::Ex::Template does have a mathematical expression parser.



Thanks, but this falls into the realm of existing wheels which is a
different part of this project.



perl -e 'use CGI::Ex::Template; $s=CGI::Ex::Template::dump_parse("3 * 4 **
2 +
5"); $s =~ s/\s+/ /g; print "$s\n"'


$VAR1 = [ \[ '+', [ \[ '*', '3', [ \[ '**', '4', '2' ], 0 ] ], 0 ], '5' ],

0 ];



Ok, but where is the evaluation?  I know it wouldn't be too hard to write
something that could evaluate this data structure to produce the correct
results but that is what this project is all about.  Some parsers make this
easier than others.  Some allow the expression to be evaluated as it is
parsed while some require additional homegrown code to parse the resulting
parse tree (data structure).

I have not had a chance to look at Flavio's links yet.  Since no one who
actually knows rules seemed to be inspired to write an example for me - I
will *eventually* figure it out on my own and post back to the list as an
FYI.

Paul Seamons


Cheers,
Joshua Gatcomb
a.k.a. Limbic~Region


Re: Using Rules Today

2006-07-03 Thread Paul Seamons
> In any case, I was wondering if someone could provide me with an example of
> a mathematical expression parser (and evaluator).
> To properly compare to the others, it would need to handle the following
> operators
>
> +, - (left associative)
> *, /, % (left associative)
> ^ (right associative)
>
> handle parens 12 - (3 + 4)
>
> handle two functions sqrt() and abs() both of which must include the
> parens.
>
> If someone has time to do this for me, I would be appreciative.  It might
> also serve as example documentation or cookbook ideas.
>
> I am specifically interested in examples that can be run in Perl 5 today
> without needing Pugs or Parrot.

It isn't specifically a parser designed for general language parsing, but 
CGI::Ex::Template does have a mathematical expression parser.  The parser is 
located near the end of the parse_expr method.  The output of the parse_expr 
is an opcode data structure that can be played out through the play_expr 
method.

The basic functionality is that a chain of operators is tokenized into a 
single array.  The apply_precedence method is then used to split the array
into the optree based upon the precedence and associativity of the operators.

The following is a sampling of the resulting "optrees" for the given 
expressions (taken from the perldoc):

 1 + 2 =>   [ \ [ '+', 1, 2 ], 0]
 a + b =>   [ \ [ '+', ['a', 0], ['b', 0] ], 0 ]
 a * (b + c)   =>   [ \ [ '*', ['a', 0], [ \ ['+', ['b', 0], ['c', 0]], 0 ]], 
0 ]
 (a + b)   =>   [ \ [ '+', ['a', 0], ['b', 0] ]], 0 ]
 (a + b) * c   =>   [ \ [ '*', [ \ [ '+', ['a', 0], ['b', 0] ], 0 ], ['c', 
0] ], 0 ]

perl -e 'use CGI::Ex::Template; $s=CGI::Ex::Template::dump_parse("3 * 4 ** 2 + 
5"); $s =~ s/\s+/ /g; print "$s\n"'

$VAR1 = [ \[ '+', [ \[ '*', '3', [ \[ '**', '4', '2' ], 0 ] ], 0 ], '5' ], 
0 ];

I apologize that the expression parsing isn't a little more abstracted for 
you, but the result should be usable.  Also, the parse_expr is designed for 
also parsing variable names in the TT2 language, so the first portion of the 
method applies variable names.  The entire thing could be cut down 
considerably if all you want to parse is math (no variables).

Paul Seamons


Re: Using Rules Today

2006-07-03 Thread Flavio S. Glock

2006/7/3, Joshua Gatcomb <[EMAIL PROTECTED]>:


I am specifically interested in examples that can be run in Perl 5 today
without needing Pugs or Parrot.


http://svn.openfoundry.org/pugs/perl5/Pugs-Compiler-Rule/compile_p6grammar.pl
- doesn't do exactly what you want, but you can see what the syntax
looks like for writing an evaluator using rules in p5.

http://svn.openfoundry.org/pugs/perl5/Pugs-Compiler-Rule/lib/Pugs/Grammar/Rule/Rule.pm
- this is the grammar for rules, written in rules.
The last rules have looser precedence; the rules at the start of the
file have tighter precedence.
This grammar is compiled to p5 using lrep.

- Flavio S. Glock


Using Rules Today

2006-07-03 Thread Joshua Gatcomb

All:
I have a for-fun project that I am working on exploring various different
parsers and their methods.  So far I have looked at things like
Parse::RecDescent, Parse::YAPP, Parse::Earley, and HOP::Parser.  I had
Perl6::Rules on my list, but it is my understanding that
Pugs::Compiler::Rule is more up to date.

In any case, I was wondering if someone could provide me with an example of
a mathematical expression parser (and evaluator).
To properly compare to the others, it would need to handle the following
operators

+, - (left associative)
*, /, % (left associative)
^ (right associative)

handle parens 12 - (3 + 4)

handle two functions sqrt() and abs() both of which must include the parens.

If someone has time to do this for me, I would be appreciative.  It might
also serve as example documentation or cookbook ideas.

I am specifically interested in examples that can be run in Perl 5 today
without needing Pugs or Parrot.

Cheers,
Joshua Gatcomb
a.k.a. Limbic~Region


Re: using rules

2005-06-09 Thread Patrick R. Michaud
On Sun, Jun 05, 2005 at 05:11:55PM +0200, BÁRTHÁZI András wrote:
> Thanks, it helped me!
> 
> More questions. ;) It seems to me, that the following constructs not 
> yet(?) implemented in Pugs. Is it true?

I'll note briefly here that questions regarding implementation (in Perl 6
or Pugs) generally belong on perl6-compiler, not on perl6-language.


> Built-in rules:
> 
>   
>   
>   etc.
> 
> Rule modifiers (:i modifier after the name of the rule):
> 
>   rule xxx :i {
> anything
>   }
> 
> It works well w/o ":i".

The rules engine is part of PGE (which Pugs incorporates), and
is not yet complete.  We'll be adding more features and built-in
assertions as time progresses.  At the moment I'm getting the
grammar class hierarchies ironed out so that I can then properly
add the built-ins.

Pm


Re: using rules

2005-06-05 Thread BÁRTHÁZI András

Hi,


I'll take a shot at it since no one else seems to want to. :-)

> Hope this is helpful.  Corrections are welcome from anyone who spots
> any mistakes.

Thanks, it helped me!

More questions. ;) It seems to me, that the following constructs not 
yet(?) implemented in Pugs. Is it true?


Built-in rules:

  
  
  etc.

Rule modifiers (:i modifier after the name of the rule):

  rule xxx :i {
anything
  }

It works well w/o ":i".

Bye,
  Andras


Re: using rules

2005-06-05 Thread Aankhen
I'll take a shot at it since no one else seems to want to. :-)

On 6/3/05, BÁRTHÁZI András <[EMAIL PROTECTED]> wrote:
> How can I catch the matched elem name, and block content? I'm guessing
> that hypotetical variables can be the solution, but it says, that those
> variables should have been defined, before I use them, and it's not that
> case.

Named subrules have their values stored in the $/ match object under a
key of the same name.  So, in this case:

$foo ~~ m//;

$/ will contain the matched block, and $/[1] will
contain the block content (i.e. the content of the second capturing
group).  The working is the same in the case of s///, so you can
probably safely use $/ within your &trigger_block and &trigger_elem.

Hypothetical variables work like this:

   my $bar; # not quite sure you need to initialise it; just in case :-)
   $foo ~~ m/$bar := (bar|baz)/;

Now $bar will contain either "bar" or "baz", depending on which one was matched.

Hope this is helpful.  Corrections are welcome from anyone who spots
any mistakes.

Aankhen


Re: using rules

2005-06-05 Thread BÁRTHÁZI András

Hi,

No ideas?

Bye,
  Andras

I'm working on a web templating system, and I'm wondering how should I 
use rules?


I have these defs:

rule elem {
\< wts \: (<[a..z]>+) \/ \>
}

rule block {
\< wts \: (<[a..z]>+)\>(.*?)\< \/ wts \: $1 \>
}

I would like to execute subroutines during the evaluation. What should I 
do? Is the following the right way?


 given $template {

   s//{trigger_block()}/;
   s//{trigger_elem()}/;

 }

How can I catch the matched elem name, and block content? I'm guessing 
that hypotetical variables can be the solution, but it says, that those 
variables should have been defined, before I use them, and it's not that 
case.


using rules

2005-06-03 Thread BÁRTHÁZI András

Hi,

I'm working on a web templating system, and I'm wondering how should I use 
rules?


I have these defs:

rule elem {
\< wts \: (<[a..z]>+) \/ \>
}

rule block {
\< wts \: (<[a..z]>+)\>(.*?)\< \/ wts \: $1 \>
}

I would like to execute subroutines during the evaluation. What should I 
do? Is the following the right way?


 given $template {

   s//{trigger_block()}/;
   s//{trigger_elem()}/;

 }

How can I catch the matched elem name, and block content? I'm guessing 
that hypotetical variables can be the solution, but it says, that those 
variables should have been defined, before I use them, and it's not that 
case.


Bye,
  Andras


using Rules with undefined values (was Re: some misc Perl 6 questions)

2005-03-09 Thread Darren Duncan
At 9:08 AM -0800 3/9/05, Larry Wall wrote:
My other quibble is that you seem to be prone to stating things in the
negative for at least two of your three tests here:
subtype KeyName of Str where { $_.defined and $_ ne '' and $_ !~ m/\W/ }
and it seems to me that you could simplify all that to just
subtype KeyName of Str where { m/^\w+$/ }
If that succeeds, you know it's defined and non-null.  You might argue that
the m/\W/ short-circuits, but I would counter-argue that failure is
supposed to be the exceptional case, and in every successful call you have
to scan the whole string anyway.  Plus it's just easier to understand.
And it lets you write the constraint without explicit reference to $_,
which I will admit was my first motivation in wanting to rewrite your
constraint.  The negatives and redundancies I only noticed later.
Okay, I have gone and replaced the "$_ ne '' and $_ !~ m/\W/" with a 
"m/^\w+$/".

However, unless Perl 6 is different than Perl 5 in its treatment of 
undefined values, I still need the ".defined" part, meaning I end up 
with:

  subtype KeyName of Str where { .defined and m/^\w+$/ }
In Perl 5, running something like the un-gated "$msg_key =~ m/^\w+$/" 
gives this warning:

  Use of uninitialized value in pattern match (m//) at 
Locale/KeyedText.pm line 85.

To avoid that, I do "defined( $msg_key ) and $msg_key =~ m/^\w+$/" in Perl 5.
You have mentioned before that Perl 6 still treats the use of 
undefined values other than in boolean context as an error.  So would 
applying a Rule against an undefined value produce a warning in Perl 
6 or not?  (A quick scan of S12 didn't say.)

-- Darren Duncan