4 ms·
The calculator seems to miss the point of recursive descent parsing. The parser should follow naturally from the grammar. As an example, here is a 52 line pytho
by coolSCV 14y ago
The calculator seems to miss the point of recursive descent parsing. The parser should follow naturally from the grammar. As an example, here is a 52 line python calculator:
https://gist.github.com/ascv/5022712 https://gist.github.com/ascv/5022712
The idea isn't to 1-up the OP but rather to showcase the technique of recursive descent parsing. The wikipedia entry for recursive descent parsing also has a very nice example.
- erezsh 14y agoWell, to be fair, your calculator seems to miss the point of being a calculator... > 10/2*2 2.5
- coolSCV 14y agoClearly a bug. Thanks when I have more time I will fix it. I didn't give much thought to operator precedence, I assumed the user would specify it using parentheses. The idea is what is important though. Recursive descent parsing uses the functions that correspond to types in the grammar to mutually call each other thus parsing the expression recursively in a descending fashion.
- coolSCV 14y agoI decided to simply add to the description, "use parentheses to specify operator precedence."