Homework # 2
due Monday, September 18, 2:00 PM

Ambiguous Grammars

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 ) + y
However the grammar above is ambiguous.

Standard compilation sequence

Write a file square.cc:

#include <iostream>
using namespace std;

int main()
{
  int i;
  cin >> i;
  cout << i*i;
  exit(0);
}

On andrew.cs.uwm.edu (ssh using port 53211), first compile this program to assembly (g++ -S square.cc) and then to an object file (g++ -c square.cc) and finally to an executable (g++ square.cc). Answer the following questions:

  1. Look in the assembly file (square.s) and find out in the function main how it refers to cin. Show that here. Run c++filt and then type a ``mangled'' name to show a readable name. What is the full name of cin ? Why does C++ mangle the name?
  2. main is a ``@function'' label. What other function labels are defined in the assembly file? Give their unmangled names.
  3. Use readelf -a square.o to examine the object file, square.o. How does the file refer to exit ?
  4. Look at the relocation table for the text segment: .rela.text. What does _ZNSolsEi refer to? Why is it there? Why is there no entry for int i; ?
  5. Use the command ldd on the executable a.out. This indicates what shared libraries are used by the executable and where they (currently) can be found. List each and investigate what each does; report why each is being used.

Submitting Your Work

Turn in your answers on paper.


About this document



John Tang Boyland 2017-09-12