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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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 (1919 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

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



Feb 18, 2009, 9:29 AM

Post #2 of 5 (1912 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 (1903 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




Feb 18, 2009, 9:28 PM

Post #4 of 5 (1901 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.


Feb 19, 2009, 3:02 AM

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


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

#!/usr/bin/perl -w  

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

# 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";
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


# 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:


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.


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