Share your repls and programming experiences

← Back to all posts
Recursive Descent Calculator
Coder100 (17137)

Recursive Descent Calculator

This is a recursive descent calculator. It uses recursion to both interpret and parse the input. The lexer is just a for loop.

Languages used

bash for the running and node for the logic


The code should be pretty self-explanatory, but it comes from here and here.


Bad tutorial

  1. Lexer: Loop through each character of the code and look for things accordingly
  2. Parser: Given a grammar, convert it to javascript. Then, use the definition of lowest precedence to use recursion to go down/up.
  3. Interpreter: Recursively evaluate the tree.

I think the code will help more.


I hope this helps you with your language! It certainly taught me a lot!

Have a great day


JosephSanthosh (1171)

This is so accurate -- I got to mention I hate calculators but this is cool!

9751 (27)


SixBeeps (5067)

Wow, it even follows Order of Operations. Nice job!

ayush2007 (42)

I tried big numbers and it worked

DynamicSquid (4635)

How do you do a carriage return on the console?

Coder100 (17137)

wdym, I don't think calculators like carriage returns... @DynamicSquid

DynamicSquid (4635)

@Coder100 oh, I meant what is a carriage return? is it the ENTER key? and isn't that just \n?

Coder100 (17137)

oh, not exactly, on older systems, \r had to be along with \n. @DynamicSquid