🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

A problem in scripting.

Started by
2 comments, last by lgc_ustc 20 years, 2 months ago
Greetings, I am trying to design a C-like scripting language of my own, and I have a problem now. This is about the ambiguity of unary and binary minus operators. My regular expression for numbers is: digit = [0-9] nat = digit+ singedNat = (+|-)?nat number = signedNat(¡°.¡±nat)?(E signedNat)? This can represent both integers and floating point numbers. However, note that there is the unary minus operator in the regular expression, problems occur. To see how that happens, consider the following expression: a = 1-1.5; b = 1- -1.5; The first minus operator in the first statement is binary, and the first minus operator in the second statement is binary too, but the sencond one is unary. How can the scanner tell the difference? Any suggestions about this very welcome! Thanks in advance.
Advertisement
The general way to do it is to have the minus sign as a separate token, rather than recognizing it as part of a number literal, and to defer figuring out what it actually is until the parsing phase.

"Sneftel is correct, if rather vulgar." --Flarelocke
Thanks for your reply It seems that I was mislead by some texts. I agree with you, if we pass the ambiguity resolution to the parser, things would be easier. Without suffix operators "++" and "--", the following grammar can eliminate this ambiguity:

digit = [0-9]
nat = digit+
singedNat = (+|-)?nat
number = nat(¡°.¡±nat)?(E signedNat)?

exp-> exp addop term | term
addop-> + | -
term-> term mulop factor | factor
mulop-> * | /
factor-> (exp) | number | addop number | id

Am I correct?
That looks about right.

"Sneftel is correct, if rather vulgar." --Flarelocke

This topic is closed to new replies.

Advertisement