CS 1713 Section 1, Summer 1997
Assignment 5: Strings: cindent.c

An important concept in computer programming is a program that accepts another program as input. One example of such a program is the cc compiler; it is a program, written in C, that accepts another program, also written in C, and produces machine language output. Other such program-processing-programs may do transformations on their input program; for example, cfront is a popular C++ to C translator; the input is a C++ program and the output is an equivalent C program.

Your assignment is to write a C program that accepts as input another C program, specified on the command line (with argc and argv), and prints a nicely indented version of the input program on the standard output. "Nicely indented" for the purpose of this program means that statements within a compound statement are tabbed over one to the right of the statements above and below, for instance:

printf ("hello");
for (i=0; i<n; i++) {
	printf ("%d\n", i);
	scanf ("%d\n", &m);
	for (j=0; j<m; j++) {
		printf ("%d\n", j);
	}
}
printf ("goodbye");
The statements between { and } are indented, with a tab, one more level than the surrounding statements. This property holds even for nested compound statements, as in the third printf statement. The executable program cindent in ~djimenez/bin does what your program is expected to do, so run it on a few sample C programs with bad indenting (examples of which many of you may find in abundance in your directories) and inspect the output.

Your program is required to:

Some hints for writing this program:

Turn in your program by e-mailing the C source (indented and commented nicely!) to the instructor. Remember to post your progress report discussing this program et cetera to the newsgroup by the due date.

This assignment is due at midnight on Monday, July 21, 1997.