Shunting-Yard Animation
11–17 of 17 posts
Re: Shunting-Yard Animation
#12I'm not familiar with the algorithm and I don't see much explanation on the site (at least on mobile) but AFAICT this is turning parenthesized infix expressions into reverse Polish notation. In other words, it takes human-readable mathematical formulas and converts them into something a simple stack-based machine can compute in one forward pass. It also appears that separate digits aren't interpreted as decimal numer…
I didn't find it misleading; it says it operates on tokens, not digits.
Re: Shunting-Yard Animation
#13Re: Shunting-Yard Animation
#14That isn't a fundamental limit of the algorithm though; you can easily add error checking, as I did here: https://github.com/Timmmm/expr/blob/1b0aef8f91460974d526b5ba...
I'm not really sure why this is omitted.
Re: Shunting-Yard Animation
#15Earlier quoted context omitted.
It treated that as 1 0 0 + 8 8 / 4 which is nonsensical, but it has no error detection so it rolled with it. Really `100` should be its own token, but there's no way to input that.
How would one typically implement this tokenization? Pre-pass on the input? My initial thought was to push an operand-terminator token when encountering an operator, but it was unclear to me whether it should be pushed to the stack or the output.
For example, this input:
13- 2 *5 + 4
would be lexed into this sequence of tokens: "13" "-" "2" "*" "5" "+" "4"
You could use the shunting yard algorithm to put these in a convenient postfix order: "13" "2" "5" "*" "-" "4" "+"
It's really easy to then turn it into an abstract syntax tree (AST). Go through each token in order: if it's a number you make it into a node and push it onto a stack; if it's a binary operator you pop two nodes off the stack and make a node out of those and the binary operator. If you write down this AST as an s-expression you get: (+ (- 13 (* 2 5)) 4)Re: Shunting-Yard Animation
#16Earlier quoted context omitted.
It treated that as 1 0 0 + 8 8 / 4 which is nonsensical, but it has no error detection so it rolled with it. Really `100` should be its own token, but there's no way to input that.
How would one typically implement this tokenization? Pre-pass on the input? My initial thought was to push an operand-terminator token when encountering an operator, but it was unclear to me whether it should be pushed to the stack or the output.
Re: Shunting-Yard Animation
#17This is great! As a kid I used to love HO scale model trains. There is an old movie of me on my birthday looking at my train set with eyes as big as plates. Fast forward, and I love networking and programming. I recognized that networking is just trains on Ethernet or Wifi, but now realize even the programming is just the same. Still making things go places and go around after all this time.