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: Advanced:
Edit C program using perl - help needed

 



lisa sangu
New User

Feb 17, 2009, 9:22 PM

Post #1 of 5 (1548 views)
Edit C program using perl - help needed Can't Post

Hi friends,
i have a example c program like this. i want a "generic solution" using perl



main()
{
int a = 10;
int b = 3;

a <<= b;


}

now i want to replace the a <<= b; by a = (int)((unsigned int)a << b); in my C code

so actual situation is like below.

----------------------------------------------------------------------------------------------------------------------------------------------

variable 1 <<= variable 2

should be converted to



variable 1 = (type of variable 1)((unsigned type of variable 1) variable 1 << variable 2);



-----------------------------------------------------------------------------------

the operations to be done are ;

1) before doing left shift i have to typecast variable to unsigned
2) after this i have to put proper paranthesis.
3) find out the type of variable on the left side of = and typecast the whole operation to the that type

please help me to write a "generic perl code" for this scenario.



thank you

lisa


KevinR
Veteran


Feb 18, 2009, 9:29 AM

Post #2 of 5 (1541 views)
Re: [lisa sangu] Edit C program using perl - help needed [In reply to] Can't Post

please don't cross post the same question. Delete or close this thread as the other one already has a reply and better defines what you are trying to do.
-------------------------------------------------


lisa sangu
New User

Feb 18, 2009, 9:14 PM

Post #3 of 5 (1532 views)
Re: [KevinR] Edit C program using perl - help needed [In reply to] Can't Post

Dear Kevin,

i have deleted the other post and keeping this one here. till now there is NO answer replys :) think nobody has the solution SmileSmileSmile

thanks

lisa


KevinR
Veteran


Feb 18, 2009, 9:28 PM

Post #4 of 5 (1530 views)
Re: [lisa sangu] Edit C program using perl - help needed [In reply to] Can't Post

I seriously doubt you are going to get any solutions to this question unless there is already a C parser that can be used.
-------------------------------------------------


gnix
Novice

Feb 19, 2009, 3:02 AM

Post #5 of 5 (1521 views)
Re: [lisa sangu] Edit C program using perl - help needed [In reply to] Can't Post

Hi,

I changed some lines of my previous solution to your problem.


Code
#!/usr/bin/perl -w  

# Check @ARGV
if(@ARGV != 1) {
print "[!] Usage: perl script.pl <file.c>\n";
exit(-1);
}

# Open files
open(OLDFILE, "<$ARGV[0]") || die "[!] Cannot open $ARGV[0]!\n";
open(NEWFILE, ">new_$ARGV[0]") || die "[!] Cannot open new_$ARGV[0]!\n";

# Read file content
@lines = <OLDFILE>;
print "$ARGV[0]\n\n";

# Parse each line
for($i=0; $i < @lines; $i++) {
if($lines[$i] =~ m/(\w+)\s+<<=\s+(\w+)/) {

($var1, $var2) = ($1, $2);
print "[+] line ". ($i + 1) .": Found var1=$var1 and var2=$var2\n";

$type1 = findType($var1, $i);
if(!$type1) {
print "[+] line ". ($i + 1) .": Type for $var1 not found!\n\n";
exit(-1);
}
else {
print "[+] line ". ($i + 1) .": Type found: $type1\n\n";
}

$lines[$i] =~ s/$var1\s+<<=\s+$var2\s*;/$var1 = ($type1)((unsigned $type1)$var1 << $var2);/g;
}

print NEWFILE $lines[$i];
}

# Close files
close(OLDFILE);
close(NEWFILE);

exit(0);



# This function try to find the declaration reading the lines before.
sub findType
{
my $var = shift;
my $i = shift;

for(; $i >= 0; $i--) {
return $1 if ($lines[$i] =~ m/(char|int|long|float|double)\s+$var[^\(]/);
}

return undef;
}


The code seems to work correctly, but is not a generic solution. At the moment, this code try to find a declaration (e.g. "int a;" or "int b=0;") reading the lines before the "var1 <<= var2" line. To do that, it uses the following regex:


Code
m/(char|int|long|float|double)\s+$var[^\(]/


this regex can find only the types specified and is not capable to understand if the declaration is contained in a struct or in a string.

In any case, I hope you can solve your problem with the help of this code.

gnix

ps: Sorry for my English. Smile

 
 


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

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