Hacker News new | past | comments | ask | show | jobs | submit login
Top-Down operator precedence parsing (thegreenplace.net)
27 points by mnemonik on Jan 2, 2010 | hide | past | favorite | 9 comments



The more I code, the more reasons I find to simply use parentheses everywhere. Why bother even thinking about 1 + 2 * 4 when you could write 1 + (2 * 4)

Ok, if you've got a lot of operators in one line, it can get difficult, but you're probably running into trouble reading it even without parenthesization. It seems like one of the huge wins of Lisp that you don't have to worry about this problem.


Agreed. Whenever I see one of those operator precedence tables in a programming language book, I just skip it. Other than BODMAS, I have no real idea what precendence logical/bitwise operators etc have - I just use parentheses, and always have done.

Like you say - why waste brain cycles trying to parse expressions. But then I also write comments in my code :-)


This brings up an interesting problem for me. I've written a number of compilers where operators were not defined at parse-time, but handled via macros that can be defined at any point. This makes the logic for transforming infix operators very difficult. If a change was made to make all operators operate left-to-right, this issue would go away. What do you guys think about this? Would it cause you serious problems while writing code?


In addition to Smalltalk, the APL family (including J and K) do this too. It doesn't cause serious problems while writing code; once you get used to it, it's a whole lot simpler. It does, however, create a barrier to mainstream acceptance. So it seems to me your decision depends on how badly you want the latter.


I've often seen the claim that not having operator precedence hurts a language's acceptance, but is there any concrete evidence of that? Could it just be a coincidence?

Were it not for the closed/licensed/expensive/commercial nature of Smalltalks, the language probably would not have fallen into to the relative obscurity it suffers today. I personally doubt that operators had anything to do with it. From what I've read of the history of the era, the language itself was much-loved and rising in popularity until the introduction of Java disrupted the traditional Smalltalk business models.


While it isn't exactly the same thing, OCaml requires you to use different operators (e.g. + vs. +.) for ints and floating point numbers, and it's a very common complaint about the language. (It has conventional precedence.)

When conventional arithmetic doesn't "just work", languages will often give a first impression of being awkward, even though the rules of operator precedence are actually more complicated than just consistently using prefix or infix left-to-right.


This is how Smalltalk does it. "Operators" are just binary messages that are evaluated from left to right.


I just introduce variables instead to keep the complexity of expressions down.


Even for combining function calls (or when using a ternary operator or LINQ call) I'll make heavy use of brackets for clarity.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: