K&R C code edits

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions K&R C code edits
# 8  
Old 10-06-2011
I got this back from the teacher today in email:
Quote:
The C-translation problems are much simpler if one lets the sed stream editor edit all applicable lines 'at once', instead of reading a line, deciding whether to edit it, asking sed to edit it, and then reading the next line.
What does he mean by asking sed to edit all applicable lines at once?
# 9  
Old 10-06-2011
It's exactly what I said suggested: Flattening all whitespace so it's only one big 'line' and statements only appear in one fashion. It's the difference between

Code:
int main(        int argc,
                            char *argv[]        )
{
        return(0);
}

and
Code:
int main(int argc, char *argv[]) { return(0); }

The C compiler doesn't care as long as lines beginning with # aren't mangled.

If he thinks that's possible, you're on a system which doesn't barf on lines bigger than 2000 characters.

---------- Post updated at 04:21 PM ---------- Previous update was at 03:47 PM ----------

A start might be:

Code:
$ cat flatc.awk
BEGIN {
        ORS=" " # Records separated by space instead of newline
}

{
        if($0 ~ /^#/)
                printf("\n%s\n", $LINE); # preprocessors statements
        else
        {
                $1=$1;  # awk prints the unmodified line unless you
                        # modify $1..$N somewhere, so $1=$1 flattens

                print;
        }
}

END {
        printf("\n"); # Even one big line must end in \n
}

$ cat popen.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
        int status;
        FILE *fp=popen("gunzip", "r");
        char buf[512];

        while(fgets(buf, 512, fp))
        {
                printf("%s", buf);
        }

        if(WEXITSTATUS(status=pclose(fp)))
        {
                printf("gunzip failed with status %d, do rollback\n",
                        WEXITSTATUS(status));
        }
}

$ awk -f flatc.awk < popen.c

#include <stdio.h>

#include <sys/types.h>

#include <sys/wait.h>
 int main(int argc, char *argv[]) { int status; FILE *fp=popen("gunzip", "r"); char buf[512];  while(fgets(buf, 512, fp)) { printf("%s", buf); }  if(WEXITSTATUS(status=pclose(fp))) { printf("gunzip failed with status %d, do rollback\n", WEXITSTATUS(status)); } }

$ awk -f flatc.awk < popen.c |
        # /^#/p means, if a line begins with #, print it.
        # /^#/d means, if a line begins with #, delete it and move to the next line.
        # All the rest reach s/int/flub/g.
        # This way, we can avoid modifying preprocessor statements.
        sed '/^#/p;/^#/d;s/int/flub/g'

#include <stdio.h>

#include <sys/types.h>

#include <sys/wait.h>
 flub main(flub argc, char *argv[]) { flub status; FILE *fp=popen("gunzip", "r"); char buf[512];  while(fgets(buf, 512, fp)) { prflubf("%s", buf); }  if(WEXITSTATUS(status=pclose(fp))) { prflubf("gunzip failed with status %d, do rollback\n", WEXITSTATUS(status)); } }

$

Having blocks as all one big line means you can do complicated search-replaces on entire valid blocks of code. otherwise, sed couldn't hunt for patterns bigger than one line.
# 10  
Old 10-06-2011
When you use sed '/^#/p;/^#/d;s/int/flub/g' in this fashion, how does it operate on the line? you have 3 commands (1. print, 2. delete, 3. substitute) acting on one big line of text. Does it do all 3 at one time, or one pass with command 1, another with command 2, then 3? I'm not clear on how this works.

Also, after piping the flat file into sed, why are you printing the preprocessor line then deleting it?

---------- Post updated at 09:02 PM ---------- Previous update was at 07:39 PM ----------

I stripped out all of the comments in my test code, stripped out the function prototypes, and used your awk statement to flatten my code down to one line preceded by preprocessor directives. At this point, I've attempting to identify the functions left using a combination of grep and sed most but am unable to do much with it.

I don't know how to isolate something in this format:
Code:
word value(word value, word value, word value....)

to be able to store the words and values from the pattern match and rewrite it.
# 11  
Old 10-07-2011
Quote:
Originally Posted by theexitwound
When you use sed '/^#/p;/^#/d;s/int/flub/g' in this fashion, how does it operate on the line? you have 3 commands (1. print, 2. delete, 3. substitute) acting on one big line of text. Does it do all 3 at one time, or one pass with command 1, another with command 2, then 3? I'm not clear on how this works.
By default, it does one pass with command 1, one pass with command 2, then one pass with command 3. But some commands alter what happens after them, like /..../d.
Quote:
Also, after piping the flat file into sed, why are you printing the preprocessor line then deleting it?
So that preprocessor lines are (1) printed unaltered, then (2) deleted, preventing them getting being mangled by anything happening in step (3).
Quote:
At this point, I've attempting to identify the functions left using a combination of grep and sed most but am unable to do much with it.
That sounds like a bad combination... again, those tools are line-based and this junk is NOT. If you slice out bits and pieces instead of operating on the entire line, how're you going to put it back together?

Quote:
I don't know how to isolate something in this format:
Code:
word value(word value, word value, word value....)

regular expressions and backreferences. You use brackets to match the parts you need.

