CS 3723 Programming Languages
Spring 2002 -- Review for Mid-Term Exam
Exam is Wednesday, 20 March 2002

Topics: Review Exercises.

  1. Shift-Reduce Parsers:

      Consider the following grammar:
      
              S ---> b M b              ("S" is the start symbol)
              M ---> ( L
              M ---> a
              L ---> M a )
      
      Use the following shift-reduce table for this grammar:
      
                 |  b  |  a  |  (  |  )  |  $   |
            -----+-----+-----+-----+-----+------+
              S  |     |     |     |     |  acc |     ( "s" means "shift")
              M  |  s  |  s  |     |     |      |
              L  |  r  |  r  |     |     |      |     ( "r" means "reduce")
              b  |     |  s  |  s  |     |  r   |
              a  |  r  |  r  |     |  s  |      |     ( "acc" means "accept")
              (  |  s  |  s  |  s  |     |      |
              )  |  r  |  r  |     |     |      |
      
    1. Carry out the shift-reduce parse of the following sentence, showing the stack, current symbol, remaining symbols, and next action to take at each stage. (This sentence has the extra artifical symbol $ stuck in at the beginning and the end.)
      
           $ b ( ( a a ) a ) b $
      
      (Remember that you should initially shift the starting $ and then shift the next symbol.)

    2. Give the resulting parse tree.

  2. Recursive Descent Parser and Translator:

      First consider the following grammar:

           P ---> A  { A } '$'
           A ---> lcletter '=' E ';'
           E ---> T {('+'|'-') T}
           T ---> S {('*'|'/') S}
           S ---> F '^' S
           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;
           $
      
    1. 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.)

      Consider the following simplified symbolic form of quadruples:

      • Operators are just single characters: '=' for ASG, '+' for ADD, '-' for SUB, '*' for MUL, '/' for DIV, and '#' for LIT. along with the new operator '^'.
      • Constant operands are just single digits.
      • Variable operands are just single lower-case characters.
      • Temporary variable operands are just single upper-case characters, taken in sequence, 'A', 'B', 'C', etc.
      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)
      
    2. By hand, translate the following input to this special form, following the pattern above:
      
           x = 12;
           y = 13;
           z = x^2 + y^2;
           $
      


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