CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
recursive matching

 



sskb
newbie

Mar 21, 2002, 9:43 PM

Post #1 of 5 (468 views)
recursive matching Can't Post

hi all,
I am stuck up with a situation like follows...

i have a line in a file like this

(a (b (c) d) e)

now i need to separate
var1 = c
var2 = b(c)d and
var3 = a (b (c) d) e
.
.
so on...
and I am not sure how many such vars will be there...
so how to extract this?

thanks for any help.

Frown


mhx
Enthusiast / Moderator

Mar 22, 2002, 11:15 AM

Post #2 of 5 (465 views)
Re: [sskb] recursive matching [In reply to] Can't Post

Could you give some more examples of how the lines could look like? Or perhaps even a more detailed description of the syntax? Which parts are optional, which parts can be repeated? Would something like this


Code
((a b) c d (e (f g (h)) i j))


be possible, and if yes what would you like to end up with?

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Paul
Enthusiast

Mar 24, 2002, 6:28 AM

Post #3 of 5 (453 views)
Re: [sskb] recursive matching [In reply to] Can't Post

>>(a (b (c) d) e) <<

/\((\w\s*\((\w\s*\((\w)\)\s*\w)\)\s*\w)\)/ and ($v1,$v2,$v3) = ($3,$2,$1);

How about that for complicated :)


(This post was edited by RedRum on Mar 24, 2002, 6:31 AM)


sskb
newbie

Mar 24, 2002, 11:03 PM

Post #4 of 5 (444 views)
Re: [RedRum] recursive matching [In reply to] Can't Post

hi mhx and redrum,
thanks for your kind replies.
I hope some examples would explain better.
I have some cases in which i am not sure a line would which of the following

case 1:

(a and (h and e) or (p not j)))

outputs required :-

$1 = (h and e)
$2 = (p not j)
$3 = $1 or $2
$4 = a and $3
---------------------------------------------
case2:

((a and ((h and e) or (p not j))) and k)

outputs required :-

$1 = (h and e)
$2 = (p not j)
$3 = $1 or $2
$4 = a and $3
$5 = $4 and k

In general, the variables should be assigned starting from the innermost parathesis.
is that clear?

thanks,
sskb


mhx
Enthusiast / Moderator

Mar 25, 2002, 1:37 AM

Post #5 of 5 (440 views)
Re: [sskb] recursive matching [In reply to] Can't Post

I don't think you want to implement a binary not operator ...

In Reply To

Code
$2 = (p not j)


... or what would you expect to be the result of 'p not j'??

However, I think [url=http://search.cpan.org/search?dist=Parse-RecDescent]Parse::RecDescent is what you need. It allows you to very easily build parse trees from a grammar specification, like so:

[perl]
#!/usr/bin/perl -w
use strict;
use Parse::RecDescent;
use Data::Dumper;

$::RD_AUTOACTION = q{ $return = $item[1] };

my $parse = new Parse::RecDescent <<'EndGrammar';

expression : or_expr

or_expr : and_expr 'or' or_expr { [@item[1..3]] }
| and_expr

and_expr : unary_expr 'and' and_expr { [@item[1..3]] }
| unary_expr

unary_expr : '(' expression ')' { $item[2] }
| 'not' VARIABLE { [@item[1..2]] }
| VARIABLE

VARIABLE : /\w+/

EndGrammar

my $expr = '(a and (h and e) or (not j)))';
my $tree = $parse->expression( $expr );

print Dumper( $tree );
[/perl]

I've just dumped the parse tree and leave the evaluation up to you. As this is just an example, I'd recommend you to browse through the [url=http://search.cpan.org/doc/DCONWAY/Parse-RecDescent-1.80/lib/Parse/RecDescent.pod]Parse::RecDescent manpage for further information, especially on how to use objects to evaluate the parse tree. There's also a [url=http://search.cpan.org/doc/DCONWAY/Parse-RecDescent-1.80/tutorial/tutorial.html]tutorial available.

Hope this helps.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives