CS 3723-001 Programming Languages
Fall 2001 -- Partial Answers to Mid-term Exam
  1. In class.
    1. In class.

    2. Here is one version:
      
               declist --->   dec  { dec }
               dec     --->   type  id { "," id }  ";"
               type    --->   "double"  |  "int"
      
     

    1. These represent error entries (an error condition).

    2. 
      Stack (top at right)      Curr    Rest of Input       Action
                                Sym
      --------------------------------------------------------------------------
      $                         (       id + id ) * id $    shift
      $  (                      id      + id ) * id $       shift
      $  ( id                   +       id ) * id $         reduce: F ---> id
      $  ( F                    +       id ) * id $         reduce: T ---> F
      $  ( T                    +       id ) * id $         reduce: E ---> T
      $  ( E                    +       id ) * id $         shift
      $  ( E  +                 id      ) * id $            shift
      $  ( E  +  id             )       * id $              reduce: F ---> id
      $  ( E  +  F              )       * id $              reduce: T ---> F
      $  ( E  +  T              )       * id $              reduce: E ---> E + T
      $  ( E                    )       * id $              shift
      $  ( E )                  *       id $                reduce: F ---> ( E )
      $  F                      *       id $                reduce: T ---> F
      $  T                      *       id $                shift
      $  T  *                   id      $                   shift
      $  T  *  id               $                           reduce: F ---> id
      $  T  *  F                $                           reduce: T ---> T * F
      $  T                      $                           reduce: E ---> T
      $  E                      $                           reduce: S ---> E
      $  S                      $                           accept
      
    3. In this case the parse would get to a * on the top of the stack and a + as the current symbol. The corresponding entry in the table is blank (an error entry).

  2. Here is code for the parser, including extra debug code but without a few of the functions that have been in earlier listings. (The part that you should have supplied is in boldface.)
    
    /* GrammarDebug.java: simple parser -- no output
     * grammar:
     *           P ---> S  '$'
     *           S ---> 'b'  M  'b'               (S is the start symbol)
     *           M ---> '('  L
     *           M ---> 'a'
     *           L ---> M  'a'  ')'
     */
    import java.io.*;
    class GrammarDebug {
       private int level = 0;
       private char next;
    
       private void P() {
          scan();
          S();
          if (next != '$') error(2);
          else System.out.println("Successful parse");
       }
          
       private void S() {
                                                    enter('S');
          if (next == 'b') scan();
          else error(0);
          M();
          if (next == 'b') scan();
          else error(1);
                                                    leave('S');
       }
    
       private void M() {
                                                    enter('M');
          if (next == '(') {
             scan();
             L();
          }
          else if (next == 'a') scan();
          else error(3);
                                                    leave('M');
       }
    
       private void L() {
                                                    enter('L');
          M();
          if (next == 'a') scan();
          else error(4);
          if (next == ')') scan();
          else error(5);
                                                    leave('L');
       }
    
       public static void main(String[] args) {
          GrammarDebug grammarDebug = new GrammarDebug();
          grammarDebug.P();
       }
    }
    
    Here is a run of the program, with input shown in boldface:
    ten42% java GrammarDebug
    b ( ( a a ) a ) b $
    +-S: Enter,     Next == b
    | +-M: Enter,   Next == (
    | | +-L: Enter,         Next == (
    | | | +-M: Enter,       Next == (
    | | | | +-L: Enter,     Next == a
    | | | | | +-M: Enter,   Next == a
    | | | | | +-M: Leave,   Next == a
    | | | | +-L: Leave,     Next == a
    | | | +-M: Leave,       Next == a
    | | +-L: Leave,         Next == b
    | +-M: Leave,   Next == b
    +-S: Leave,     Next == $
    Successful parse