parse perl expression

D

david

Hi all,

I have an perl expression which i have to examine (not to run).

for example:

($obj->fun1(x,y,z) || ($obj->fun2(x,y,z) && $obj->fun3('a') || ($obj-
fun4 && $obj->fun5))
(Please don't count the number of parentheses :).)

My question is how could i parse this expression so that i'll get a
list like this

$obj->fun1(x,y,z),
$obj->fun2(x,y,z)
$obj->fun3('a')
$obj->fun4
$obj->fun5
The hierarchy is less important. I need just the operators.
I fear to use regexps (i know them but i am not a wizard in it)
because in the arguments there could be also parentheses.

What do you think ?

Thanks a lot,
David
 
S

sln

Hi all,

I have an perl expression which i have to examine (not to run).

for example:

($obj->fun1(x,y,z) || ($obj->fun2(x,y,z) && $obj->fun3('a') || ($obj-
(Please don't count the number of parentheses :).)

My question is how could i parse this expression so that i'll get a
list like this

$obj->fun1(x,y,z),
$obj->fun2(x,y,z)
$obj->fun3('a')
$obj->fun4
$obj->fun5
The hierarchy is less important. I need just the operators.
I fear to use regexps (i know them but i am not a wizard in it)
because in the arguments there could be also parentheses.

What do you think ?

Thanks a lot,
David

Well, if you set aside all the menutia of parsing language
source code including nesting, comments, and errors,
and have a good idea of what your looking for in a static
way, you might have limited success but not in any way a
guarantee.

What do you mean by "I need just the operators"?

Parsing the source code of structured language is very, very complicated.

-sln
 
A

A. Sinan Unur

I have an perl expression which i have to examine (not to run).

for example:

($obj->fun1(x,y,z) || ($obj->fun2(x,y,z) && $obj->fun3('a') || ($obj-
(Please don't count the number of parentheses :).)

My question is how could i parse this expression

I did not try it out but would PPI work for you?

http://search.cpan.org/~adamk/PPI-1.203/lib/PPI.pm

Sinan


--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
D

david

S

sln

Fortunately there are modules to do this sort of thing for you.

use Regexp::Common;

my $str = <<'PERL';
($obj->fun1(x,y,z) || ($obj->fun2(x,y,z) && $obj->fun3('a') ||
($obj->fun4 && $obj->fun5))
$obj->fun6($obj->fun7(foo(bar(baz), (quux), ())))
PERL

print for $str =~ m{ (
\$\w+ \s*->\s* \w+\s*
$RE{balanced}?
) }gx;

__END__

$obj->fun1(x,y,z)
$obj->fun2(x,y,z)
$obj->fun3('a')
$obj->fun4
$obj->fun5
$obj->fun6($obj->fun7(foo(bar(baz), (quux), ())))

Notice how the ->fun7 call is not extracted separately, since it's
inside the ->fun6 call. If this is a problem you will need to tweak the
pattern, or apply it again to the extracted parts.

Ben

Unfortunately you pigeonholed the text. You CANNOT
parse perl documents like this. See PPI.

Nice try though.

-sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top