views:

387

answers:

12

I am currently designing and implementing a small programming language as an extra-credit project in a class I'm taking. My problem is that the language has three numeric types: Long, Double, and Fraction. Fractions can be written in the language as proper or improper fractions (e.g. "2 1/3" or "1/2"). This fact leads to problems such as "2/3.5" (Long/Double) and "2/3"(Long/Long) not being handled correctly by the lexer.The best solution that I see is to change the division operator. So far, I think "\" is the best solution since "//" starts comments.

Would you pick "\", if you were designing the language?

Would you pick something else? If so, what?

Note: changing the way fractions are written is not possible.

Thanks in advance for your help,

-EDIT-

Current operators are:

** (power)

* (multiply)

% (modulus)

+ (add)

- (subtract and negation)

~ (reciprocal)

! (NOT)

> (greater)

< (less)

>= (greater-equal)

<= (less-equal)

== (equality)

<> (inequality)

& (AND)

| (OR)

^ (XOR)

?? (null-coalesing)

= (assignment)

Note: I am under no strictly binding demands by my teacher; however, I basically convinced him that I could turn a simple fraction calculator into a language. Changing the fraction's syntax would go against the fraction calculator's syntax for fractions, which is preferably avoided.

+5  A: 

I'm not quite sure what's the problem, since I don't know grammar theory. But I would try changing proper fraction syntax, e.g. to 2_1/3, with underscore rather than space.

About using \ as division operator — I would certainly not like it, since it is used in about every language as an escape character. I wouldn't recommend such overload of meanings.

doublep
@Gman: Fine. I withdraw my comment.
Robert Harvey
@Robert Harvey: valid identifiers dont' start with numbers, so using _ in a fraction literal would be okay. the overloaded division I would find more confusing.
Jimmy
I like the underscore idea, that would work with my lexer quite well, but I would like to exhaust all possibility of saving the space.
Mackenzie
Having given it more thought, this would require giving up the concept of improper fraction literals, which is not something I want to do.
Mackenzie
Robert Harvey
+6  A: 

How about a word "div"?

Fyodor Soikin
+1, not a bad idea (the Pascal way) but you should, at a minimum, also use mul for multiplication, otherwise there's a inconsistency between operators.
paxdiablo
+1  A: 

If it's for a class, I would simply use a keyword like dividedby. Otherwise you are going to have to figure out what the / means by its context. There really aren't any other single characters that are suitable.

Be aware, however, that your teacher may be asking you to solve the contextual problem, in which case he wants you to use the slash. I would ask.

Robert Harvey
Oh my god, you are reinventing COBOL!
spong
@Spong. Yes, I thought of that. But it *is* just a class assignment.
Robert Harvey
@Robert Harvey I am implementing the language as an indirect result of an assignment, out of my own free-will, but my professor is giving me extra-credit for it anyway. There is a very good possibility that I am going to release the language. However, as I think Dennis Ritchie once said, it is unlikely that a language will actually find many supporters.
Mackenzie
+1  A: 

What about requiring Fraction type to use a double in all cases; ie, 2.0 / 3.5 vs 2 / 3.5 Then no new operator needed.

drewk
Unfortunately, some doubles could not accurately be converted to a fraction.
Mackenzie
Then you shouldn't be using doubles for the denominators.
Wallacoloo
@wallacoloo I am not allowing doubles to be the denominators of fractions. The problems mainly arise as a result of long/long division. For example, is "2/3" an improper fraction, or is it a long divided-by another long? I should not have used "2/3.5" as an example, because I am not currently supporting Long/Double division operations.
Mackenzie
+1  A: 

You could adopt J's somewhat nonstandard convention of % for division (because it kinda looks like the division symbol) and | for modulo.

But then you have to find something else for bitwise-or. Perhaps /?

David
A: 

I will not pick '\' because it is less accesible than '/' in keyboards. and also, it is more widely used. so your programmers will fell more in home.

SDReyes
+7  A: 

Are you allowing compact if statements like

condition ? trueblocks : falseblocks;

in your grammar? If not, you could use ':' as your division operator. It is after all the standard mathematical symbol for division (by hand, on paper) in many (most?) places around the word.

Pedery
Currently, no, but I would like to reserve the possibility for later addition. My parser s written by hand, and does not have support for ternary operators yet.
Mackenzie
Using a colon this way doesn't preclude using something else for the ternary operator. Python (and, iirc, Perl) allow you to say `trueblock if condition else falseblock` which is a pretty defensible alternative, for example.
David Winslow
+1  A: 

I'd avoid \ like crazy since it would confound people's expectations.

I would go ahead and use / but get the problem out of the lexer and into the parser where it will be easier to deal with and you can issue sensible error messages.

Norman Ramsey
I have shared your sentiment up till now. I originally intended fractions to function as longs as well, but with their addition to the language it makes for some very strange division rules (i.e. Double/Double == allowed, but Long/Long == forbidden).
Mackenzie
+1  A: 

I think the two-character /| is a possible idea. / is the division and | looks like fraction rotated 90 degrees.

If you allow bracket or unary operators, I could recommend you [2 1/2] or #1/2.

SHiNKiROU
+1  A: 

How about making fractions the primary data type? Thinking of the division operator as an infix constructor?

just somebody
+9  A: 
mpez0
That would be a great solution, but most keyboards do not support that character, which would make the language awkward to use.
Mackenzie
+2  A: 

Another reason to avoid using \ as a replacement for / is because it may cause confusion as to which operand is the numerator and which is the denominator. For example, MATLAB arithmetic operators include both right division / and left division \. The value of 1/2 is 0.5, but the value of 1\2 is 2. In other words, / has the numerator on the left, and \ has the numerator on the right.

gnovice
Matlab is just weird. "Everything is a matrix" as a programming paradigm is much more bizarre than other fashions like "everything is an object", "everything is a function" or "everything is a string".
ShreevatsaR
In MATLAB's world, though, the two division operators are necessary. For two matrices, `(A^-1)*B` is not the same as `B*(A^-1)`.And I consistently make the argument that MATLAB is more of a programmable calculator than a general purpose programming language.
rlbond