The input line: 42 The tokens are < 42 > The parseTree is [{ 42.0, [], [], 0.0 }] // KEY IDEA: this one won't work until you tokenize tilde ~ correctly! // The input line: 42~10 The tokens are < 42, ~, 10 > The parseTree is [{ 42.0, [], [], 10.0 }] The input line: 42 m The tokens are < 42, m > The parseTree is [{ 42.0, [m], [], 0.0 }] The input line: 42~10 v s m The tokens are < 42, ~, 10, v, s, m > The parseTree is [{ 42.0, [m, s, v], [], 10.0 }] The input line: 5+6 The tokens are < 5, +, 6 > The parseTree is [+, [{ 5.0, [], [], 0.0 }], [{ 6.0, [], [], 0.0 }]] // KEY IDEA: this grammar is _right_-associative // The input line: 5-6+7 The tokens are < 5, -, 6, +, 7 > The parseTree is [-, [{ 5.0, [], [], 0.0 }], [+, [{ 6.0, [], [], 0.0 }], [{ 7.0, [], [], 0.0 }]]] The input line: 5*6 The tokens are < 5, *, 6 > The parseTree is [*, [{ 5.0, [], [], 0.0 }], [{ 6.0, [], [], 0.0 }]] // KEY IDEA: division is not evaluated until the Evaluation step... // The input line: 5/6 The tokens are < 5, /, 6 > The parseTree is [/, [{ 5.0, [], [], 0.0 }], [{ 6.0, [], [], 0.0 }]] // KEY IDEA: as a result, division always creates TWO quantity lists // there will NEVER be denominator units until things evaluate! // The input line: 3 m/s The tokens are < 3, m, /, s > The parseTree is [/, [{ 3.0, [m], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]] The input line: 3 m * 2 m/s The tokens are < 3, m, *, 2, m, /, s > The parseTree is [*, [{ 3.0, [m], [], 0.0 }], [/, [{ 2.0, [m], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]]] The input line: 5+6*7 The tokens are < 5, +, 6, *, 7 > The parseTree is [+, [{ 5.0, [], [], 0.0 }], [*, [{ 6.0, [], [], 0.0 }], [{ 7.0, [], [], 0.0 }]]] The input line: 18 m/s + 42 m/s The tokens are < 18, m, /, s, +, 42, m, /, s > The parseTree is [+, [/, [{ 18.0, [m], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]], [/, [{ 42.0, [m], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]]] // KEY IDEA: multiplication should have higher precedence, so the grouping // will be to the left // The input line: 5*6+7 The tokens are < 5, *, 6, +, 7 > The parseTree is [+, [*, [{ 5.0, [], [], 0.0 }], [{ 6.0, [], [], 0.0 }]], [{ 7.0, [], [], 0.0 }]] The input line: 3^2 The tokens are < 3, ^, 2 > The parseTree is [^, [{ 3.0, [], [], 0.0 }], 2] The input line: 3 m/s^2 The tokens are < 3, m, /, s, ^, 2 > The parseTree is [/, [{ 3.0, [m], [], 0.0 }], [^, [{ 1.0, [s], [], 0.0 }], 2]] // KEY IDEA: grouping does not add any structure; it only changes precedence! // there's nothing special to make this happen, other than implementing U() // The input line: (42) The tokens are < (, 42, ) > The parseTree is [{ 42.0, [], [], 0.0 }] The input line: (4+2) The tokens are < (, 4, +, 2, ) > The parseTree is [+, [{ 4.0, [], [], 0.0 }], [{ 2.0, [], [], 0.0 }]] The input line: (4+2)*7 The tokens are < (, 4, +, 2, ), *, 7 > The parseTree is [*, [+, [{ 4.0, [], [], 0.0 }], [{ 2.0, [], [], 0.0 }]], [{ 7.0, [], [], 0.0 }]] The input line: (3 m/s)^2 The tokens are < (, 3, m, /, s, ), ^, 2 > The parseTree is [^, [/, [{ 3.0, [m], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]], 2] The input line: (1 m/s + 5 m/s)/(3/s) The tokens are < (, 1, m, /, s, +, 5, m, /, s, ), /, (, 3, /, s, ) > The parseTree is [/, [+, [/, [{ 1.0, [m], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]], [/, [{ 5.0, [m], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]]], [/, [{ 3.0, [], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]]] The input line: (-42) The tokens are < (, -, 42, ) > The parseTree is [-, [{ 42.0, [], [], 0.0 }]] The input line: 60 m +(-18 m) The tokens are < 60, m, +, (, -, 18, m, ) > The parseTree is [+, [{ 60.0, [m], [], 0.0 }], [-, [{ 18.0, [m], [], 0.0 }]]] // KEY IDEA: these def and # parse trees are not too tricky in the Parser // they're unusual in how they get evaluated... // The input line: def answer 42 The tokens are < def, answer, 42 > The parseTree is [def, answer, [{ 42.0, [], [], 0.0 }]] The input line: 10 answer / s The tokens are < 10, answer, /, s > The parseTree is [/, [{ 10.0, [answer], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]] The input line: # 10 answer/s The tokens are < #, 10, answer, /, s > The parseTree is [#, [/, [{ 10.0, [answer], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]]] The input line: 10 answer - 9 answer The tokens are < 10, answer, -, 9, answer > The parseTree is [-, [{ 10.0, [answer], [], 0.0 }], [{ 9.0, [answer], [], 0.0 }]] The input line: def x 3 m/s The tokens are < def, x, 3, m, /, s > The parseTree is [def, x, [/, [{ 3.0, [m], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]]] The input line: 3*(11 m/s + x) The tokens are < 3, *, (, 11, m, /, s, +, x, ) > The parseTree is [*, [{ 3.0, [], [], 0.0 }], [+, [/, [{ 11.0, [m], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]], [{ 1.0, [x], [], 0.0 }]]] The input line: def answer 7*(x+x) The tokens are < def, answer, 7, *, (, x, +, x, ) > The parseTree is [def, answer, [*, [{ 7.0, [], [], 0.0 }], [+, [{ 1.0, [x], [], 0.0 }], [{ 1.0, [x], [], 0.0 }]]]] The input line: def hz 1/s The tokens are < def, hz, 1, /, s > The parseTree is [def, hz, [/, [{ 1.0, [], [], 0.0 }], [{ 1.0, [s], [], 0.0 }]]] The input line: answer + 18 m hz The tokens are < answer, +, 18, m, hz > The parseTree is [+, [{ 1.0, [answer], [], 0.0 }], [{ 18.0, [hz, m], [], 0.0 }]]