|
CS 3721
Programming Languages
Spring 2014 |
Recitation 3.
Semantic Actions
|
Week 3: Jan 28-30
|
Submit following directions at:
submissions
and rules at:
rules.
Deadlines are:
- 2014-02-04 23:59:59 (that's Tue, 04 Feb 2014, 11:59:59 pm)
for full credit.
- 2014-02-07 23:59:59 (that's Fri, 07 Feb 2014, 11:59:59 pm)
for 75% credit.
|
Semantic Actions and MIPS Assembly Language:
Study the pages:
Semantic Actions
- Study the the page Semantic Actions.
This problem asks you to do the same things as Part 1 of that page,
the part that translates to RPN.
Use the grammar and example from Problem 2 of Recitation 2.
Assume the four ids in the sentence from Problem 2
is tagged with
a, b, c, and d, in that order, as
shown below:
Input sentence
with tags |
$ id + id * id ^ id $
| | | |
a b c d
|
Here is the grammar from Problem 2, written with one
rule per line as shown below. Add semantic actions to the grammar rules,
so that the result of parsing the input above (and of carrying
out the semantic actions) will be to translate the sentence
into RPN.
Grammar:
Arithmetic Expressions |
P −−> E
E −−> E + T
E −−> E - T
E −−> T
T −−> T * S
T −−> T / S
T −−> S
S −−> F ^ S
S −−> F
F −−> ( E )
F −−> id
|
Modify add to your answer to Problem 2 of Rec,
so that you show the results of the semantic actions,
which should be the RPN form of the input expression,
- This problem asks you to do the same things as Part 2
of the Semantic Actions page, the part that translates to a
simple intermediate code.
Again add semantic actions the grammar so that it will generate
similar intermediate code similar to that on the
Semantics Page.
You can mostly use the same semantic actions given there, except
that you need similar ones for the operator ^.
You should end up with complete
actions beside each grammar rule:
Again modify your answer to Problem 2 of Recitation 2
so that you show the results of the semantic actions,
which should be the input expression rewritten as a
sequence of intermediate code instructions.
MIPS Assembler Language
- Translate the
following sequence of assignments into MIPS and print the
final value stored in e.
Translate to MIPS |
a = 8 * 2;
b = 7 * a;
c = b + 1;
d = a / c;
e = 3 + d;
(print value of e)
|
This is not hard if you follow the pattern of the MIPS program in
the third link at the top of this page. Here are two tables from
the second link above:
Successive Steps
of Conversion to MIPS |
f = g + 5
add f, g, 5
load value of location g into register $f2
load value of 5 into register $f4
add values in $f2 and $f4 , store result in $f6
store value in $f6 into location f
load 128($s1) into $f2
load 40($s1) into $f4
add $f2 and $f4 , result in $f6
store the value of $f6 into 120($s1)
l.d $f2, 128($s1)
l.d $f4, 40($s1)
add.d $f6, $f2, $f4
s.d $f6, 120($s1)
|
| |
Offsets for
Digits, Letters |
Dig | Off | Let | Off | Let | Off |
0 | 0 | a | 80 | n | 184 |
1 | 8 | b | 88 | o | 192 |
2 | 16 | c | 96 | p | 200 |
3 | 24 | d | 104 | q | 208 |
4 | 32 | e | 112 | r | 216 |
5 | 40 | f | 120 | s | 224 |
6 | 48 | g | 128 | t | 232 |
7 | 56 | h | 136 | u | 240 |
8 | 64 | i | 144 | v | 248 |
9 | 72 | j | 152 | w | 256 |
| | k | 160 | x | 264 |
| | l | 168 | y | 272 |
| | m | 176 | z | 280 |
---|
|
This shows how to translate f = g + 5 to MIPS assemply.
Translation of the earlier 5 assignments is essentially the same.
In order to print the value in the variable e, the third link
above gives code to print the value in variable a, with
a newline afterwards. (It turns out you have to do it exactly as
shown, except for the number 80, which is the offset of "a".)
Printing Value
of 80($s1), or a |
# Print value in a
li $v0, 3
l.d $f12, 80($s1)
syscall
# Print NewL as ASCII char
li $v0, 4
la $a0, NewL
syscall
|
Finally, from the third link above, you need the two instructions
at the beginning, and everything after the comment # Stuff at end #.
We will be explaining most of these issues.
Revision date: 2014-01-27.
(Please use ISO
8601, the International Standard.)
|