1. Homepage
  2. Programming
  3. COMP2041 Software Construction - Assignment 2: Sheepy

COMP2041 Software Construction - Assignment 2: Sheepy

Engage in a Conversation
AustraliaUNSWCOMP2041COMP9044Software ConstructionShellDashSheepy

Assignment 2: Sheepy



This assignment aims to give you CourseNana.COM

  • practice in Python programming generally
  • experience in translating between complex formats with Python
  • clarify your understanding of Shell syntax & semantics
  • introduce you to Python syntax & semantics


Your task in this assignment is to write a POSIX Shell Transpiler.
Generally, compilers take a high-level language as input and output assembler, which can then can be directly executed.
A Transpiler (or Source-to-Source Compiler) takes a high-level language as input and outputs a different high-level language.
Your transpiler will take Shell scripts as input and output Python.
Such a translation is useful because programmers sometimes convert Shell scripts to Python.
Most commonly this is done because extra functionality is needed, e.g. a GUI.
And this functionality is much easier to implement in Python.
Your task in this assignment is to automate this conversion.
You must write a Python program that takes as input a Shell script and outputs an equivalent Python program. CourseNana.COM

The translation of some POSIX Shell code to Python is straightforward.
The translation of other Shell code is difficult or infeasible.
So your program will not be able to translate all Shell code to Python.
But a tool that performs only a partial translation of shell to Python could still be very useful. CourseNana.COM

You should assume the Python code output by your program will be subsequently read and modified by humans.
In other words, you have to output readable Python code.
For example, you should aim to preserve variable names and comments. CourseNana.COM

Your compiler must be written in Python.
You must call your Python program sheepy.py.
It will be given a single argument, the path to a Shell script as its first command line argument.
It should output, to standard output, the equivalent Python code.
For example: CourseNana.COM

cat gcc.sh

for c_file in *.c
    gcc -c $c_file
./sheepy.py gcc.sh
#!/usr/bin/python3 -u

import glob, subprocess

for c_file in sorted(glob.glob("*.c")):
    subprocess.run(["gcc", "-c", c_file])

If you look carefully at the example above you will notice the Python code does not have exactly the same semantics as the shell code.
If there are no .c files in the current directory the for loop in the shell program executes once and tries to compile a non-existent file named *.c whereas the Python for loop does not execute.
And if the file name contains spaces the shell code will pass it as multiple arguments to gcc but the Python code will pass it as a single argument - in other words the shell breaks but the Python works. CourseNana.COM

This is a general issue with translating Shell to Python.
In many cases, the natural translation of the shell code will have slightly different semantics.
For some purposes, it might be desirable to produce more complex Python code that matches the semantics exactly.
For example: CourseNana.COM

#!/usr/bin/python3 -u

import glob, subprocess

if glob.glob("*.c"):
    for c_file in sorted(glob.glob("*.c")):
        subprocess.run(["gcc", "-c"] + c_file.split())
    subprocess.run(["gcc", "-c", "*.c"])

This is not desirable for our purposes.
Our goal is to produce the clearest most human-readable code so the first (simpler) translation is more desirable. CourseNana.COM


The shell features you need to implement is described below as a series of subsets. CourseNana.COM

It suggested you tackle the subset in the order listed but this is not required. CourseNana.COM

Subset 0


The echo builtin is used to output a string to stdout.
For example: CourseNana.COM


echo hello world
echo 42 is the meaning of life, the universe, and everything
echo To be or not to be: that is the question


The = operator is used to assign a value to a variable.
For example: CourseNana.COM




The $ operator is used to access the value of a variable.
For example: CourseNana.COM


echo The meaning of life, the universe, and everything is $theAnswer

echo I hope you are enjoying $name this semester

echo $H, $W

echo $palindrome


The # operator is used to start a comment.
For example: CourseNana.COM


# This is a comment

echo hello world # This is also a comment

Subset 1


The *?[, and ] characters are used in globbing.
For example: CourseNana.COM


echo *


echo all of the single letter Python files are: ?.py


The fordo, and done keywords are used to start and end for loops.
For example: CourseNana.COM


for i in 1 2 3
    echo $i

