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

    1. No, the grammar is not ambiguous.

    2. I will give this in class.

    3. This grammar will not work for a recursive descent parser because there is left recursion in the grammar, for example, the symbol E occurs on the left side of a rule, and also as the first symbol on the right side, so that recursive descent would fall into indefinite recursion.
     

  2. You need to write two functions: sexpr and tail. Using the scanner, each function just looks like the corresponding right side of the rule. So the sexpr function returns if the next token is a digit and calls tail if the next token is a right paren. (A left paren is an error.) The function tail returns if the next token is a right paren, and calls sexpr and the tail otherwise.

    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 ) $       reduce: F ---> id
      $  F                      *       ( id + id ) $       reduce: T ---> F
      $  T                      *       ( id + id ) $       shift
      $  T  *                   (       id + id ) $         shift
      $  T  *  (                id      + id ) $            shift
      $  T  *  (  id            +       id ) $              reduce: F ---> id
      $  T  *  (  F             +       id ) $              reduce: T ---> F
      $  T  *  (  T             +       id ) $              reduce: E ---> T
      $  T  *  (  E             +       id ) $              shift
      $  T  *  (  E  +          id      ) $                 shift
      $  T  *  (  E  +  id      )       $                   reduce: F ---> id
      $  T  *  (  E  +  F       )       $                   reduce: T ---> F
      $  T  *  (  E  +  T       )       $                   reduce: E ---> E + T
      $  T  *  (  E             )       $                   shift
      $  T  *  (  E  )          $                           reduce: F ---> ( E )
      $  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).