(Click or use local copy.)

 CS 3721, Spring 2004
 Programming Languages

 Recitation 7: February 23, 25
 Tiny Compiler 1: While and if-then-else
    MW   09:00-09:50 pm, SB 3.01.04
 Due: 2004-03-01 23:59:59

Recitation 7 must be submitted following directions at: submissions on or before
  • 2004-03-01  23:59:59 (that's Monday, 1 March 2004, 11:59:59 pm) for full credit.
  • 2004-03-05  23:59:59 (that's Friday, 5 March 2004, 11:59:59 pm) for 75% credit.


Overview: This recitation extends the previous one to translate while, if-then-else and input statement, as well as everything translated before. You need to add extra code to your parser to translate these additional constructs.

You will now be using the full grammar from Recitation 5. (Below the part in bold red type is what needs to be implemented for this recitation.)

Grammar for Tiny language
M  --->  { S } '$'
S  --->  I  |  W  |  G  |  A  |  P  |  C
I  --->  '[' E '?' { S } ':' { S } ']'  |  '[' E '?' { S } ']'
W  --->  '{' E '?' { S } '}'
A  --->  lower-case '=' E ';'
P  --->  '<'  E  ';'
G  --->  '>'  lower-case ';'
C  --->  '<'  upper-case ';'
E  --->  T {('+' | '-') T}
T  --->  U {('*' | '/' | '%') U}
U  --->  F '^' U | F
F  --->  '(' E ')'  |  lower-case  |  digit


Translation of Constructs: One mainly has to decide how to translate each construct into MIPS assembly language (in a way consistent with the previous recitation).


Sample Inputs: You could process the two source programs given in the recitation about recursive descent parsing:

Sample Input 1
f = 0; g = 1; n = 0;
{ n - 8*5 ?
   h = f + g;
   < n; < B; < f; < B;
   [ f%2 ? < 1; : < 2; ]
   < N;
   n = n + 1;
   f = g; g = h;
}  $

which should produce the output: here, or

Sample Input 2
f = 1; g = 2; n = 3; > m;
{ m - n ?
   < n; < T; < g; < T;
   j = g; d = 2; t = 1;
   { t ?
      [ j%d ? e = 0; : e = 1; ]
      { e ?
         j = j/d; < d; < B;
         [ j%d ? e = 0; : e = 1; ]
      }
      [ j - 1 ? t = 1; : t = 0; ]
      [ d - 2 ? d = d + 2; : d = 3; ]
   }
   < N;
   n = n + 1;
   h = f + g;
   f = g; g = h;
}  $

which should produce the output: here


What you should submit: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Recitation Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, and c.

 Contents of submission for Recitation 7:

Last Name, First Name; Course Number; Recitation Number (7).

a. Java or C++ code for your translator, perhaps in multiple files.

b. Results of runs of your program using sample inputs above, giving the complete resulting MIPS code.

c. Results of executing the MIPS code in b using the SPIM simulator.


Key ideas: This recitation just adds features to the language. I hope that some of you are struck by how easy it is to add such complex statements as general while's and if-then-else's, where each can appear in an arbitrary nested fashion.


Revision date: 2003-03-04. (Use ISO 8601, an International Standard.)