Demonstrate use of Rules
Use Syntactic Derivation to Generate Semantic Rules; Use Semantic
rules to Generate Code
X = Y;
Derivation Syntax Rules Semantic Rules
s1 R3: s1 -> n2=e3; code(s1) = code(e3)
COPY AX,M(e3)
COPY M(n2),AX
MEANING: code(s1) = code (e3)
COPY AX,M(e3)
COPY M(n2), AX
n2 = e3; R1: n2 -> X M(n2) = X
MEANING: code(s1) = code (e3)
COPY AX,M(e3)
COPY X, AX
X = e3; R2: e3 -> n4 M(e3) = M(n4)
code(e3) = //nothing//
MEANING: code(s1) = //nothing//
COPY AX,M(n4)
COPY X, AX
X = n4; R1: n4 -> Y M(n4) = Y
MEANING: code(s1) = COPY AX,Y
COPY X, AX
X = Y;
Now Rules R4 and R5
| Syntax Rule | Semantic Rules
|
|---|
R4: <e>i->(<e>j+<e>k)
M(<e>i) = createname
| | code(<e>i)= |
code(<e>j)
| |
code(<e>k)
| |
COPY AX,M(<e>j)
| |
ADD AX,M(<e>k)
| |
COPY M(<e>i),AX
|
| |
Says code for <e>i is code to calculate expression <e>j
followed by code to calculate expression <e>k
and code to add them together and store that sum into Memory
associated with <e>i
| Syntax Rule | Semantic Rules
|
|---|
R5: <e>i->(<e>j*<e>k)
M(<e>i) = createname
| | code(<e>i)= |
code(<e>j)
| |
code(<e>k)
| |
COPY AX,M(<e>j)
| |
MUL AX,M(<e>k)
| |
COPY M(<e>i),AX
|
| |
Says code for <e>i is code to calculate expression <e>j
followed by code to calculate expression <e>k
and code to multiply them together and store that sum into Memory
associated with <e>i
Basically rules R4 and R5 are identical except that the + and ADD
in one are replace by the * and MUL in the other.
Look at Example from Pages 275-276 of book
It does Z = (X + Y)
More complicated example for U1 = (X + (Y * Z)) also in book. Look at it.
Rules for Looping
Sequence of statements
| Syntax Rule | Semantic Rules
|
|---|
|
|
code(<q>i) = |
code(<s>j)
|
|
code(<q>k)
|
|
| Syntax Rule | Semantic Rules
|
|---|
R7: <q>i -> <s>j
code(<q>i) = code(<s>j)
| |
Says code for a sequence of statements is the code for the
first statement followed by the code for the next statement, etc.
Compound Statement
| Syntax Rule | Semantic Rules
|
|---|
|
| code(<c>i) = code(<q>j)
|
While Statement
| Syntax Rule | Semantic Rules
|
|---|
R9: <s>i-> |
while (<n>j < <e>k)
|
|
<c>h
|
|
M(<s>i) = createname
|
M'(<s>i) = createname
|
code(<s>i) = |
M(<s>i) |
code(<e>k)
|
| COPY AX,M(<n>j)
|
| CMP AX,M(<e>k)
|
| JNB M'(s<n>)
|
| code(<c>h)
|
| JMP M(<s>i)
|
M'(<s>i) |
NO-OP
|
|
Cleanup Translation
Some code generated that can be removed.
Important: Everything done by simple substitution
Everything "adds up"
- code( { <s>1;<s>2;<s>3 } )
- is
- code(<s>1)
- code(<s>2)
- code(<s>3)