CS2112: Exercises + Answers from $1 Scope & Introduction

* The shell metacharacters "*" and "?" don't match filenames beginning with a ".". Why?

Because when you are selecting files from the current directory you don't normally want to see the directories "." (current directory) and ".." (parent directory) as well. UNIX exploits this to also hide files like ".profile", ".login" etc.

* Any metacharacters of grep or egrep that are also metacharacters of sh have to be quoted. Which characters are these?

shell metacharacters include: * ? [ ] \ ' " ` | < > ; $ # { }
but there are probably a whole load more.

grep/egrep metacharacters are: \ ^ $ . [ ] * + ? | ( )

so at least these are in common: \ $ [ ] * ? |

Note that you also need to be careful with spaces and commas, as shells take these as argument separators.

e.g. find all students taking CS2041 and CS2052 (assuming these are next to each other in the file):

grep "CS2041 CS2052" students
or: grep CS2041\ CS2052 students

if they aren't next to each other in the file:

grep CS2041 students | grep CS2052

find all lines containing "a", any number of "b"s (including none) and then a "c":

grep ab\*c file
or: grep "ab*c" file

as "+" is not a shell metacharacter, we can find all lines containing "a", any number of "b"s (not including none) and then a "c" by:
egrep ab+c file

* Choose some complicated expressions and for each


e.g. ((1 + 2) * 3 / (- a) - (+ b)) * (c + d + e * 4)

a) fully bracket it


(((((1+2)*3)/(-a))-b)*((c+d)+(e*4)))

b) write it using the other representations in $1.3.1

 1 2 + 3 * a negate / b - c d + e 4 * + *
 * - / * + 1 2 3 negate a b + + c d * e 4
 (*, (-, (/, (*, (+, 1, 2), 3), (-, a)), b), (+, (+, c, d), (*, e, 4)))
 (*, (-, (/, (*, (+, 1, 2), 3), (-, a)), b), (+, c, d, (*, e, 4)))
 etc.
You must explicitly distinguish between negate and subtract in the unbracketed prefix and postfix forms, as there is no other way of detecting the difference. The infix and fully bracketed forms do not have this problem.

c) draw the corresponding parse tree


note: pre-/in-/post-fix correspond to pre-/in-/post-order left-to-right tree-walks

             *
          /    \ 
        -        +
       / \       / \ 
      /   b   +     *
     / \      / \    / \ 
    *   -   c   d e   4
   / \    |
  +   3 a
 / \ 
1   2
(see $8.3 of the lecture notes)

Try expressions involving functions calls and data structure accesses - see if you can find different ways of drawing the parse trees for these by making different assumptions about which parts of the expression are the operators.

e.g. "a (b, c, d)" is really:

		a
	/	|	\
b		c		d
but is often represented as:
		( )
	/		\
a				,
			/		\
		b				,
					/		\
				c				d
and similarly for "a [b, c, d]" or "a [b] [c] [d]"
(see $2.1 of the lecture notes)

* Use the "man" command to find out more details about "make", "grep", "egrep", and whichever shell you normally use (probably "ksh") e.g. type "man make" etc.

Use grep or egrep on some text files e.g. to list all the Subjects of all your email.

Write a shell command file to compile and run any program you wrote e.g. for a first semester lab.

Convert your shell command file into a makefile, that can compile and run your program as two separate steps. Add a third entry to the makefile to keep the output from the run and "diff" it with a previous output, so you can immediately see the consequences of any change to your program.

I hope you tried these. You will need to be able to do these sorts of things for the lab exercises.