|
|
|
|
|
by dcrazy
46 days ago
|
|
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:
would be lexed into this sequence of tokens: You could use the shunting yard algorithm to put these in a convenient postfix order: 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: