Associativity of binaries in Oberon0.

12 views
Skip to first unread message

db

unread,
Feb 7, 2011, 9:16:56 PM2/7/11
to LDTA 2011 Tool Challenge
Hello!

Actually I'm pretty sure this issue is considered in the book, but
frankly speaking I'm trying to avoid rummaging for the answer.
What is the associativity of binary operators in Oberon0? One would
expect left-associativity of additive operators (due to subtraction).
What about multiplicative? Do we mean (X MOD Y) DIV Z writing X MOD Y
DIV Z?

Best regards,
Dmitri.

Claus Brabrand

unread,
Feb 8, 2011, 5:36:50 AM2/8/11
to ldta-2011-to...@googlegroups.com
Dear Dimtri,

A quick text search for the word "assoc" in Niklaus Wirth's "Compiler
Construction" reveals:

In Section 5.2: "Evaluation Rules" (on p. 27) it says:
> exp(v0) = term(v1) | v0 := v1
> exp(v1) "+" term(v2) | v0 := v1 + v2
> exp(v1) "-" term(v2). v0 := v1 - v2
> term(v0) = factor(v1) | v0 := v1
> term(v1) "*" factor(v2) | v0 := v1 * v2
> term(v1) "/" factor(v2). v0 := v1 / v2
> factor(v0) = number(v1) | v0 := v1
> "(" exp(v1) ")". v0 := v1
Otherwise, it's presumably easy to change (the flexibility of this would be
interesting to consider in relation to your tool/formalism).

Cheers,
/Claus.

***


--
Claus Brabrand, Ph.D.
Associate Professor
IT University of Copenhagen (ITU)
Email: ((( brab...@itu.dk )))
WWW: ((( http://www.itu.dk/people/brabrand/ )))

Eric Van Wyk

unread,
Feb 8, 2011, 5:56:53 AM2/8/11
to ldta-2011-to...@googlegroups.com
Thanks for answering this.

-Eric

db

unread,
Feb 8, 2011, 6:12:06 AM2/8/11
to LDTA 2011 Tool Challenge
Dear Claus,

do this rules really deal with Oberon0 semantics? I'd rather consider
them as a mere illustration since
they are given prior to Oberon0 definition and didn't referenced back
later.

Best regards,
Dmitri
> Email: ((( brabr...@itu.dk )))
> WWW: (((http://www.itu.dk/people/brabrand/)))

Tony Sloane

unread,
Feb 8, 2011, 5:33:09 PM2/8/11
to ldta-2011-to...@googlegroups.com
Hi all,

The answer to the original question is not clear in the book, but with some poking around in the code I think we can conclude that binary operators are left associative.

Finding an answer is complicated by the fact that the Oberon-0 parser calls the code generator during the parse; no AST is built. My reasoning is as follows. Consider the parsing routine for terms:

PROCEDURE term(VAR x: OSG.Item);
VAR y: OSG.Item; op: INTEGER;
BEGIN
factor(x);
WHILE (sym >= OSS.times) & (sym <= OSS.and) DO
op := sym;
OSS.Get(sym);
IF op = OSS.and THEN OSG.Op1(op, x) END ;
factor(y); OSG.Op2(op, x, y)
END
END term;

This parses the left and right sides of a *, DIV, MOD, or & as factors, then immediately calls the code generator with the operator and the two operands. OSG.Op2 produces the instruction for this operation. (The code generation for & is more complex, since short-circuit evaluation is used.) Thus, you can see that evaluation proceeds left to right for all operators at this precedence level, since the code for a * b in a * b * c is generated even before the c has been parsed. Similar reasoning seems to apply for the other binary operators.

Tony

Reply all
Reply to author
Forward
0 new messages