CS75 Written Homework Assignments

About Written Homework
I do not accept late homework assignments for a grade, but I encourage you to try all written homework problems even if you do not submit them; written assignments will help you understand the lecture material and will give you some idea of the types of questions that may appear on exams.

You are welcome to work in small groups on written homework assignments. However, if you do so, I suggest first trying to solve all the problems individually, then meeting as a group to come up with a final solution together (rather than having each member solve a subset of them individually); it is good to test your understanding of the material by trying to solve every homework problem on your own first. If you work in a group, please submit a single solution with all of your names on it, so that it is clear with whom you collaborated and so that I do not have to grade several identical solutions.

You should submit a hardcopy of your written homework assignments.

Homework 5: Due Thurs April 23 by 11:20am

Either email me your solution before the due date or hand it in to Rich before the due date (either in person or slide it under his office door).

Construct the SLR parse table for the following grammar, and make sure to show all the steps in its construction.
B --> B || T
B --> T
T --> T && F
T --> F
F --> true
F --> false
Using your parse table, show the shift-reduce parse steps (like I do in class) for the parse of the string: true || false
Homework 4: Due Thurs April 2 by 11:20am
Convert the three C-- programs below to MIPS assembly code, and then try running your MIPS assembly files on spim or xspim. I suggest starting with friday's lab assignment, and looking at the SPIM and MIPS references off the "On-line Recourses" part of the homepage.

// program 1:
int main () {
  write 9;
  writeln;
}

// program 2:
int main () {
  write (9 + 2 * 4);
  writeln;
}

// program 3: 
// for this one, do not try to optimize the code you write
// by removing some loads and stores,  instead implement MIPS code
// that a naive compiler would generate:  a store instruction for 
// x = 8, followed by a load instruction to get x for 3 + x, ...
int x;      

int main () {
    x = 8;
    x = 3 + x;
    write x;
    writeln;
}
hand in this assignment by emailing me your three MIPS assembly files as attachments.
Homework 3: Due Tues Feb 23 by 11:20am
Given the following grammar:
Expr -->   Expr + Expr 
         | Expr * Expr 
         | Expr POW NUM 
         | (Expr) 
         | NUM
where POW is the exponentiation operator (e.g. 2 POW 4 is 16), is right associative, and has higher precedence than *

Do the following:

  1. Convert it to an LL(1) grammar using the techniques we have talked about in class.
  2. Using your LL(1) version of this grammar, show the parse tree and the abstract syntax tree for the following strings:
    1. (2 + 4) POW 3
    2. 2 + 4 + 3
  3. Compute the FIRST and FOLLOW sets for your LL(1) grammar
  4. Show the parse table for your LL(1) grammar

Homework 2: Due Feb 05 by 11:20am (I encourage you to finish this earlier)
  1. Turn in solutions to the following problems from Chapter 3 of the book (p.125):
    • 3.3.2
    • 3.3.3
    • 3.3.5 (submit any 6 of these 8 problems, and try to get them all for an extra challenge). Also, for (d) and (e) use a smaller digit alphabet ({0, 1, 2} is fine) rather than using an alphabet of all 10 digits in your regular expressions.

  2. Convert the following regular expression to an NFA, and then convert the NFA to a DFA by applying the algorithms we discussed in class:
      (a | ab)*
      
    This problem is designed to give you some practice with the algorithms we discussed in class. In your solution, you should show all the steps of applying these algorithms (don't just come up with a DFA for this regular expression, which I'm sure you can do without applying the algorithms).
Homework 1: Due Jan 29th by 11:20am
  1. Turn in solutions to the following problems from Chapter 2 of the book:
    • 2.2.1
    • 2.2.2 a, b, c, d
      For the "justification" part, just add a sentence or two with an informal explanation of how/why this grammar generates the language (or why this language is generated from this grammar); you do not need to provide a formal proof.
    • 2.2.3 a, b, c, d

    As an extra challenge, try problem 2.2.5.

    If you do not know binary number representation, here is a brief description
    (X^K is notation for X raised to the Kth power):
    
    binary numbers are base 2 numbers consisting of strings of 0's and 1's 
      decimal numbers are base 10 numbers consisting of strings of {0,1,...,9}
      base 3 numbers consist of strings of {0, 1, 2}
      base 4 numbers consist of string os {0, 1, 2, 3}
      and so on 
    
    for any base number:
      the lowest order digit counts the number of base^0 occurrences in the value
      (either 0 occurrences, or 1 occurrence, or ... , or base-1 occurrences)
      the 2nd lowest order digit counts the number of base^1 occurrences in the value
      the 3rd lowest order digit counts the number of base^2 occurrences in the value
      and so on
    
    For example:
      the number 101 in decimal has a value of 1*10^2 + 0*10^1 + 1*10^0 = 101
      the number 101 in binary has a value of 1*2^2 + 0*2^1 + 1*2^0  =  5  
      the number 101 in base3 has a value of  1*3^2 + 0*3^1 + 1*3^0  =  10 
    
    To represent the decimal value 23 in:
      base 10:  2*10^1 + 3*10^0   is  23
      base 4:  1*4^2 + 1*4^1 + 3*4^0  is  113
      base 2:  1*2^4 + 0*2^3 + 1*2^2 + 1*2^1 + 1*2^0  is 10111
    
    For this problem, the two terminals in the grammar are 11 and 1001, which are the decimal values 3 and 9.

  2. Try out the simple compiler from Chapt. 2:

    Copy the simple compiler code (based on the infix to postfix compiler from Chapt. 2) from ~newhall/public/cs75/simplecompiler/ to one of your subdirectories. Read the README file, then try compiling and running it with different infix expression input. By reading through chapt. 2 of the book and by reading through the simple compiler's source code you will get an idea of how you will organize and implement parts of your C compiler.

    In addition, the main function has some commented-out memory access errors. You may want to try uncommenting these errors, re-making, and then running with valgrind to get some idea of how valgrind works. I'll be giving a gdb and valgrind demo next week.