Code:
# matches and replaces the entire string with nothing.
$ echo "int a(int b, int c)" | sed 's/\([a-z]* [a-z]*\)(\([a-z]* [a-z]*\)\(, [a-z]* [a-z]*\))//g'

# prints first match.
$ echo "int a(int b, int c)" | sed 's/\([a-z]* [a-z]*\)(\([a-z]* [a-z]*\)\(, [a-z]* [a-z]*\)*)/\1/g'
int a
# prints second match
$ echo "int a(int b, int c, int d)" | sed 's/\([a-z]* [a-z]*\)(\([a-z]* [a-z]*\)\(, [a-z]* [a-z]*\)*)/\2/g'
int b
# prints third match
$ echo "int a(int b, int c)" | sed 's/\([a-z]* [a-z]*\)(\([a-z]* [a-z]*\)\(, [a-z]* [a-z]*\)*)/\3/g'
, int d
$

Note how the third match only matches the last thing in (...)* when there's more than one. I think you'd have to nest it like ( (...)* ) to get the entire block.

You can use that in awk too,
Code:
match("string", /regex/, array)

and the resulting matches fill array from 1 to n. I'm not sure whether you need to escape () like \( \) in awk or not.
# 12  
Old 10-07-2011
Quote:
Originally Posted by Corona688
By default, it does one pass with command 1, one pass with command 2, then one pass with command 3.

...

# prints third match
$ echo "int a(int b, int c)" | sed 's/\([a-z]* [a-z]*\)(\([a-z]* [a-z]*\)\(, [a-z]* [a-z]*\)*)/\3/g'
, int d
Thanks for the explanation of the first bit there. I was confused.

As for the back-referencing in sed, what if I don't know how many variables are being passed in the function? I have to generalize the regular expression to incorporate that, don't I? As you said, if i I have to pull out part of the script, then it's no longer a good idea to use a stream editor. Can I somehow delimit using the comma inside the declaration?
# 13  
Old 10-07-2011
Quote:
Originally Posted by theexitwound
Thanks for the explanation of the first bit there. I was confused.

As for the back-referencing in sed, what if I don't know how many variables are being passed in the function?
I already demonstrated that. Look at my 'prints third match' example. You can use * to search for repeats of entire \( \) blocks to select more than one thing.

Forgetting all the silly \s for the moment, (,[a-z])* matches ,a,b,c,d,e,f,g and gives you ",g" as the backreference.

((,[a-z])*) matches ,a,b,c,d,e,f,g and gives you ,a,b,c,d,e,f,g as the first backreference and ,g as the second.

You could get a,b,c,d,e,f,g with ((,?[a-z])*) since ? makes the char before it optional.
# 14  
Old 10-07-2011
Maybe I'm just missing something, but if you use what you have above, "int a(int b)" won't be found by using those sed patterns because it's expecting to have at least another variable passed to it....although int a(int b, int c) would be found.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. UNIX for Dummies Questions & Answers

multiple text edits inone pass

File_1 looks like: bunch of text Untitled Placemark bunch of text bunch of text Untitled Placemark bunch of text bunch of text Untitled Placemark bunch of text File_2 looks like: Title_001 Title_002 Title_003 First: I need to replace the 1st occurence of "Untitled Placemark"... (2 Replies)
Discussion started by: kenneth.mcbride
2 Replies

3. Shell Programming and Scripting

Problem with call of Java Programm & return code handling & output to several streams.

Hello Everybody, thanks in advance for spending some time in my problem. My problem is this: I want to call a java-Programm out of my shell skript, check if die return code is right, and split the output to the normal output and into a file. The following code doesn't work right, because in... (2 Replies)
Discussion started by: danifunny
2 Replies

4. UNIX for Dummies Questions & Answers

OpenLDAP DB_CONFIG edits, changes live? or do I need run something

So I am probably missing something , but when I made edits to my DB_CONFIG file to fix form db_lock issues, the changes are not propagating after a service restart. Anyone know if I need to run anything else, or are the changes live? (0 Replies)
Discussion started by: jcejka
0 Replies

5. UNIX for Dummies Questions & Answers

Compile & Run Java Code

The java program is a part of speech tagger -> The Stanford NLP (Natural Language Processing) Group The goal is to use this script as part of a webpage to tag parts of speech based on a user-inputted string. I have no idea what to do with the files - I'm a complete *nix noob. I tried running... (4 Replies)
Discussion started by: tguillea
4 Replies

6. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

7. Shell Programming and Scripting

Multiple edits to a bunch of html files

I'm trying to upgrade a whole bunch of pages on my site to a new design. I thought one way of doing it would be to enclose the content in special comment tags and then use some form of script to wrap the new html around it. Like this: <!-- content start --> <h1>Blah blah blah</h1> yada yada... (9 Replies)
Discussion started by: dheian
9 Replies

8. Shell Programming and Scripting

Need help with scripting mass file edits..

Hello, I am wanting to know a way to shell (ksh)script-edit a file by having a script that searches for a specific string, and then input lines of text in the file after that specific string. Please help, as I will be up all night if I can't figure this out. (16 Replies)
Discussion started by: LinuxRacr
16 Replies
Login or Register to Ask a Question