my_bc "<mathematical expression>"
Example: my_bc "1 + 2 * (3 - 42) % 5"
A valid expression:
- must only contain the operators
+
,-
,*
,/
, and%
- must only have integer values
- can contain parentheses, but each group must be properly closed
- can contain spaces
This program uses whole number arithmetic: divisions are euclidean, keeping only the quotient, while the remainder can be obtained with a modulo operation.
The program first parses the expression using the shunting-yard algorithm, outputting a data structure containing the tokens of the expression in reverse Polish notation order.
Syntax errors are caught during parsing. If there are any, the program produces no output.
Once the parsing is completed, the expression in evaluated from its reverse-polish notation form.