K&R C code edits

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions K&R C code edits
# 15  
Old 10-07-2011
if you can use it, awk has much clearer regexes than sed. The only ()'s you need to escape are ones you want to be actual, literal, ( ) characters instead of grouping brackets.

Code:
$ cat funcrep.awk

{       # Find and replace all matching function declarations
        while(match($0, /([a-z]* [a-z]*)<((,?[a-z]* [a-z]*)*)>/, MATCH))
        {
                FUNC=MATCH[1]   # int fn
                PARAMS=MATCH[2]         # int a, int b, int c
                BEFORE=substr($0, 0, RSTART-1); # Everything before the match
                AFTER=substr($0, RSTART+RLENGTH); # Everything after

                printf("function %s params %s\n", FUNC, PARAMS);
                # Replace it with what we want.
                $0 = BEFORE ":::" AFTER
        }
# 1 at the end of a code block means 'always print $0 after this codeblock finishes'
} 1

$ echo "........ int fn<int a, int b, int c, int d> ........" | awk -f funcrep.awk
function int fn params int a, int b, int c, int d
........ ::: ........
$

It matches <> for clarity, you'll want to change those to \( \).

And, of course, you'll have to take FUNC and PARAMS, split them apart and rearrange them into what you want, and put that back into $0 instead of "..."

And you still have for loops to worry about.

But I hope that'll get you started.

---------- Post updated at 03:19 PM ---------- Previous update was at 03:17 PM ----------

Quote:
Originally Posted by theexitwound
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....
* matches zero or more. Zero of a pattern is perfectly okay. Even an empty parameter list like () should match.
# 16  
Old 10-08-2011
I completely forgot * matches zero or more.

---------- Post updated 10-08-11 at 01:00 PM ---------- Previous update was 10-07-11 at 05:29 PM ----------

How do i make this a lazy return? All I want is the function in the brackets for square(). (just doing single-line tests to see how it works)

Code:
 cat newcode.c | grep -e "( *[a-zA-Z]\+ \+[a-zA-Z]\+) .*}[^.*]" --color=auto

 main() { int i=1; printf("i starts out life as %d.", i);  i = add(1, 1);  printf(" And becomes %d after function is executed.\n", i);  for (int i=0; i<10; i++) { i++; } }    int add( int a, int b) { in
t c; c = a + b; return c; }  double square(double value) { double c; c = value * value; return c; }  
float three(int a, int b, int c) { return c; } 

# 17  
Old 10-08-2011
Quote:
Originally Posted by theexitwound
How do i make this a lazy return?
A what?
Quote:
All I want is the function in the brackets for square().
Oh, a lazy match?

Depends what sense you mean. If you only want the matching part, -o. If you want the shortest possible match, grep can't do that.

I think -o will only return the first match on the line. grep is line based.

And once you get it out, how are you going to put it back in replacing only the section of the match?

This, yet again, is why I suggested using awk instead of individual line-based utilities and wrote in an example for you to fill in Smilie it gives you the matches and lets you replace in-situ.
# 18  
Old 10-08-2011
I was only using grep to test the output so I could see it in color. I've read that sed doesn't support lazy matching and it has to be emulated with 'not' patterns, using [^ ], but I can't figure it out. I thought perhaps I would do something like (ignoring multiple spaces and backreferences and substitution):

Code:
[a-zA-Z]* [a-zA-Z]* ([a-zA-Z]* [a-zA-Z]*){.*}[^.]

datatype1 word1 (datatype2 word2) {any characters}(anything but a character)

but that "anything but a character" doesn't seem to work. It still gets greedy and pulls all the way to the last bracket at the end of the file. I've tried using ?'s and *'s in different arrangements but I can't get a lazy match.
# 19  
Old 10-08-2011
Quote:
Originally Posted by theexitwound
but that "anything but a character" doesn't seem to work.
That's because you have a .* earlier, which means "match absolutely any number of anything".

. is taken as a literal . inside [ ] brackets, so your ^. doesn't do what you think it does either.

I don't think that will work to do non-greedy match. You don't have non-greedy match available.

Do you actually need to match the entire function contents? All you need to change is the declaration. If you wanted to make sure it had function content and wasn't just a function pointer or something, you could just match the very first { after it.

In other words -- tell me your actual goal here, not just the way you want to do it.

Last edited by Corona688; 10-08-2011 at 07:54 PM..
# 20  
Old 10-08-2011
This assignment has been so confusing, I'm losing the entire purpose to be honest. I believe I have to change headings like:
Code:
int main(int argc, char **argv)
{

into
Code:
main(argc, argv)
int argc;
char **argv;
{

I need to be able to
1.) Identify when it's a function heading. I'm looking for word word(word word,word word...). Is there any easier way to do this? Certainly I can't just look for (word, word...).
2.) pull out int argc, char **argv, and parse them back into new lines prior to the first {.

As I start to think that I have something, I get so confused with how it's checking and replacing that I end up just deleting it all and starting over, only to get confused again. Too much is being checked and replaced in one line to make it easy to follow.
# 21  
Old 10-08-2011
Quote:
Originally Posted by theexitwound
This assignment has been so confusing, I'm losing the entire purpose to be honest. I believe I have to change headings like:
Code:
int main(int argc, char **argv)
{

into
Code:
main(argc, argv)
int argc;
char **argv;
{

I need to be able to
1.) Identify when it's a function heading. I'm looking for word word(word word,word word...). Is there any easier way to do this? Certainly I can't just look for (word, word...).
I showed you how to do that.
Quote:
2.) pull out int argc, char **argv, and parse them back into new lines prior to the first {.
You were taught how to use awk, were you not? I've set up an awk template for you. it gets you the function and the parameters. From there it's basic string operations to rearrange them into what you want.

Getting for-loops will be similar work -- matching the statements you want to change with a regex and replacing them until there's nothing left which matches.

I cannot do the work for you but I've made it as easy as I possibly can.
Quote:
As I start to think that I have something, I get so confused with how it's checking and replacing that I end up just deleting it all and starting over, only to get confused again. Too much is being checked and replaced in one line to make it easy to follow.
I might suggest you start with investigating the things I wrote for you Smilie
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