for word in this is a string
    echo $word

for file in *.c
    echo $file


The exit builtin is used to exit the shell.
For example: CourseNana.COM


echo hello world


echo this will not be printed

exit 0

echo this will double not be printed

exit 3


The cd builtin is used to change the current working directory.
For example: CourseNana.COM


echo *

cd /tmp

echo *

cd ..

echo *


The read builtin is used to read a line from stdin.
For example: CourseNana.COM


echo What is your name:
read name

echo What is your quest:
read quest

echo What is your favourite colour:
read colour

echo What is the airspeed velocity of an unladen swallow:
read velocity

echo Hello $name, my favourite colour is $colour too.

External Commands

Any line that is not a known builtin, or keyword, or other shell syntax should be treated as an external command.
For example: CourseNana.COM


touch test_file.txt
ls -l test_file.txt

for course in COMP1511 COMP1521 COMP2511 COMP2521 # keyword
do                                                # keyword
    echo $course                                  # builtin
    mkdir $course                                 # external command
    chmod 700 $course                             # external command
done                                              # keyword

Subset 2

Command Line Arguments

The $0$1$2, etc. variables are used to access the command line arguments.
For example: CourseNana.COM


echo This program is: $0


echo going to print the first $number_of_lines lines of $file_name


The ${} operator is used to access the value of a variable.
For example: CourseNana.COM



echo FOO${string}BAZ


The test builtin is used to test a condition.


The ifthenelifelse, and fi keywords are used to start and end if statements.
For example: CourseNana.COM


if test -w /dev/null
    echo /dev/null is writeable


The whiledo, and done keywords are used to start and end while loops.
For example: CourseNana.COM


while test $row != 11111111111
    echo $row

Single Quotes

The ' character is used to start and end a single-quoted string.
For example: CourseNana.COM


echo 'hello    world'

echo 'This is not a $variable'

echo 'This is not a glob *.sh'

Subset 3

Double Quotes

The " character is used to start and end a double-quoted string.
For example: CourseNana.COM


echo "hello    world"

echo "This is sill a $variable"

echo "This is not a glob *.sh"


A command substitution can be started and ended with a ` character (backtick). CourseNana.COM

For example: CourseNana.COM


date=`date +%Y-%m-%d`

echo Hello `whoami`, today is $date

echo "command substitution still works in double quotes: `hostname`"

echo 'command substitution does not work in single quotes: `not a command`'

echo -n

The -n flag for echo tells it not to print a newline at the end of the output
For example: CourseNana.COM


echo -n "How many? "
read n

number of command line arguments

The $# variable is used to access the number of command line arguments.
For example: CourseNana.COM


echo I have $# arguments

command line argument lists

The $@ variable is used to access all the command line arguments.
For example: CourseNana.COM


echo "My arguments are $@"

Subset 4


The casein)esac, and ;; keywords are used to start and end case statements.
For example: CourseNana.COM


case $# in
        echo no arguments
        echo one argument
        echo more than one argument


The $() operator is used for command substitution.
The $() operator is the same as the ` operator.
Except that The $() operator may be nested.
For example: CourseNana.COM


date=$(date +%Y-%m-%d)

echo Hello $(whoami), today is $date

echo "command substitution still works in double quotes: $(hostname)"

echo 'command substitution does not work in single quotes: $(not a command)'

echo "The groups I am part of are $(groups $(whoami))"


The $(()) operator is used to evaluate an arithmetic expression For example: CourseNana.COM


echo $((x  + y))

<, >, and >>

The <>, and >> operators are used to redirect stdin and stdout respectively.
For example: CourseNana.COM


echo hello >file
echo world >> file
cat <file

&& and ||

The && and || operators are used to perform boolean logic.
For example: CourseNana.COM


test -w /dev/null && echo /dev/null is writeable
test -x /dev/null || echo /dev/null is not executable

if/while conditions

In this subset, if and while conditions can now be any external command, or multiple commands joined by && or ||.
For example: CourseNana.COM


if test -w /dev/null && test -x /dev/null
    echo /dev/null is writeable and executable

if grep -Eq $(whoami) enrolments.tsv
    echo I am enrolled in COMP2041/9044


