Anybody familiar with the Triangle Comipiler?

D

Diphay Z

The triangle compiler, which was described in the "Programming
Language Processors In Java" --by Watt & Brown. Could anybody discuss
with me?

One of my exercise needs me to extend the compiler to support the
following statement:

for I from E1 to E2 do C, where I is an identifier, E1 and E2 are
expressions will be evaluated as integers, C is a command.

The difficult part of this expansion was in the code generator part.
This statement is hard to formulate a template.
e.g.
For a while command: while E do C
the template can be written as follows:
JUMP h
g: excute C
h: evaluate E
JUMP(1) g

but for the ForCommand, I cannot find out a method to formulate its
template. Anybody can help me? Thanks a lot
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Diphay said:
The triangle compiler, which was described in the "Programming
Language Processors In Java" --by Watt & Brown. Could anybody discuss
with me?

One of my exercise needs me to extend the compiler to support the
following statement:

for I from E1 to E2 do C, where I is an identifier, E1 and E2 are
expressions will be evaluated as integers, C is a command.

The difficult part of this expansion was in the code generator part.
This statement is hard to formulate a template.
e.g.
For a while command: while E do C
the template can be written as follows:
JUMP h
g: excute C
h: evaluate E
JUMP(1) g

but for the ForCommand, I cannot find out a method to formulate its
template. Anybody can help me? Thanks a lot

You haven't specified the exact semantics of the for loop, but assuming
that E1 and E2 will be evaluated only once, and I will be incremented by
one after each iteration, and the loop will continue until I > value of E2:

init:
i <- eval E1
j <- eval E2
jmp test
next:
do C
i <- i + 1
test:
cmp i, j
jna next

But really, you need to tell us what exactly the for statement is
supposed to do.
 
D

Diphay Z

Firstly, thanks for you reply. As described on the textbook, the for
loop is executed as follows. First, the expressions E1 and E2 are
evaluated, yielding the integers m and n (say), respectively. Then the
subcommand C is executed repeatedly, with identifier I bound in
successive iterations to each integer in the range m through n. If m >
n, C is not executed at all. (The scope of I is C, which may use the
value of I but may not update it. The types of E1 and E2 must be
Integer.) Here is an example:
for n from 2 to m do
if prime(n) then
putint(n)

thx!
 
S

Sudsy

Diphay said:
Firstly, thanks for you reply. As described on the textbook, ...

Homework questions are not appropriate to this newsgroup. If your
professor or TA can't help then they're not doing their job, eh?
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Diphay said:
Firstly, thanks for you reply. As described on the textbook, the for
loop is executed as follows. First, the expressions E1 and E2 are
evaluated, yielding the integers m and n (say), respectively. Then the
subcommand C is executed repeatedly, with identifier I bound in
successive iterations to each integer in the range m through n. If m >
n, C is not executed at all. (The scope of I is C, which may use the
value of I but may not update it. The types of E1 and E2 must be
Integer.)

In that case, the for loop can be compiled as I specified in my previous
post. Obviously, the syntax may differ in your particular code generator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top