K&R C code edits

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions K&R C code edits
# 1  
Old 10-05-2011
K&R C code edits

1. The problem statement, all variables and given/known data:

Quote:
The ctrace facility expects its input code to conform to the language definition from the 1970's (Kernighan & Ritchie). This script's task is to help in adapting a C89 or C99 program to that syntax.

In particular:

Remove function prototypes, and remove type info from function headings.

Instead, the function arguments will be declared after the closing ) of the function heading, and before the opening { of the function body.

HINT: All the formal parameters in ANSI C have at least two words (a type name and parameter name) and all the formal parameters in K&R C have exactly one word (the parameter name). You can use that fact to identify a pattern, and then use sed's "t" command to repeat for additional parameters, until done.

NOTE: If a parameter is a pointer, the asterisk is part of the type, not the parameter name.

Remove declarations from within the headings of for loops, e.g.
for ( int i = 0; i < 10; i++ )
would no longer declare i to be 'int' here, but somewhere before the loop.

Try to move the declaration for for-loop counters to a new line immediately after the last '{'.
HINT: You can temporarily reverse a file and then use 'sed's pattern and hold spaces to hold the declaration until it is ready for insertion.

The results of this program script should appear on standard output (from which they can be redirected to a file). The resulting code must compile correctly in any C compiler.
2. Relevant commands, code, scripts, algorithms:

We have to do this using 'unix tools' and not use the script as if it were C. Meaning, he wants more uses of grep, sed, awk, cut, etc... than he does while, for, do's and done's.

3. The attempts at a solution (include all code and scripts):

We've done two other programs using these tools and I know how to use grep pretty well, but sed's so complicated that I'm not quite sure what method to use (and or why to use it) as this is our first major homework using it.

He said he was able to complete the homework in 2 lines of code using multiple pipes, but he didn't show us any of the code.

I'm assuming that I have to take something like this:
Code:
for (int i=0; i<10; i++) 
{...  }

and change it into:
Code:
for (i=0; i<10; i++)
{... }
int i;

and delete headers such as:
Code:
int func1(int a, int b...);

and change the actual functions to:
Code:
int func1(a, b...)
{ ...  }
int a;
int b;

The deletions of the headers is pretty straight forward using sed:
Code:
sourcefile=$1
cat $sourcefile | sed "/^[a-zA-Z].* [a-zA-Z].*(.*);.*/ d" > newcode.c

First of all, am I interpreting his instructions correctly?
But where I don't know how to progress is the bigger picture. I don't have enough practice doing scripts such as these to know how to attack it. Can you give some help in crafting an attack at this? I'm not looking for the code but more the pseudocode.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Penn State University, PA, USA, Roger Christman, CMPSC311
# 2  
Old 10-05-2011
Quote:
I'm assuming that I have to take something like this:

Code:
for (int i=0; i<10; i++) 
{...  }

and change it into:

Code:
for (i=0; i<10; i++)
{... }
int i;

Not quite. The wording in your instructions, IMHO, uses a poor choice of words. I would have said to put the initialisation (int i) immediately following the previous opening curly brace. The word last obviously leads to incorrect interpretations as you did.

Thus, the resulting code would be:
Code:
{     /* beginning of current block */
int i;
for (i=0; i<10; i++)
{... }

Helping with home work is tricky, guiding without giving you the answer; I'll add more if I can figure how not to give you too much.

Last edited by agama; 10-05-2011 at 11:17 PM.. Reason: typo
# 3  
Old 10-05-2011
That's what I thought too at one point. I couldn't be using i as a variable unless it was defined earlier. It didn't make sense to have it after the block of the function. The instructions are a direct cut from his assignment page.

But as I said, I'm not sure how he'll be grading it. He obviously wants us to use unix/linux tools as much as possible. However, the class is shell scripting so having working code is just as important, even if it's not as efficient as his code could be.

I don't know how to use the tools we have (sed, grep, cut, tr, etc...) to do this. That's more what I'm asking. How can normal command-line tools be used to do this? For instance, which tools can do this the best? substitution through sed? Do I need to use the hold buffer and pattern buffers extensively? I don't know how to attack the problem.
# 4  
Old 10-06-2011
Code:
int main(int argc, char **argv)
{
     printf("Hello World\n");
     return 0;
}

K&R format
Code:
main(argc, argv)
int argc;
char **argv;
{
     printf("Hello World\n");
}

K&R implies an int, but you do not have to return anything.


The only use I know for K&R now is:
The default HPUX compiler is K&R, you have to buy a modern one or install gcc from a depot file.

So my question why is this guy assigning something like that?
# 5  
Old 10-06-2011
Probably so they can use it with ctrace...
# 6  
Old 10-06-2011
I've been arguing with this teacher the entire semester about what he's assigning and how he's assigning things. He's terrible, overall, and the class attendance (~18/60 daily) and average on the first homework (40/100) and the first exam (70/160) shows it. He's just not in touch with someone trying to learn it. For instance, this is our very first assignment in shell scripting. We learned awk but we're not allowed to use it (?) for the tabulated data program we had to do.

And as a result, I don't understand how to actually attack this problem. Do I treat the for loops separate from the other declarations of functions? And how do I use sed to manipulate a multiple line function? What if the function has the curly braces on the same line as the declaration? I have to take all this into account most likely. Is this how it would be done?

//use grep (or sed) to find a line with a function declaration
//search for the open parenthesis to the closed parenthesis and remember what's between them?
//call back the stored words as you write out a new line for each one?

Can all this be done in one pass of sed? This is where I'm lost. I don't know what's possible or not or how to go about approaching it.
# 7  
Old 10-06-2011
Most commandline utilities are line-based, C is definitely not, so the first thing I'd do is try to transform C code into something more tolerable by stripping out whitespace and putting it exactly where I want. Many versions of sed don't work for lines longer than 2000 bytes though -- what system do you have?
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