CPSC 460
Project Part 3: minyacc.y, minlex.l
Intermediate Code Generation

This is the third and final part of the compiler development project that we are doing this semester. Please begin work on this project immediately as it is considerably more open ended than the first two and will take some time to complete. Recall, you are building a compiler for the minl programming language. Minl is a minimal programming language containing just enough high level features to write useful code. (Remember that this is an approach to building a compiler for a full version of a language.) The first part of the project had you construct the lexical analyzer using the Lex tool. The second part of the assignment had you construct the syntactical analyzer using the Yacc tool. This part of the assignment requires you to generate an intermediate code of the input program. Specifically, your Yacc specification of the minl grammar needs to be modified to generate an equivalent program in the RSIC (Really Simple Intermediate Code) language. The RSIC language contains a mere 8 instructions, which are described below.
HALT Halt the program
DECLARE,vname,INTEGER Declares vname to be an integer
variable
LABEL,labname Attaches the label labname to the current location
STORE,value,vname Stores the value into the variable vname
WRITE,vname Writes the value of vname to the screen
JUMP,labname Jump to the location given by labname
JUMP0,value,labname Jump to labname if the value is 0
OP,v1,v2,vname Apply operation OP to v1 and
v2, store result in vname OP can be MULT, ADD, SUB, EQ, LT

Specific requirements

  1. Each RSIC instruction must be on a separate line, with no spaces before or between the instruction components. Note that the components are separated by a comma.
  2. Labels must start with a '@' character, as shown in the sample output.
  3. The HALT instruction must be the last instruction.
  4. Before halting the program, your translator must output the values of all the variables in the original program (i.e., not the temporary variables introduced by the compiler). The values must be output exactly in the same order in which they are declared in the source minl program.
  5. Declarations can be located anywhere in the RSIC program, but a variable must be declared before it is used. (This means that you are not required to do all variable declarations before the first executable statement.)
  6. Your translator should expect one command line argument that identifies the file containing the input minl program. It should create an output file with the same base name as the input file, but with a different extension. For example, the input file myprog.minl should create the file myprog.rsic that contains the equivalent RSIC program. Or, you can use redirection. Specify in a comment in your program how to run it.

Sample Input

program foo; 
 int x, y, z ;  
 int a, b, c ;  
 int d ;  
begin
 x = 5 ; 
 y = 7; 
 z = 9; 
 a = x + (y*z) ; 
 b = a + (x + (y*z)) ; 
 c = b - a ; 
 while (c - a) == 0 
   begin
      d = y - x ;  
      if d == 0   then 
        c = b - a ;   
      else 
        c = b + a ;   
      endif ;  
   end ;     
end

Output for Sample Input

DECLARE,x,INTEGER 
DECLARE,y,INTEGER 
DECLARE,z,INTEGER 
DECLARE,a,INTEGER 
DECLARE,b,INTEGER 
DECLARE,c,INTEGER 
DECLARE,d,INTEGER 
STORE,5,x         
STORE,7,y         
STORE,9,z          
DECLARE,T1,INTEGER 
MULT,y,z,T1        
DECLARE,T2,INTEGER 
ADD,T1,x,T2        
STORE,T2,a         
DECLARE,T3,INTEGER 
MULT,y,z,T3        
DECLARE,T4,INTEGER 
ADD,T3,x,T4        
DECLARE,T5,INTEGER 
ADD,T4,a,T5        
STORE,T5,b         
SUB,b,a,c          
LABEL,@1            
DECLARE,T6,INTEGER  
SUB,c,a,T6          
JUMP0,T6,@2         
JUMP,@3             
LABEL,@2            
SUB,y,x,d           
JUMP0,d,@4          
JUMP,@5             
LABEL,@4             
SUB,b,a,c            
JUMP,@6              
LABEL,@5             
ADD,b,a,c            
LABEL,@6             
JUMP,@1              
LABEL,@3             
WRITE,x              
WRITE,y              
WRITE,z              
WRITE,a              
WRITE,b              
WRITE,c              
WRITE,d              
HALT        

Testing your program

An RSIC interpreter is available for your use, which allows you to test for correctness by executing the output of your translator. The interpreter takes a single command line argument which is the RSIC input program (i.e., the output of your program). For example, if you have the program above and name your output file try.r then you can test your output as input to the interpreter by typing: ~lambert/Public/interp try.r If you do that, you should get the following output:

  Instructions stored.  Executing the program now: 


 VAR INT 5
 VAR INT 7
 VAR INT 9
 VAR INT 68
 VAR INT 136
 VAR INT 204
 VAR INT 2
(that is, the Write for x, y, z, a, b, c, d)

To hand in

You will need two files, your yacc (minyacc.y) and your lex (minlex.l). If you have any header file information, please put it in minlhead.h, and submit that also (that is not required).