$20
For this assignment, you will use sed and bash to create a program for formatting C code. Your program should take a source code file as input and apply the following:
• No more than one space between tokens.
• No trailing whitespace after a line.
• Binary operators should always surrounded by a single space on either side (including assignment and Boolean operators). Only the following operators must be accounted for: +, -, *, /,
!=, =, ==, <=, =, <, , &&, ||, and ˆ.
• Conditions should not have whitespace immediately inside of either the opening or closing parentheses. Further, a parenthetical condition should not possess any whitespace preceding it.
• The program should not modify spaces which are leading, expanded tabs.
• The program should ensure that each opening curly brace, that does not begin the line it is on (excluding existing whitespace), should have at least one space character preceding it.
• The contents of comments should be left alone. You may behave as though comments (either single- or multi-line) will not appear on lines with source code.
Note: Do to the use of the “<” and “” “operators” in preprocessor directives, ignore all lines consisting of preprocessor directives (e.g., lines beginning with zero or more whitespace, followed by a hash (“#”) character).
Note: Do not be concerned with changes in strings; if any such replacement changes the contents of a string, this need not be corrected for, nor will it be considered an issue.
Hint: All of the above does not need to be done in a single pass.
This assignment requires only sed and bash. Do not use awk, Python, or any other languages/utilities.
Example
In the code below, underscores (_) represent spaces in the source code. Note that there are no changes to comments or pre-processor directives (i.e. #include) lines.
Input (inputProgram.c):
1 /** 2 author:_____some_student 3 **/
4 #include_<stdio.h
5
6 int_main()_{
7 ____int_numberIn;
8
9 ____printf("Enter_a_number:_");
10
11 ____scanf("%d",_&numberIn);__
12
13 ____if_(_numberIn__10_)_{
14 ____//____add__two
15 ________return_numberIn_+__2; 16 ____}_else___if____(numberIn<___5){
17 ____//____subtract_two___
18 ________return__numberIn_-_2;
19
20 ____int____isNumOdd__=_numberIn__%__2;
21
22 ____return__numberIn*2;
23 }
Output (outputProgram.c):
1 /** 2 author:_____some_student 3 **/
4 #include_<stdio.h
5
6 int_main()_{
7 ____int_numberIn;
8
9 ____printf("Enter_a_number:_");
10
11 ____scanf("%d",_&numberIn);
12
13 ____if(numberIn__10)_{
14 ____//____add__two
15 ________return_numberIn_+_2; 16 ____}_else_if(numberIn_<_5)_{
17 ____//____subtract_two___
18 ________return_numberIn_-_2;
19
20 ____int_isNumOdd_=_numberIn_%_2;
21
22 ____return_numberIn_*_2;
23 }
Script Execution
Your program should be invoked through a single bash file (see below) with the path to the input program as the argument. The resulting output file should be printed directly to stdout.
$ assign2.bash /path/to/input.txt
Assignment Data
A sample input file can be found in:
/usr/local/courses/ssilvestro/cs3423/Spring20/assign2.
Script Files
Your program should consist of at least two files:
• assign2.bash - the main file which is initially invoked
• At least one .sed file which is used for a sed invocation run in assign2.bash. Each sed invocation should have its own .sed file which may contain multiple sed commands. For example, your submission may include assign2.bash, command1.sed, command2.sed, ..., commandN.sed where each .sed file correspond to sed invocations within assign2.bash.
Verifying Your Program
Your program must work for any arbitrary input files by applying the rules above. You can test your program with the input provided in inputProgram.c and compare the output with outputProgram.c using