Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

parsing a java src file

by rmexico (Novice)
on Jan 28, 2006 at 16:35 UTC ( #526183=perlquestion: print w/ replies, xml ) Need Help??
rmexico has asked for the wisdom of the Perl Monks concerning the following question:

how do i parse a java src file, specifically for this:
public class test { public static final test alpha = new test("foo", "bar"); public static final test beta = new test("baz", "boo"); ...

so that i'm finding all local variables that match the classname, and get the parameters that are passed to the constructor.
this is easy enough if it's all on the same line and the order is consistent, but there might be carriage returns and the static/final words might be interchanged.
In the above example, i'm strictly looking for:
alpha = {foo, bar}
beta = {baz, boo}

thoughts?
thanks

Comment on parsing a java src file
Download Code
Re: parsing a java src file
by explorer (Chaplain) on Jan 28, 2006 at 18:21 UTC
    If this is the input kk.java:
    public class test { public static final test alpha = new test("foo", "bar"); public static final test beta = new test("baz", "boo"); } public class test2 { public static final test2 alpha2 = new test2("foo", "bar"); public static final test2 beta2 = new test2("baz", "boo"); public static final test alpha = new test("foo", "bar"); public static final test beta = new test("baz", "boo"); }
    And this the code one line:
    perl -MData::Dumper -le 'undef $/;$javafile = <>; while ( $javafile =~ + / class (\w+) (.*?)^\}/smg ) { ($class,$body) = ($1,$2); while ( $bo +dy =~ /(\w+)\s*=\s*new\s+$class\s*\((.*)\)/g) { ($variable,$parameter +s)=($1,$2); foreach $v (split",",$parameters){ $v =~ s/[ "]//g; push +@{$var{$class}{$variable}},$v } } }; print Dumper %var ' kk.java
    this is the result:
    $VAR1 = 'test'; $VAR2 = { 'alpha' => [ 'foo', 'bar' ], 'beta' => [ 'baz', 'boo' ] }; $VAR3 = 'test2'; $VAR4 = { 'alpha2' => [ 'foo', 'bar' ], 'beta2' => [ 'baz', 'boo' ] };
    Updated: Not oneline version:
    #!/usr/bin/perl use Data::Dumper; use diagnostics; use warnings; use strict; undef $/; my %vars; # My vars space my $javafile = <>; # Read the java src # Read all class' bodies while ( $javafile =~ / class (\w+) (.*?)^\}/smg ) { my ($class,$body) = ($1,$2); # Read all vars' declarations while ( $body =~ /(\w+)\s*=\s*new\s+$class\s*\((.*)\)/g) { my ($variable,$parameters)=($1,$2); # Split parameters foreach my $v ( split ",",$parameters ) { # Filter bad characters $v =~ s/[ \"]//g; # Parameters falling into the black hole push @{ $vars{$class}{$variable} }, $v; } } } # Out of the space print Dumper %vars;
    Updated. Edited for last test.
      hmm, if i change your kk.java though to something like i have below, i get no results.
      public class test { private String one; private String two; public test(String one, String two) { this.one = one; this.two = two; } public static final test alpha = new test("foo", "bar"); public final static test beta = new test("baz", "boo"); }
      thoughts?
        Solved... but is tricky,,, Only work if the body of the class end with a '}' at the first column of src.
Re: parsing a java src file
by philcrow (Priest) on Jan 30, 2006 at 14:40 UTC
    Parsing java code is hard. It is easier to let javac do that. Then use javap to dump the compiled code headers (or all the code) in cannonical form. The standardized output form makes the output much easier to parse.

    I used javap with the -c flag to put listener apis from java's foundation classes into a useful form in Java::Swing. You can see what I did by getting the distribution and looking in the Swing/Generate subdirectory for the all_listeners script. It has a Parse::RecDescent grammar which parsed the javap output for me.

    Phil

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://526183]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2014-09-23 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls