CSCI 301 L26 Worksheet

Lecture 26 - Exercises

Part A - RPN

  1. Rewrite the following infix expression in RPN: \((4 + 4) / (6 - 2)\)

  2. Rewrite the following RPN expression in infix notation (that is, write out the expression, but don’t evaluate it): 8 4 - 2 *

  3. Rewrite the following RPN expression in infix notation (that is, write out the expression, but don’t evaluate it): 6 8 3 * + 6 /


Here’s the RPN grammar: \[ \begin{align*} S & \rightarrow \_ S \mid PS \mid DNT \mid \epsilon \\ T & \rightarrow \_ S \mid PS \mid \epsilon \\ N & \rightarrow DN \mid \epsilon \\ D & \rightarrow 0 \mid 1 \mid 2 \mid \cdots \mid 9 \\ P & \rightarrow + \mid - \mid * \mid / \\ \end{align*} \]

Part B - FIRST and FOLLOW

  1. Compute the FIRST set for each nonterminal in the grammar. | Nonterminal | FIRST | | —|—————- | | \(S\) | | | \(T\) | | | \(N\) | | | \(D\) | | | \(P\) | |

Part C - Recursive Descent Parsing

Here’s the function for the \(T\) production. The parse table is given in the header comment. Complete the code for this function by filling in the TODO items.

;; T
;; prediction
;;     P    _   D   null
;; T-> PS   _S  err e
(define T
  (lambda (input)
    (cond [(null? input) '()] ; If null, T -> e(psilon)
          [(char-whitespace? (car input)) ; TODO - _ case: T -> _S]
          [(D? (car input)) ; TODO - D case: error]
          [(P? (car input)) ; TODO - P case: T -> PS] 
           )))