Some examples of shell code and possible translations are available as a table or a zip file CourseNana.COM

These examples should provide most the information you need to tackle subsets 0 & 1. CourseNana.COM

Translating subsets 2-4 will require you to discover information from online or other resources.
This is a deliberately part of the assignment. CourseNana.COM

The Python you output can and probably will be different to the examples you've been given. CourseNana.COM

So there is no way to directly test if your Python output is correct. CourseNana.COM

But the Python you output when run has to behave the same as the input shell script it was generated from.
So a good check of any translation is to execute the Shell and the Python and then use diff to check that their output is identical. CourseNana.COM


Like all good programmers, you should make as few assumptions about your input as possible.

You can assume the code you are given is Shell which works with the version of on CSE systems (essentially POSIX compatible). CourseNana.COM

Other shells such as Bash contain many other features. If these features and not present in /bin/dash on CSE machines you do not have to handle these CourseNana.COM

You do not need to implement keywords & builtins not listed above, for example the pipe operator ('|') does not appear above so you do not need to translate pipelines. CourseNana.COM

You should implement the keywords & builtins listed above directly in Python, you cannot execute them indirectly via subprocess or other Python modules. For example, this is not an acceptable translation. CourseNana.COM

subprocess.call("for c_file in *.c; do gcc -c $c_file; done", shell=True)

The only shell builtins which you must translate directly into Python are: CourseNana.COM

exit read cd test echo
The builtins (exit read cd) must be translated into Python to work. For example this Python code does not work:
The last 2 (test echo) can be, and often are also implemented by stand-alone programs. So, for example, this Python code will work:
Doing this will receive no marks, instead of using subprocess.call you should translate uses of test, and echo directly to Python, e.g.:
print "hello world"

The only Shell builtin option you need to handle is echo's -n option.
You do not need to handle other echo options such as -e.
You do not need to handle options for other builtins.
For example, you do not need to handle the various (rarely-used) read options. CourseNana.COM

Dash has many special variables.
You need only handle a few of these, which indicate the shell script's arguments.
These special variables need be translated: CourseNana.COM

$# $@ $0 $1 $2 $3 ...

You assume the shell scripts you are given execute correctly on a CSE lab machine.
Your program does not have to detect errors in the shell script it is given. CourseNana.COM

You should assume as little as possible about the formatting of the shell script you are given but most of the evaluation of your program will be on sensibly formatted shell scripts. Copying the indenting will mostly but not always give you legal Python. CourseNana.COM

You should transfer any comments in the shell code. With some approaches it can be difficult to transfer comments in exactly the same position, in this case it is OK if comments are shifted to some degree. CourseNana.COM

You don't have to preserve white-space and you will not be penalized for example for removing or adding trailing white-space. CourseNana.COM

If there are shell keywords, e.g. case, that you cannot translate the preferred behaviour is to include the untranslated shell construct as a comment. Other sensible behaviour is acceptable. CourseNana.COM


Get the easiest transformations working first, make simplifying assumptions as needed, and get some simple small shell scripts successfully transformed. Then look at handling more constructs and removing the assumptions.

You won't be able to output Python as you generate it e.g. you won't know which import statements are needed to be printed first. Append the Python code to a list as you generate it. CourseNana.COM

If you want a good mark, you'll need to be careful in your handling of syntax which has no special meaning in shell but does in Python. CourseNana.COM

The bulk of knowledge about shell syntax & semantics you need to know has been covered in lectures. But if you want to get a high mark, you may need to discover more. Similarly much of the knowledge of Python you need has been covered but if you want to get a high mark you may need to discover more. CourseNana.COM

Python in sheepy.py

Your sheepy.py should work with the default Python on a CSE lab machine.

You are only permitted to import these modules in sheepy.py CourseNana.COM

argparse array atexit bisect collections copy dataclasses
datetime decimal enum fileinput fnmatch fractions functools
itertools keyword locale math operator os pathlib pprint
random re statistics string sys tempfile textwrap time
traceback turtle typing unicodedata uuid

You are not permitted to use other modules. CourseNana.COM

