Anybody familiar with the Triangle Comipiler?

Discussion in 'Java' started by Diphay Z, Dec 8, 2004.

  1. Diphay Z

    Diphay Z Guest

    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
     
    Diphay Z, Dec 8, 2004
    #1
    1. Advertising

  2. Diphay Z wrote:
    > 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.

    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Dec 8, 2004
    #2
    1. Advertising

  3. Diphay Z

    Diphay Z Guest

    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!
     
    Diphay Z, Dec 8, 2004
    #3
  4. Diphay Z

    Sudsy Guest

    Diphay Z wrote:
    > 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?

    --
    Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.
     
    Sudsy, Dec 8, 2004
    #4
  5. Diphay Z wrote:
    > 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.

    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Dec 8, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Peters

    Re: Anyone familiar with TAR_DLY?

    Andy Peters, Nov 18, 2005, in forum: VHDL
    Replies:
    1
    Views:
    498
    Kai Harrekilde-Petersen
    Nov 18, 2005
  2. Leveridge Systems INC
    Replies:
    1
    Views:
    350
    =?Utf-8?B?TGVubg==?=
    Oct 21, 2004
  3. JK
    Replies:
    1
    Views:
    316
    Tony Morris
    Sep 28, 2004
  4. Fábio Santos

    Re: Anybody familiar with pygments ?

    Fábio Santos, May 9, 2013, in forum: Python
    Replies:
    0
    Views:
    109
    Fábio Santos
    May 9, 2013
  5. Chris “Kwpolska†Warrick

    Re: Anybody familiar with pygments ?

    Chris “Kwpolska†Warrick, May 10, 2013, in forum: Python
    Replies:
    0
    Views:
    149
    Chris “Kwpolska†Warrick
    May 10, 2013
Loading...

Share This Page