[Instructions] [Search] [Current] [News] [Syllabus] [Handouts] [Outlines] [Assignments]
Handouts
Notes
1: program whatever; 2: begin 3: int j,k; -- Not sure yet 4: int n = 10; -- The number of elements 5: array(1..10) of real A; -- A is an array 6: A[1] = 1; -- Set up the first element of the array 7: int i; -- A counter variable 8: for i = 2 to n do 9: A[i] = i + A[i-1]; 10: od 11: end.
program id(whatever) semi begin int id(j) comma id(k) semi assign integer(4) semi int id(n) assign integer(10) semi array lparen integer(1) ellipses integer(10) rparen of real id(A) semi id(A) lbracket integer(1) rbracket assign integer(1) semi int id(i) semi for id(i) assign integer(2) to id(n) do id(A) lbracket id(i) rbracket assign id(i) plus id(A) lbracket id(i) minus integer(1) rbracket semi od end period
PROGRAM
program
id(whatever)
arglist
empty
semi
BODY
STATEMENT
COMPOUND-STATEMENT
begin
STATEMENT-LIST
STATEMENT
DECLARATION
TYPE
int
ID-LIST
ID-LIST
ID
id(j)
comma
ID
id(k)
semi
STATEMENT-LIST
...
end
period
STATEMENT
|
ASSIGNMENT
/ | \
LVALUE assign EXP
/ |
ARREF BINOP
/ | | \ / | \
id(A) [ EXP ] EXP + EXP
| | |
id(i) id(i) ARREF
/ | | \
id(A) [ EXP ]
|
BINOP
/ | \
EXP - EXP
| |
id(i) integer(1)
assign
/ \
ARREF subtract
/ \ / \
id(A) id(i) id(i) ARREF
/ \
id(A) integer(1)
A is used on line 6. It is declared on line 5.
i is used on line 8. It is declared on line 7.
n is used on line 8. It is declared on line 4.
A is used on line 8 (twice). It is declared on line 5.
i is used on line 9. It is declared on line 7.
n is used on line 8. Is it assigned earlier?
Yes. It is assigned on line 4.
i is used on line 9. Is it assigned earlier?
Yes. It is assigned on line 8.
A[i-1] is used on line 9. Is it assigned
earlier? It's probably beyond our capabilities to check right now.
1 is used as an index into array A.
Is it a valid index? It is an integer, and A is indexed
by integers. It has the value 1, and A permits indices
between 1 and 10.
i is used as an index into array A.
Is it a valid index? It is an integer variable. It is probably too
difficult to ensure that its value is acceptable.
i-1 is used as an index into array A
Is it a valid index? i is an integer. 1
is an integer. Hence, i-1 is an integer, and valid in
that sense.
i + A[i-1] is assigned to
A[i]. The right-hand-side must have type real, since
A contains real values. i is an integer,
being added to a real. Can we do that?
base
that holds the base of the heap.
base j base+4 k base+8 n base+12 A[1] base+16 base+20 A[2] ... base+92 i
base is stored in temporary
0 (basically, register 0).
SEQ(
MOVE(MEM(BINOP(+, TEMP(0), 8), 4), CONST(10)),
SEQ(
MOVE(MEM(BINOP(+, TEMP(0), 12), 8), CONST(1)),
SEQ(
MOVE(MEM(BINOP(+, TEMP(0), 92), 4), CONST(2)),
SEQ(
LABEL(test)
SEQ(
CJUMP(LE,
MEM(BINOP(+, TEMP(0), 92), 4), MEM(BINOP(+, TEMP(0), 8), 4),
loop, endloop),
SEQ(
LABEL(loop),
...
SEQ(
JUMP(test)
SEQ(
LABEL(endloop),
null)
)))))))
t1 = i-1; t2 = t1-1; t3 = t2*8; t4 = base+8+t3; t5 = i-1; t6 = t3*8; t7 = base+8+t6; mem(t7) = i + mem(t4);
base+92).
t1 and t5 both hold i-1).
j and k.
i
increases by one each time through the loop, t4 increases
by 8 each time through the loop.
Back to Introduction. On to Debate on C vs. Java as implementation language.
[Instructions] [Search] [Current] [News] [Syllabus] [Handouts] [Outlines] [Assignments]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Fri Sep 11 12:28:03 1998.
This page generated on Wed Sep 16 11:21:14 1998 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu