Some programming languages have ``if'' expressions:
<expr> ::= <expr> + <expr> | <expr> = <expr> | <expr> / <expr>
| if <expr> then <expr> else <expr>
| <INTEGER> | <ID> | ( <expr> )
Thus one can write:
(if x = 0 then 1 else 10 / x ) + yHowever the grammar above is ambiguous.
if x = 0 then 1 else 10 / x + y''if has lowest precedence, then =, then +
and then /, with highest precedence.
Both + and / should have left associativity,
and = should be non-associative (x = y = z should
be a syntax error).
Write a C++ main program square.cc
that reads in an integer and then prints its
square, and then exits. This program should be 10 lines long at most.
Compile it using g++ on Unix in the
following ways: (Hint: Compile from the source each time.)
square.s).square.o).square).-static option to g++
(square-static).
For each of the four artifacts produced in the previous stage, answer
the
following questions in a text file README:
nm: it lists all the
``names'' in an object or executable file.nm gives the address each name is bound to.
Use ``nm something | grep main'' to cut down the
output to only lines with ``main'' in them.)
You submit your program work by putting the files (square.cc
and friends, as well as README) in the homework2 directory
in your AFS class volume.
You may do all your work in this directory, or you may wish to do your
work in a different directory and copy things when correct into this
directory. In any case, you will lose permission to write things in
this directory after the deadline, which is 4:00pm on Tuesday,
February 10th. In other words, you must be done before lecture
starts.
The homework2 directory should include the following:
square.ccsquare.s, square.o, square, square-staticREADME.