This is an individual assignment and must be done by yourself. You may discuss the assignment with your fellow students, but you may not look at their code.
Assignments are submitted to Gradescope. There will be an autograder that will grade your submission. You can resubmit your assignment an unlimited number of times until the deadline.
Remember that late submissions are not allowed without instructors prior permission.
To prepare your assignment for submission, use the included
package.py
script. Simply run it:
./package.py
To obtain a file a1.zip
that you can upload to Gradescope.
The package.py
script has been tested to run on the CSUG
machines. If you're running it on your own machine, make sure you have
Python 3.7 or higher, and the latest version of the cryptography
package.
Your goal is to build a find-and-replace for C programs that can change all instances of a variable name, or all instances of a structure member.
Your program will be invoked as follows:
find-and-replace.py spec infile.c outfile.c
Here, spec
is the find-and-replace specification (see below),
infile.c
is the input file on which to apply the spec
, and
outfile.c
is the file to which the output must be saved.
The find-and-replace specifications consist of three parts:
Each part is separated by a colon. Here are some examples:
x:y
, specifies that all variables with name x
should be replaced by y
.x:y:sort
, the same as 1, except only those variables in function sort
should be changed.point.x:rad
, specifies that the member/field x
of the structure type point
should be changed to rad
.Here is an example invocation:
find-and-replace.py point.x:rad:polar coord.c coord_polar.c
This changes the name of the field x
in the structure point
to rad
in the function polar
. The input file is coord.c
and the output C file must be stored coord_polar.c
.
A simple string-based find-and-replace will not suffice for this assignment. You are expected to parse the C file, and operate on the resulting AST. After making changes to the AST, write the changes to the output file.
You will use Python 3 for this assignment. Use the pycparser
library
for parsing C code and obtaining an AST. See the README.html
file
included in the assignment for instructions on installing and using
pycparser.
You may want to use a symbol table to track the type of variables, especially when replacing structure fields. A symbol table is a map from variable to type (and other information). There is one symbol table per function, and usually a global symbol table.
Note your tool must produce valid C code.
The assignment is available on Blackboard under Homeworks and Assignments, it is attached to the announcement.