For example, you are not permitted to import shlex in sheepy.py You are also not permitted to import subprocess in sheepy.py but the Python you generate can import subprocess. CourseNana.COM

You can request modules be added to the permitted list in the course forum. CourseNana.COM

Most of the modules listed above are little or no use for the assignment. CourseNana.COM

Three modules, osre and sys are important for the assignment. CourseNana.COM

Python Translated from Shell

The Python sheepy.py generates from the input Shell script should work with Python on a CSE lab machine.


Any import statements should be at the top of the generated Python to ensure it is readable. CourseNana.COM

The generated Python should only import modules it uses. CourseNana.COM

The generated code is permitted only to import these modules. CourseNana.COM

glob os shutil subprocess sys
The generated Python should not generate warnings from the Python static checker pyflakes3. The automarking will run pyflakes3 and if it does generate warnings there may be a small penalty. For example, pyflakes3 will generate a warning if you have an For example  unnecessary import statement and there might be a small penalty for this.

You are encouraged to generate Python which does not generate warnings from the Python style checker ' pycodestyle' but there will be no penalty if you fail to do so and some warnings, e.g. "line too long", are hard to avoid. CourseNana.COM

You'll have subtle problems with output ordering unless you use Python's -u flag in the '#!' line for your shell script. This is easy to do - just copy the first line of the example Python. CourseNana.COM

The '#!' line in the example generated code assumes /usr/bin/python3 is the pathname of the Python interpreter, which it is on CSE systems and many other places. CourseNana.COM

If /usr/bin/python3 isn't the the appropriate pathname on your computer try this '#!' line which searches all the directories in $PATH for the Python interpreter:. CourseNana.COM

#! /usr/bin/env -S python3 -u

Demo Shell Scripts

You should submit five shell scripts named demo00.sh .. demo04.sh which your program translates correctly (or at least well). These should be realistic shell scripts containing features whose successful translation indicates the performance of your assignment. Your demo scripts don't have to be original, e.g. they might be lecture examples. If they are not original they should be correctly attributed.

If you have implemented most of the subsets,r these should be longer shell scripts (20+ lines). They should if possible test many aspects of shell to Python translation. CourseNana.COM

Test Shell Scripts

You should submit five shell scripts named test00.sh .. test04.sh which each test a single aspect of translation. They should be short scripts containing shell code which is likely to be mis-translated. The test??.sh scripts do not have to be examples that your program translates successfully.

You may share your test examples with your friends but the ones you submit must be your own creation. CourseNana.COM

The test scripts should show how you've thought about testing carefully. They should be as short as possible (even just a single line). CourseNana.COM

Get in Touch with Our Experts

Wechat WeChat
Whatsapp Whatsapp
Australia代写,UNSW代写,COMP2041代写,COMP9044代写,Software Construction代写,Shell代写,Dash代写,Sheepy代写,Australia代编,UNSW代编,COMP2041代编,COMP9044代编,Software Construction代编,Shell代编,Dash代编,Sheepy代编,Australia代考,UNSW代考,COMP2041代考,COMP9044代考,Software Construction代考,Shell代考,Dash代考,Sheepy代考,Australiahelp,UNSWhelp,COMP2041help,COMP9044help,Software Constructionhelp,Shellhelp,Dashhelp,Sheepyhelp,Australia作业代写,UNSW作业代写,COMP2041作业代写,COMP9044作业代写,Software Construction作业代写,Shell作业代写,Dash作业代写,Sheepy作业代写,Australia编程代写,UNSW编程代写,COMP2041编程代写,COMP9044编程代写,Software Construction编程代写,Shell编程代写,Dash编程代写,Sheepy编程代写,Australiaprogramming help,UNSWprogramming help,COMP2041programming help,COMP9044programming help,Software Constructionprogramming help,Shellprogramming help,Dashprogramming help,Sheepyprogramming help,Australiaassignment help,UNSWassignment help,COMP2041assignment help,COMP9044assignment help,Software Constructionassignment help,Shellassignment help,Dashassignment help,Sheepyassignment help,Australiasolution,UNSWsolution,COMP2041solution,COMP9044solution,Software Constructionsolution,Shellsolution,Dashsolution,Sheepysolution,