CS 3723 Programming Languages
Fall 2001 -- Simple Quadruple Assignment

Recursive Descent Parser and Translator:

  1. First consider the following grammar:

         P ---> A  { A } '$'
         A ---> lcletter '=' E ';'
         E ---> T {('+'|'-') T}
         T ---> S {('*'|'/') S}
         S ---> F '^' S  |  F
         F ---> lcletter | '(' E ')' | digit
    
    This is a grammar for a simple language consisting of a sequence of assignment statements, terminated by semicolons. The whole program is terminated by a sharp ($). Operands are single lowercase letters or single digits. Operators are = + - * / ^

    Here is a sample program (sentence) in this language:

    
         a = 4;
         b = (3 + a)*6;
         c = a^2 + b^2;  $
    
    Adapt the parser for arithmetic expressions handed out in class to this slightly larger language, so that you can parse the input above.
    (Here is the original parser: In C,     In Java.
    Here is the same original parser with extra debug output: In C,     In Java.)

  2. Next consider the following simplified symbolic form of quadruples:

    With these conventions, the sample program above becomes the following as a sequence of symbolic quadruples.

    
         (#,4,~,A)
         (=,A,~,a)
         (#,3,~,B)
         (+,B,a,C)
         (#,6,~,D)
         (*,C,D,E)
         (=,E,~,b)
         (#,2,~,F)
         (^,a,F,G)
         (#,2,~,H)
         (^,b,H,I)
         (+,G,I,J)
         (=,J,~,c)
         ($,~,~,~)
    
    Add extra code to your parser so that it will translate the sequence of assignment statements into this simple form of quadruples.

    Hints:

    1. You should modify the code from the application that evaluates arithmetic expressions. (See item 6 under handouts.) The code that you need here is very similar, except that instead of returning a double, you should return a char, one that represents the expression that has so far been calculated (either a lower-case letter, or in case of a temporary an upper-case letter).

    2. At various times you will need a new temporary variable for the result field of one of the simple quadruples. For this assignment, these should be upper-case letters A, B, .... Here is a simple Java function that will generate a new temporary each time it is called:
      
         private static char startIt = 'A'; // class variable
      
         private static char newTemp() {
            return startIt++;
         }
      
    3. In case of a digit (a constant), you will need to generate a new temporary variable, output a '#' quad, and then return the new temporary as a char.

    4. In case of a '+' operator, there will be two letters returned from two calls to functions T(). You will need to generate a new temporary, output a '+' quad, and then return the temporary as a char.

    5. In case of a variable (lower-case letter), you just return that letter.

    Testing:

    You can try the sample input above, or even simpler input initially. I have a working C interpreter for this simple language: source in C,   source in HTML. NOTE: this C source must be compiled with an extra -lm option ("math library") because of the pow function. (This interpreter assumes that the source looks like the above, with a non-blank char for and "unused" parameter in a quad.) My interpreter also prints out the number assigned whenever there is an assignment quad. For the first input, the interpreter produces:

    m[97] = 4.000000
    m[98] = 42.000000
    m[99] = 1780.000000
    
    What to turn in:

    Try out the following solution to the quadratic formula as input to your program:

    
         a = 1; b = 3; c = 1;
         d = b*b - 4*a*c;
         e = d^(1/2);
         r = (0 - b + e)/(2*a); $
    
    After translating, my interpreter should produce the following when your quads are fed into it:
    m[97] = 1.000000
    m[98] = 3.000000
    m[99] = 1.000000
    m[100] = 5.000000
    m[101] = 2.236068
    m[114] = -0.381966
    
    Then try the following, with a more complex expression Here the output should be the same as that above, except for lines 4 and 5 omitted:
    
         a = 1; b = 3; c = 1;
         r = (0 - b + (b*b - 4*a*c)^(1/2))/(2*a); $
    


Revision date: 2001-10-25. (Please use ISO 8601, the International Standard.)