Wednesday, February 10, 2010

Grammar booboo

Suzanne asked about a weird production in the starter grammar I provided ... which seems to allow an expression on the left-hand-side of an assignment.    Oops. 

In many programming languages, there are "lexprs" (left-hand expressions) and "rexprs" (right-hand expressions), and there can be lexprs inside rexprs and vice versa.  For example, you could have something like this in Java:
     myArray[ j + 5 ].itsField =  (foo.bar()).baz();
where j + 5 is an rexpr (denoting a value), but myArray[j + 5] is a lexpr  (denoting a location from which we can get a value or into which we can store a value). 

But not in Cool.   All fields are private.  There are no arrays.  I don't think you can have anything more complicated than a simple identifier on the left side of an assignment.

My bad.  You could just check for this in the type-checker, but a better solution is to fix the grammar so that the left side of an assignment is an identifier and not an expression.  Good catch Suzanne.

No comments:

Post a Comment