[EMAIL PROTECTED] wrote:
Hi all,

i make a interpreter for a query string. As a result i need something like:
'true'. or sometimes 'false' :-))


my input looks like:
((exp) and (exp)) and ((exp) or (exp))

i started with this script:
----
#!/usr/bin/perl

$reg = "(\\([^()]*\\))";
$str = "((a=1 )or(a =2))and(((a= 3)and(a = 4))or(  (a= 5)and(a= 6)))";

print "$reg\n";
while($str =~ /$reg/g){
        $match = $1;
        $match =~ s/\(/\\\(/;
        $match =~ s/\)/\\\)/;
        print "$match\n";
        $str =~ s/$match/ true /;
        print "$str\n";
        sleep(1);
}
exit;
------

is there a better regexp for this ? and what is the best way take tare to the 'and' 'or' things ???

Note: I do this for a scratch. at the end i need a java code, but i am better in perl for design, so i done it in perl first. I read something, that the perl regex is more powerfull than others.... so i must take care about this. Filaly it has to work in Java.

This is a job for a parser. You could use Parse::RecDec as Eugene Haimov pointed out, but I'm not sure there is a comparable library for Java. It certainly can't be done using regular expressions.
You are parsing a grammar something like:-


expr := factor
       | factor 'AND' expr
       | factor 'OR' expr
factor := ( expr )
       | something

and the something is the "a= 2" bit that you haven't really defined.

my $next;

sub factor()
{
  if ($next = '(') {
    lex();
    expr();
    lex();
  } else {
    something();
  }
}

sub expr()
{
  factor();
  if($next = 'and' || $next = 'or') {
    # code to deal with and & or goes here.
    lex();
    expr();
  }
}

sub lex()
{
  my $ret = $next;
  if ($_ =~ s/^\(//)   { $next =  "("; }
  elsif ($_ =~ s/^\)//)   { $next =  "("; }
  elsif ($_ =~ s/^or|^OR//)   { $next =  "or"; }
  elsif ($_ =~ s/^and|^AND//)  { $next =  "and"; }
  elsif ($_ =~ s/^\w+//)   {$next =  "x"; }
  return $ret;
}

This is of course completely untested.



THANX FOR HELP ANS COMMENTS ON MY SNIPE. Kris
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs




--
Robert Thorpe
Software Engineer
Antenova Limited
Far Field House
Albert Road
Stow-cum-Quy
Cambridge
CB5 9AR, UK

t: +44 (0)1223 810603
f: +44 (0)1223 810650
m: +44 (0)7866 552477

www.antenova.com

The information in this e-mail is for use by the addressee(s) only. If you
are not the intended recipient, please notify us immediately and delete the
message from your computer. No liability or responsibility is accepted for
viruses and it is your responsibility to scan attachments (if any).
Opinions and views expressed in this email are those of the sender and do
not reflect the opinions and views of Antenova Ltd.

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to