Computer Science 433
Programming Languages
Fall 2014, The College of Saint Rose
In this assignment, you will be completing some problems related to syntax, grammars, and parse trees.
You may work alone or with a partner on these problems. In order to earn full credit, you must show all of your work for these problems.
Textbook Problems
Reverse Polish Notation
Reverse Polish notation (RPN) is a common name for postfix mathematical expressions. Such notation lends itself to a stack-based evaluation and is used by the PostScript printer language and some old calculators. It has the advantage that it can specify any expression that can be specified in the usual (infix) notation without the need for parentheses to enforce order of operations.
If we are given an expression in RPN we would evaluate it as follows.
So for the expression
9 7 12 - *
we start with the empty stack. We then push 9, push 7, push 12, so our stack consists of [ 9 7 12 ]. We encounter the - operator, pop the 12 and the 7 from the stack, calculate 7 - 12 = -5 and push the result, giving a stack of [ 9 -5 ]. We then encounter the * operator, pop the -5 and 9 from the stack, calculate 5 * -9 = -45 and push the result, giving a stack of [ -45 ]. Now there are no more tokens on the input, so the value at the top of the stack contains our result, -45.
5 8 19 + *
2 3 + 5 7 * /
7 * 12 + 9 / 3
23
2 3 + 5 7 * / 3 4 + * 1 -
9 9 * 8 7 * * 5 5 * * 4 -
5 - 3 * 2 + 7
10 * 3 * 9 / 4
(5 + 8) / (9 -3)
5 + 8 / 9 - 3
3 4 + 9 3 - 4 * *
A Grammar for BASIC
Considered this grammar for a subset of the BASIC language.
<program> => <lines> <lines> => <line> | <line> <lines> <line> => <line-number> <stmt> \n <line-number> => integer-literal <stmt> => REM string-literal | PRINT <print-expr> | INPUT <variable> | LET <assignment> | END <variable> => <integer-var> | <string-var> <integer-var> => integer-variable-name <string-var> => integer-variable-name$ <print-expr> => "string-literal" | <variable> <assignment> => <integer-var> = integer-literal | <string-var> = "string-literal"
10 REM THIS IS FUN!
20 LET X = 8
30 PRINT X
Three types of BASIC statements that are not included are the IF/THEN construct, the standard GOTO statement, and the IF/GOTO construct
An IF/THEN looks like this:
30 IF X>5 THEN LET X = 5
A GOTO statement looks like this:
70 GOTO 10
An IF/GOTO statement looks like this:
100 IF Y<>Z GOTO 150
Submission
Before 11:59 PM, Monday, September 15, 2014, submit your work for grading. Create and submit a single archive file (a .7z or .zip file containing all required files) using Submission Box under assignment "PS2".
Grading
This assignment will be graded out of 45 points.
Problem | Value | Score |
Q1: Sebesta Exercise 12 | 2 | |
Q2: Sebesta Exercise 13 | 4 | |
Q3: Sebesta Exercise 14 | 4 | |
Q4: Sebesta Exercise 20 | 5 | |
Q5: RPN expression evaluations | 3 | |
Q6: Convert infix to RPN | 2 | |
Q7: BNF for RPN | 8 | |
Q8: Parse tree for RPN | 4 | |
Q9: Leftmost derivation/parse tree for BASIC program | 5 | |
Q10: Augmented BASIC BNF grammar | 8 | |
Total | 45 | |